1 /* $NetBSD: chown.c,v 1.31 2005/03/16 02:55:10 xtraeme Exp $ */
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
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994, 2003\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94";
42 __RCSID("$NetBSD: chown.c,v 1.31 2005/03/16 02:55:10 xtraeme Exp $");
46 #include <sys/types.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);
73 main(int argc
, char **argv
)
77 int Hflag
, Lflag
, Rflag
, ch
, fflag
, fts_options
, hflag
, rval
, vflag
;
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)
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.
129 fts_options
= FTS_PHYSICAL
;
132 fts_options
|= FTS_COMFOLLOW
;
136 "the -L and -h options "
137 "may not be specified together.");
138 fts_options
&= ~FTS_PHYSICAL
;
139 fts_options
|= FTS_LOGICAL
;
142 fts_options
|= FTS_COMFOLLOW
;
147 if ((cp
= strchr(*argv
, ':')) != NULL
) {
152 else if ((cp
= strrchr(*argv
, '.')) != NULL
) {
153 if (uid_from_user(*argv
, &uid
) == -1) {
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
) {
170 if (!Rflag
) /* Change it at FTS_DP. */
171 fts_set(ftsp
, p
, FTS_SKIP
);
173 case FTS_DNR
: /* Warn, chown, continue. */
174 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
177 case FTS_ERR
: /* Warn, continue. */
179 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
182 case FTS_SL
: /* Ignore unless -h. */
184 * All symlinks we found while doing a physical
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
195 change_owner
= lchown
;
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.
209 if ((*change_owner
)(p
->fts_accpath
, uid
, gid
) && !fflag
) {
210 warn("%s", p
->fts_path
);
214 printf("%s\n", p
->fts_path
);
218 err(EXIT_FAILURE
, "fts_read");
228 if (*s
== '\0') /* Argument was "uid[:.]". */
230 gr
= *s
== '#' ? NULL
: getgrnam(s
);
232 gid
= id(s
, "group");
241 if (*s
== '\0') /* Argument was "[:.]gid". */
243 if (*s
== '#' || uid_from_user(s
, &uid
) == -1) {
250 id(const char *name
, const char *type
)
258 val
= (id_t
)strtoul(name
, &ep
, 10);
260 err(EXIT_FAILURE
, "%s", name
);
262 errx(EXIT_FAILURE
, "%s: invalid %s name", name
, type
);
270 (void)fprintf(stderr
,
271 "usage: %s [-R [-H | -L | -P]] [-fhv] %s file ...\n",
272 myname
, ischown
? "[owner][:group]" : "group");