Sync usage with man page.
[netbsd-mini2440.git] / usr.sbin / chown / chown.c
blobd4fed219ffa23456c6651f534a11d152c4d803e7
1 /* $NetBSD: chown.c,v 1.31 2005/03/16 02:55:10 xtraeme Exp $ */
3 /*
4 * Copyright (c) 1988, 1993, 1994, 2003
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) 1988, 1993, 1994, 2003\
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[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94";
41 #else
42 __RCSID("$NetBSD: chown.c,v 1.31 2005/03/16 02:55:10 xtraeme Exp $");
43 #endif
44 #endif /* not lint */
46 #include <sys/types.h>
47 #include <sys/stat.h>
49 #include <ctype.h>
50 #include <dirent.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <locale.h>
54 #include <fts.h>
55 #include <grp.h>
56 #include <pwd.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
62 static void a_gid(const char *);
63 static void a_uid(const char *);
64 static id_t id(const char *, const char *);
65 static void usage(void);
67 static uid_t uid;
68 static gid_t gid;
69 static int ischown;
70 static char *myname;
72 int
73 main(int argc, char **argv)
75 FTS *ftsp;
76 FTSENT *p;
77 int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval, vflag;
78 char *cp;
79 int (*change_owner)(const char *, uid_t, gid_t);
81 (void)setlocale(LC_ALL, "");
83 myname = (cp = strrchr(*argv, '/')) ? cp + 1 : *argv;
84 ischown = (myname[2] == 'o');
86 Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
87 while ((ch = getopt(argc, argv, "HLPRfhv")) != -1)
88 switch (ch) {
89 case 'H':
90 Hflag = 1;
91 Lflag = 0;
92 break;
93 case 'L':
94 Lflag = 1;
95 Hflag = 0;
96 break;
97 case 'P':
98 Hflag = Lflag = 0;
99 break;
100 case 'R':
101 Rflag = 1;
102 break;
103 case 'f':
104 fflag = 1;
105 break;
106 case 'h':
108 * In System V the -h option causes chown/chgrp to
109 * change the owner/group of the symbolic link.
110 * 4.4BSD's symbolic links didn't have owners/groups,
111 * so it was an undocumented noop.
112 * In NetBSD 1.3, lchown(2) is introduced.
114 hflag = 1;
115 break;
116 case 'v':
117 vflag = 1;
118 break;
119 case '?':
120 default:
121 usage();
123 argv += optind;
124 argc -= optind;
126 if (argc < 2)
127 usage();
129 fts_options = FTS_PHYSICAL;
130 if (Rflag) {
131 if (Hflag)
132 fts_options |= FTS_COMFOLLOW;
133 if (Lflag) {
134 if (hflag)
135 errx(EXIT_FAILURE,
136 "the -L and -h options "
137 "may not be specified together.");
138 fts_options &= ~FTS_PHYSICAL;
139 fts_options |= FTS_LOGICAL;
141 } else if (!hflag)
142 fts_options |= FTS_COMFOLLOW;
144 uid = (uid_t)-1;
145 gid = (gid_t)-1;
146 if (ischown) {
147 if ((cp = strchr(*argv, ':')) != NULL) {
148 *cp++ = '\0';
149 a_gid(cp);
151 #ifdef SUPPORT_DOT
152 else if ((cp = strrchr(*argv, '.')) != NULL) {
153 if (uid_from_user(*argv, &uid) == -1) {
154 *cp++ = '\0';
155 a_gid(cp);
158 #endif
159 a_uid(*argv);
160 } else
161 a_gid(*argv);
163 if ((ftsp = fts_open(++argv, fts_options, NULL)) == NULL)
164 err(EXIT_FAILURE, "fts_open");
166 for (rval = EXIT_SUCCESS; (p = fts_read(ftsp)) != NULL;) {
167 change_owner = chown;
168 switch (p->fts_info) {
169 case FTS_D:
170 if (!Rflag) /* Change it at FTS_DP. */
171 fts_set(ftsp, p, FTS_SKIP);
172 continue;
173 case FTS_DNR: /* Warn, chown, continue. */
174 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
175 rval = EXIT_FAILURE;
176 break;
177 case FTS_ERR: /* Warn, continue. */
178 case FTS_NS:
179 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
180 rval = EXIT_FAILURE;
181 continue;
182 case FTS_SL: /* Ignore unless -h. */
184 * All symlinks we found while doing a physical
185 * walk end up here.
187 if (!hflag)
188 continue;
190 * Note that if we follow a symlink, fts_info is
191 * not FTS_SL but FTS_F or whatever. And we should
192 * use lchown only for FTS_SL and should use chown
193 * for others.
195 change_owner = lchown;
196 break;
197 case FTS_SLNONE: /* Ignore. */
199 * The only symlinks that end up here are ones that
200 * don't point to anything. Note that if we are
201 * doing a phisycal walk, we never reach here unless
202 * we asked to follow explicitly.
204 continue;
205 default:
206 break;
209 if ((*change_owner)(p->fts_accpath, uid, gid) && !fflag) {
210 warn("%s", p->fts_path);
211 rval = EXIT_FAILURE;
212 } else {
213 if (vflag)
214 printf("%s\n", p->fts_path);
217 if (errno)
218 err(EXIT_FAILURE, "fts_read");
219 exit(rval);
220 /* NOTREACHED */
223 static void
224 a_gid(const char *s)
226 struct group *gr;
228 if (*s == '\0') /* Argument was "uid[:.]". */
229 return;
230 gr = *s == '#' ? NULL : getgrnam(s);
231 if (gr == NULL)
232 gid = id(s, "group");
233 else
234 gid = gr->gr_gid;
235 return;
238 static void
239 a_uid(const char *s)
241 if (*s == '\0') /* Argument was "[:.]gid". */
242 return;
243 if (*s == '#' || uid_from_user(s, &uid) == -1) {
244 uid = id(s, "user");
246 return;
249 static id_t
250 id(const char *name, const char *type)
252 id_t val;
253 char *ep;
255 errno = 0;
256 if (*name == '#')
257 name++;
258 val = (id_t)strtoul(name, &ep, 10);
259 if (errno)
260 err(EXIT_FAILURE, "%s", name);
261 if (*ep != '\0')
262 errx(EXIT_FAILURE, "%s: invalid %s name", name, type);
263 return (val);
266 static void
267 usage(void)
270 (void)fprintf(stderr,
271 "usage: %s [-R [-H | -L | -P]] [-fhv] %s file ...\n",
272 myname, ischown ? "[owner][:group]" : "group");
273 exit(EXIT_FAILURE);