xref: /csrg-svn/usr.sbin/vipw/vipw.c (revision 66576)
121186Sdist /*
261899Sbostic  * Copyright (c) 1987, 1993
361899Sbostic  *	The Regents of the University of California.  All rights reserved.
433502Sbostic  *
542713Sbostic  * %sccs.include.redist.c%
621186Sdist  */
721186Sdist 
813736Ssam #ifndef lint
961899Sbostic static char copyright[] =
1061899Sbostic "@(#) Copyright (c) 1987, 1993\n\
1161899Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1233502Sbostic #endif /* not lint */
1313736Ssam 
1421186Sdist #ifndef lint
15*66576Spendry static char sccsid[] = "@(#)vipw.c	8.2 (Berkeley) 04/01/94";
1633502Sbostic #endif /* not lint */
1721186Sdist 
1846378Sbostic #include <sys/types.h>
1913736Ssam #include <sys/stat.h>
20*66576Spendry 
21*66576Spendry #include <err.h>
2236857Sbostic #include <pwd.h>
2313736Ssam #include <stdio.h>
2446378Sbostic #include <stdlib.h>
2542039Sbostic #include <string.h>
26*66576Spendry #include <unistd.h>
2713736Ssam 
28*66576Spendry #include "pw_util.h"
29*66576Spendry 
3046378Sbostic char *tempname;
3137133Sbostic 
32*66576Spendry void	copyfile __P((int, int));
33*66576Spendry void	usage __P((void));
34*66576Spendry 
35*66576Spendry int
36*66576Spendry main(argc, argv)
37*66576Spendry 	int argc;
38*66576Spendry 	char *argv[];
3913736Ssam {
40*66576Spendry 	int pfd, tfd;
4146378Sbostic 	struct stat begin, end;
42*66576Spendry 	int ch;
4313736Ssam 
44*66576Spendry 	while ((ch = getopt(argc, argv, "")) != EOF)
45*66576Spendry 		switch (ch) {
46*66576Spendry 		case '?':
47*66576Spendry 		default:
48*66576Spendry 			usage();
49*66576Spendry 		}
50*66576Spendry 
51*66576Spendry 	argc -= optind;
52*66576Spendry 	argv += optind;
53*66576Spendry 
54*66576Spendry 	if (argc != 0)
55*66576Spendry 		usage();
56*66576Spendry 
5746378Sbostic 	pw_init();
5846378Sbostic 	pfd = pw_lock();
5946378Sbostic 	tfd = pw_tmp();
6046378Sbostic 	copyfile(pfd, tfd);
6146378Sbostic 	(void)close(tfd);
6231744Sbostic 
6337133Sbostic 	for (;;) {
6446378Sbostic 		if (stat(tempname, &begin))
6546378Sbostic 			pw_error(tempname, 1, 1);
6646956Sbostic 		pw_edit(0);
6746378Sbostic 		if (stat(tempname, &end))
6846378Sbostic 			pw_error(tempname, 1, 1);
6946378Sbostic 		if (begin.st_mtime == end.st_mtime) {
70*66576Spendry 			warnx("no changes made");
7146378Sbostic 			pw_error((char *)NULL, 0, 0);
7239127Sbostic 		}
7346378Sbostic 		if (pw_mkdb())
7437133Sbostic 			break;
7546378Sbostic 		pw_prompt();
7613736Ssam 	}
7736857Sbostic 	exit(0);
7813736Ssam }
7915633Sralph 
80*66576Spendry void
8146378Sbostic copyfile(from, to)
82*66576Spendry 	int from, to;
8331744Sbostic {
84*66576Spendry 	int nr, nw, off;
8546378Sbostic 	char buf[8*1024];
8646378Sbostic 
8746378Sbostic 	while ((nr = read(from, buf, sizeof(buf))) > 0)
8846378Sbostic 		for (off = 0; off < nr; nr -= nw, off += nw)
8946378Sbostic 			if ((nw = write(to, buf + off, nr)) < 0)
9046378Sbostic 				pw_error(tempname, 1, 1);
9146378Sbostic 	if (nr < 0)
9246378Sbostic 		pw_error(_PATH_MASTERPASSWD, 1, 1);
9331744Sbostic }
94*66576Spendry 
95*66576Spendry void
96*66576Spendry usage()
97*66576Spendry {
98*66576Spendry 
99*66576Spendry 	(void)fprintf(stderr, "usage: vipw\n");
100*66576Spendry 	exit(1);
101*66576Spendry }
102