xref: /csrg-svn/usr.sbin/vipw/vipw.c (revision 46956)
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*46956Sbostic static char sccsid[] = "@(#)vipw.c	5.16 (Berkeley) 03/03/91";
1633502Sbostic #endif /* not lint */
1721186Sdist 
1846378Sbostic #include <sys/types.h>
1913736Ssam #include <sys/stat.h>
2036857Sbostic #include <pwd.h>
2113736Ssam #include <stdio.h>
2246378Sbostic #include <stdlib.h>
2342039Sbostic #include <string.h>
2413736Ssam 
2546378Sbostic char *progname = "vipw";
2646378Sbostic char *tempname;
2737133Sbostic 
2831744Sbostic main()
2913736Ssam {
3046378Sbostic 	register int pfd, tfd;
3146378Sbostic 	struct stat begin, end;
3213736Ssam 
3346378Sbostic 	pw_init();
3446378Sbostic 	pfd = pw_lock();
3546378Sbostic 	tfd = pw_tmp();
3646378Sbostic 	copyfile(pfd, tfd);
3746378Sbostic 	(void)close(tfd);
3831744Sbostic 
3937133Sbostic 	for (;;) {
4046378Sbostic 		if (stat(tempname, &begin))
4146378Sbostic 			pw_error(tempname, 1, 1);
42*46956Sbostic 		pw_edit(0);
4346378Sbostic 		if (stat(tempname, &end))
4446378Sbostic 			pw_error(tempname, 1, 1);
4546378Sbostic 		if (begin.st_mtime == end.st_mtime) {
4646378Sbostic 			(void)fprintf(stderr, "vipw: no changes made\n");
4746378Sbostic 			pw_error((char *)NULL, 0, 0);
4839127Sbostic 		}
4946378Sbostic 		if (pw_mkdb())
5037133Sbostic 			break;
5146378Sbostic 		pw_prompt();
5213736Ssam 	}
5336857Sbostic 	exit(0);
5413736Ssam }
5515633Sralph 
5646378Sbostic copyfile(from, to)
5746378Sbostic 	register int from, to;
5831744Sbostic {
5946378Sbostic 	register int nr, nw, off;
6046378Sbostic 	char buf[8*1024];
6146378Sbostic 
6246378Sbostic 	while ((nr = read(from, buf, sizeof(buf))) > 0)
6346378Sbostic 		for (off = 0; off < nr; nr -= nw, off += nw)
6446378Sbostic 			if ((nw = write(to, buf + off, nr)) < 0)
6546378Sbostic 				pw_error(tempname, 1, 1);
6646378Sbostic 	if (nr < 0)
6746378Sbostic 		pw_error(_PATH_MASTERPASSWD, 1, 1);
6831744Sbostic }
69