xref: /original-bsd/usr.sbin/vipw/vipw.c (revision 74d2773b)
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.15 (Berkeley) 02/12/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 		if (pw_edit(0)) {
43 			(void)fprintf(stderr, "vipw: edit failed\n");
44 			pw_error((char *)NULL, 0, 1);
45 		}
46 		if (stat(tempname, &end))
47 			pw_error(tempname, 1, 1);
48 		if (begin.st_mtime == end.st_mtime) {
49 			(void)fprintf(stderr, "vipw: no changes made\n");
50 			pw_error((char *)NULL, 0, 0);
51 		}
52 		if (pw_mkdb())
53 			break;
54 		pw_prompt();
55 	}
56 	exit(0);
57 }
58 
59 copyfile(from, to)
60 	register int from, to;
61 {
62 	register int nr, nw, off;
63 	char buf[8*1024];
64 
65 	while ((nr = read(from, buf, sizeof(buf))) > 0)
66 		for (off = 0; off < nr; nr -= nw, off += nw)
67 			if ((nw = write(to, buf + off, nr)) < 0)
68 				pw_error(tempname, 1, 1);
69 	if (nr < 0)
70 		pw_error(_PATH_MASTERPASSWD, 1, 1);
71 }
72