use __progname instead of argv[0] for error messages
[unix-history] / usr / src / usr.bin / chpass / pw_copy.c
CommitLineData
99ee2953 1/*-
8964deec
KB
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
99ee2953
KB
4 *
5 * %sccs.include.redist.c%
6 */
7
8#ifndef lint
cc952d1c 9static char sccsid[] = "@(#)pw_copy.c 8.2 (Berkeley) %G%";
99ee2953
KB
10#endif /* not lint */
11
12/*
13 * This module is used to copy the master password file, replacing a single
14 * record, by chpass(1) and passwd(1).
15 */
16
cc952d1c 17#include <err.h>
99ee2953
KB
18#include <pwd.h>
19#include <stdio.h>
20#include <string.h>
21
cc952d1c 22#include <pw_util.h>
99ee2953 23
cc952d1c
JSP
24extern char *tempname;
25
26void
456319a0
KB
27pw_copy(ffd, tfd, pw)
28 int ffd, tfd;
99ee2953
KB
29 struct passwd *pw;
30{
cc952d1c
JSP
31 FILE *from, *to;
32 int done;
33 char *p, buf[8192];
99ee2953
KB
34
35 if (!(from = fdopen(ffd, "r")))
36 pw_error(_PATH_MASTERPASSWD, 1, 1);
456319a0 37 if (!(to = fdopen(tfd, "w")))
99ee2953
KB
38 pw_error(tempname, 1, 1);
39
40 for (done = 0; fgets(buf, sizeof(buf), from);) {
cc952d1c
JSP
41 if (!strchr(buf, '\n')) {
42 warnx("%s: line too long", _PATH_MASTERPASSWD);
0d67ba24 43 pw_error(NULL, 0, 1);
99ee2953
KB
44 }
45 if (done) {
46 (void)fprintf(to, "%s", buf);
0d67ba24
KB
47 if (ferror(to))
48 goto err;
99ee2953
KB
49 continue;
50 }
cc952d1c
JSP
51 if (!(p = strchr(buf, ':'))) {
52 warnx("%s: corrupted entry", _PATH_MASTERPASSWD);
0d67ba24 53 pw_error(NULL, 0, 1);
99ee2953
KB
54 }
55 *p = '\0';
56 if (strcmp(buf, pw->pw_name)) {
57 *p = ':';
58 (void)fprintf(to, "%s", buf);
0d67ba24
KB
59 if (ferror(to))
60 goto err;
99ee2953
KB
61 continue;
62 }
63 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
64 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
65 pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos,
66 pw->pw_dir, pw->pw_shell);
67 done = 1;
0d67ba24
KB
68 if (ferror(to))
69 goto err;
99ee2953
KB
70 }
71 if (!done)
72 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
73 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
74 pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos,
75 pw->pw_dir, pw->pw_shell);
0d67ba24
KB
76
77 if (ferror(to))
78err: pw_error(NULL, 1, 1);
99ee2953
KB
79 (void)fclose(to);
80}