xref: /csrg-svn/usr.sbin/vipw/vipw.c (revision 66648)
121186Sdist /*
2*66648Spendry  * Copyright (c) 1987, 1993, 1994
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[] =
10*66648Spendry "@(#) Copyright (c) 1987, 1993, 1994\n\
1161899Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1233502Sbostic #endif /* not lint */
1313736Ssam 
1421186Sdist #ifndef lint
15*66648Spendry static char sccsid[] = "@(#)vipw.c	8.3 (Berkeley) 04/02/94";
1633502Sbostic #endif /* not lint */
1721186Sdist 
1846378Sbostic #include <sys/types.h>
1913736Ssam #include <sys/stat.h>
2066576Spendry 
2166576Spendry #include <err.h>
2236857Sbostic #include <pwd.h>
2313736Ssam #include <stdio.h>
2446378Sbostic #include <stdlib.h>
2542039Sbostic #include <string.h>
2666576Spendry #include <unistd.h>
2713736Ssam 
2866576Spendry #include "pw_util.h"
2966576Spendry 
3046378Sbostic char *tempname;
3137133Sbostic 
3266576Spendry void	copyfile __P((int, int));
3366576Spendry void	usage __P((void));
3466576Spendry 
3566576Spendry int
main(argc,argv)3666576Spendry main(argc, argv)
3766576Spendry 	int argc;
3866576Spendry 	char *argv[];
3913736Ssam {
4066576Spendry 	int pfd, tfd;
4146378Sbostic 	struct stat begin, end;
4266576Spendry 	int ch;
4313736Ssam 
4466576Spendry 	while ((ch = getopt(argc, argv, "")) != EOF)
4566576Spendry 		switch (ch) {
4666576Spendry 		case '?':
4766576Spendry 		default:
4866576Spendry 			usage();
4966576Spendry 		}
5066576Spendry 
5166576Spendry 	argc -= optind;
5266576Spendry 	argv += optind;
5366576Spendry 
5466576Spendry 	if (argc != 0)
5566576Spendry 		usage();
5666576Spendry 
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) {
7066576Spendry 			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 
8066576Spendry void
copyfile(from,to)8146378Sbostic copyfile(from, to)
8266576Spendry 	int from, to;
8331744Sbostic {
8466576Spendry 	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 }
9466576Spendry 
9566576Spendry void
usage()9666576Spendry usage()
9766576Spendry {
9866576Spendry 
9966576Spendry 	(void)fprintf(stderr, "usage: vipw\n");
10066576Spendry 	exit(1);
10166576Spendry }
102