vm: fix sanity checks on arm
[minix.git] / usr.sbin / vipw / vipw.c
bloba6f97b2fdb32b52683d8ff83132ba8d482623d6f
1 /* $NetBSD: vipw.c,v 1.14 2009/04/19 00:44:49 lukem Exp $ */
3 /*
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)vipw.c 8.3 (Berkeley) 4/2/94";
41 #else
42 __RCSID("$NetBSD: vipw.c,v 1.14 2009/04/19 00:44:49 lukem Exp $");
43 #endif
44 #endif /* not lint */
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/stat.h>
50 #include <err.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <fcntl.h>
57 #include <errno.h>
58 #include <util.h>
60 int main __P((int, char **));
61 static void copyfile __P((int, int));
62 static void usage __P((void));
64 char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN];
66 int
67 main(int argc, char *argv[])
69 const char *prefix;
70 int pfd, tfd;
71 struct stat begin, end;
72 int ch;
74 prefix = "";
75 while ((ch = getopt(argc, argv, "d:")) != -1) {
76 switch (ch) {
77 case 'd':
78 prefix = optarg;
79 if (pw_setprefix(prefix) < 0)
80 err(1, "%s", prefix);
81 break;
82 case '?':
83 default:
84 usage();
87 argc -= optind;
88 argv += optind;
90 if (argc != 0)
91 usage();
93 (void)snprintf(mpwd, sizeof(mpwd), "%s%s", prefix, _PATH_MASTERPASSWD);
94 (void)snprintf(mpwdl, sizeof(mpwdl), "%s%s", prefix,
95 _PATH_MASTERPASSWD_LOCK);
97 pw_init();
98 tfd = pw_lock(0);
99 if (tfd < 0) {
100 if (errno == EEXIST)
101 errx(1, "the passwd file is busy.");
102 else
103 err(1, "%s", mpwdl);
106 pfd = open(mpwd, O_RDONLY, 0);
107 if (pfd < 0)
108 pw_error(mpwd, 1, 1);
109 copyfile(pfd, tfd);
110 (void)close(tfd);
112 for (;;) {
113 if (stat(mpwdl, &begin))
114 pw_error(mpwdl, 1, 1);
115 pw_edit(0, NULL);
116 if (stat(mpwdl, &end))
117 pw_error(mpwdl, 1, 1);
118 if (begin.st_mtime == end.st_mtime &&
119 begin.st_mtimensec == end.st_mtimensec) {
120 warnx("no changes made");
121 pw_error((char *)NULL, 0, 0);
123 if (pw_mkdb(NULL, 0) == 0)
124 break;
125 pw_prompt();
127 return 0;
130 static void
131 copyfile(int from, int to)
133 int nr, nw, off;
134 char buf[8*1024];
136 while ((nr = read(from, buf, sizeof(buf))) > 0)
137 for (off = 0; off < nr; nr -= nw, off += nw)
138 if ((nw = write(to, buf + off, nr)) < 0)
139 pw_error(mpwdl, 1, 1);
140 if (nr < 0)
141 pw_error(mpwd, 1, 1);
144 static void
145 usage(void)
148 (void)fprintf(stderr, "usage: %s [-d directory]\n", getprogname());
149 exit(1);