xref: /original-bsd/usr.sbin/vipw/vipw.c (revision 28e93ce0)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1987 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)vipw.c	5.16 (Berkeley) 03/03/91";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <pwd.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 char *progname = "vipw";
26 char *tempname;
27 
28 main()
29 {
30 	register int pfd, tfd;
31 	struct stat begin, end;
32 
33 	pw_init();
34 	pfd = pw_lock();
35 	tfd = pw_tmp();
36 	copyfile(pfd, tfd);
37 	(void)close(tfd);
38 
39 	for (;;) {
40 		if (stat(tempname, &begin))
41 			pw_error(tempname, 1, 1);
42 		pw_edit(0);
43 		if (stat(tempname, &end))
44 			pw_error(tempname, 1, 1);
45 		if (begin.st_mtime == end.st_mtime) {
46 			(void)fprintf(stderr, "vipw: no changes made\n");
47 			pw_error((char *)NULL, 0, 0);
48 		}
49 		if (pw_mkdb())
50 			break;
51 		pw_prompt();
52 	}
53 	exit(0);
54 }
55 
56 copyfile(from, to)
57 	register int from, to;
58 {
59 	register int nr, nw, off;
60 	char buf[8*1024];
61 
62 	while ((nr = read(from, buf, sizeof(buf))) > 0)
63 		for (off = 0; off < nr; nr -= nw, off += nw)
64 			if ((nw = write(to, buf + off, nr)) < 0)
65 				pw_error(tempname, 1, 1);
66 	if (nr < 0)
67 		pw_error(_PATH_MASTERPASSWD, 1, 1);
68 }
69