xref: /csrg-svn/usr.sbin/vipw/vipw.c (revision 46378)
121186Sdist /*
231744Sbostic  * Copyright (c) 1987 Regents of the University of California.
333502Sbostic  * All rights reserved.
433502Sbostic  *
542713Sbostic  * %sccs.include.redist.c%
621186Sdist  */
721186Sdist 
813736Ssam #ifndef lint
921186Sdist char copyright[] =
1031744Sbostic "@(#) Copyright (c) 1987 Regents of the University of California.\n\
1121186Sdist  All rights reserved.\n";
1233502Sbostic #endif /* not lint */
1313736Ssam 
1421186Sdist #ifndef lint
15*46378Sbostic static char sccsid[] = "@(#)vipw.c	5.15 (Berkeley) 02/12/91";
1633502Sbostic #endif /* not lint */
1721186Sdist 
18*46378Sbostic #include <sys/types.h>
1913736Ssam #include <sys/stat.h>
2036857Sbostic #include <pwd.h>
2113736Ssam #include <stdio.h>
22*46378Sbostic #include <stdlib.h>
2342039Sbostic #include <string.h>
2413736Ssam 
25*46378Sbostic char *progname = "vipw";
26*46378Sbostic char *tempname;
2737133Sbostic 
2831744Sbostic main()
2913736Ssam {
30*46378Sbostic 	register int pfd, tfd;
31*46378Sbostic 	struct stat begin, end;
3213736Ssam 
33*46378Sbostic 	pw_init();
34*46378Sbostic 	pfd = pw_lock();
35*46378Sbostic 	tfd = pw_tmp();
36*46378Sbostic 	copyfile(pfd, tfd);
37*46378Sbostic 	(void)close(tfd);
3831744Sbostic 
3937133Sbostic 	for (;;) {
40*46378Sbostic 		if (stat(tempname, &begin))
41*46378Sbostic 			pw_error(tempname, 1, 1);
42*46378Sbostic 		if (pw_edit(0)) {
43*46378Sbostic 			(void)fprintf(stderr, "vipw: edit failed\n");
44*46378Sbostic 			pw_error((char *)NULL, 0, 1);
4537133Sbostic 		}
46*46378Sbostic 		if (stat(tempname, &end))
47*46378Sbostic 			pw_error(tempname, 1, 1);
48*46378Sbostic 		if (begin.st_mtime == end.st_mtime) {
49*46378Sbostic 			(void)fprintf(stderr, "vipw: no changes made\n");
50*46378Sbostic 			pw_error((char *)NULL, 0, 0);
5139127Sbostic 		}
52*46378Sbostic 		if (pw_mkdb())
5337133Sbostic 			break;
54*46378Sbostic 		pw_prompt();
5513736Ssam 	}
5636857Sbostic 	exit(0);
5713736Ssam }
5815633Sralph 
59*46378Sbostic copyfile(from, to)
60*46378Sbostic 	register int from, to;
6131744Sbostic {
62*46378Sbostic 	register int nr, nw, off;
63*46378Sbostic 	char buf[8*1024];
64*46378Sbostic 
65*46378Sbostic 	while ((nr = read(from, buf, sizeof(buf))) > 0)
66*46378Sbostic 		for (off = 0; off < nr; nr -= nw, off += nw)
67*46378Sbostic 			if ((nw = write(to, buf + off, nr)) < 0)
68*46378Sbostic 				pw_error(tempname, 1, 1);
69*46378Sbostic 	if (nr < 0)
70*46378Sbostic 		pw_error(_PATH_MASTERPASSWD, 1, 1);
7131744Sbostic }
72