xref: /original-bsd/usr.sbin/vipw/vipw.c (revision a92f78cd)
1422def8fSdist /*
2*a92f78cdSpendry  * Copyright (c) 1987, 1993, 1994
3ab3002c2Sbostic  *	The Regents of the University of California.  All rights reserved.
4d078155dSbostic  *
57ead0043Sbostic  * %sccs.include.redist.c%
6422def8fSdist  */
7422def8fSdist 
8cf23d6fcSsam #ifndef lint
9ab3002c2Sbostic static char copyright[] =
10*a92f78cdSpendry "@(#) Copyright (c) 1987, 1993, 1994\n\
11ab3002c2Sbostic 	The Regents of the University of California.  All rights reserved.\n";
12d078155dSbostic #endif /* not lint */
13422def8fSdist 
14422def8fSdist #ifndef lint
15*a92f78cdSpendry static char sccsid[] = "@(#)vipw.c	8.3 (Berkeley) 04/02/94";
16d078155dSbostic #endif /* not lint */
17cf23d6fcSsam 
18065f62e4Sbostic #include <sys/types.h>
19cf23d6fcSsam #include <sys/stat.h>
207dee54c6Spendry 
217dee54c6Spendry #include <err.h>
22b06a6350Sbostic #include <pwd.h>
23b06a6350Sbostic #include <stdio.h>
24065f62e4Sbostic #include <stdlib.h>
25c76b5e6aSbostic #include <string.h>
267dee54c6Spendry #include <unistd.h>
27ea399dcdSbostic 
287dee54c6Spendry #include "pw_util.h"
297dee54c6Spendry 
30065f62e4Sbostic char *tempname;
316944685aSbostic 
327dee54c6Spendry void	copyfile __P((int, int));
337dee54c6Spendry void	usage __P((void));
347dee54c6Spendry 
357dee54c6Spendry int
main(argc,argv)367dee54c6Spendry main(argc, argv)
377dee54c6Spendry 	int argc;
387dee54c6Spendry 	char *argv[];
396944685aSbostic {
407dee54c6Spendry 	int pfd, tfd;
41065f62e4Sbostic 	struct stat begin, end;
427dee54c6Spendry 	int ch;
437dee54c6Spendry 
447dee54c6Spendry 	while ((ch = getopt(argc, argv, "")) != EOF)
457dee54c6Spendry 		switch (ch) {
467dee54c6Spendry 		case '?':
477dee54c6Spendry 		default:
487dee54c6Spendry 			usage();
497dee54c6Spendry 		}
507dee54c6Spendry 
517dee54c6Spendry 	argc -= optind;
527dee54c6Spendry 	argv += optind;
537dee54c6Spendry 
547dee54c6Spendry 	if (argc != 0)
557dee54c6Spendry 		usage();
566944685aSbostic 
57065f62e4Sbostic 	pw_init();
58065f62e4Sbostic 	pfd = pw_lock();
59065f62e4Sbostic 	tfd = pw_tmp();
60065f62e4Sbostic 	copyfile(pfd, tfd);
61065f62e4Sbostic 	(void)close(tfd);
626944685aSbostic 
638b868815Sbostic 	for (;;) {
64065f62e4Sbostic 		if (stat(tempname, &begin))
65065f62e4Sbostic 			pw_error(tempname, 1, 1);
663bac0ec5Sbostic 		pw_edit(0);
67065f62e4Sbostic 		if (stat(tempname, &end))
68065f62e4Sbostic 			pw_error(tempname, 1, 1);
69065f62e4Sbostic 		if (begin.st_mtime == end.st_mtime) {
707dee54c6Spendry 			warnx("no changes made");
71065f62e4Sbostic 			pw_error((char *)NULL, 0, 0);
728b868815Sbostic 		}
73065f62e4Sbostic 		if (pw_mkdb())
74ea399dcdSbostic 			break;
75065f62e4Sbostic 		pw_prompt();
76cf23d6fcSsam 	}
77b06a6350Sbostic 	exit(0);
78b06a6350Sbostic }
79b06a6350Sbostic 
807dee54c6Spendry void
copyfile(from,to)81065f62e4Sbostic copyfile(from, to)
827dee54c6Spendry 	int from, to;
836944685aSbostic {
847dee54c6Spendry 	int nr, nw, off;
85065f62e4Sbostic 	char buf[8*1024];
866944685aSbostic 
87065f62e4Sbostic 	while ((nr = read(from, buf, sizeof(buf))) > 0)
88065f62e4Sbostic 		for (off = 0; off < nr; nr -= nw, off += nw)
89065f62e4Sbostic 			if ((nw = write(to, buf + off, nr)) < 0)
90065f62e4Sbostic 				pw_error(tempname, 1, 1);
91065f62e4Sbostic 	if (nr < 0)
92065f62e4Sbostic 		pw_error(_PATH_MASTERPASSWD, 1, 1);
93ea399dcdSbostic }
947dee54c6Spendry 
957dee54c6Spendry void
usage()967dee54c6Spendry usage()
977dee54c6Spendry {
987dee54c6Spendry 
997dee54c6Spendry 	(void)fprintf(stderr, "usage: vipw\n");
1007dee54c6Spendry 	exit(1);
1017dee54c6Spendry }
102