xref: /original-bsd/usr.sbin/vipw/vipw.c (revision 3413c235)
1 /*
2  * Copyright (c) 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1987, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)vipw.c	8.2 (Berkeley) 04/01/94";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 
21 #include <err.h>
22 #include <pwd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include "pw_util.h"
29 
30 char *tempname;
31 
32 void	copyfile __P((int, int));
33 void	usage __P((void));
34 
35 int
36 main(argc, argv)
37 	int argc;
38 	char *argv[];
39 {
40 	int pfd, tfd;
41 	struct stat begin, end;
42 	int ch;
43 
44 	while ((ch = getopt(argc, argv, "")) != EOF)
45 		switch (ch) {
46 		case '?':
47 		default:
48 			usage();
49 		}
50 
51 	argc -= optind;
52 	argv += optind;
53 
54 	if (argc != 0)
55 		usage();
56 
57 	pw_init();
58 	pfd = pw_lock();
59 	tfd = pw_tmp();
60 	copyfile(pfd, tfd);
61 	(void)close(tfd);
62 
63 	for (;;) {
64 		if (stat(tempname, &begin))
65 			pw_error(tempname, 1, 1);
66 		pw_edit(0);
67 		if (stat(tempname, &end))
68 			pw_error(tempname, 1, 1);
69 		if (begin.st_mtime == end.st_mtime) {
70 			warnx("no changes made");
71 			pw_error((char *)NULL, 0, 0);
72 		}
73 		if (pw_mkdb())
74 			break;
75 		pw_prompt();
76 	}
77 	exit(0);
78 }
79 
80 void
81 copyfile(from, to)
82 	int from, to;
83 {
84 	int nr, nw, off;
85 	char buf[8*1024];
86 
87 	while ((nr = read(from, buf, sizeof(buf))) > 0)
88 		for (off = 0; off < nr; nr -= nw, off += nw)
89 			if ((nw = write(to, buf + off, nr)) < 0)
90 				pw_error(tempname, 1, 1);
91 	if (nr < 0)
92 		pw_error(_PATH_MASTERPASSWD, 1, 1);
93 }
94 
95 void
96 usage()
97 {
98 
99 	(void)fprintf(stderr, "usage: vipw\n");
100 	exit(1);
101 }
102