]>
git.saurik.com Git - apple/file_cmds.git/blob - chown/chown.c
3598ecc8f7b64f87789bd24c37d4c42b7a667a4c
2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
36 __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n");
42 static char sccsid
[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94";
44 __RCSID("$NetBSD: chown.c,v 1.15 1998/10/05 21:37:39 kim Exp $");
48 #include <sys/param.h>
64 void a_gid
__P((char *));
65 void a_uid
__P((char *));
66 void chownerr
__P((char *));
67 u_long id
__P((char *, char *));
68 int main
__P((int, char **));
69 void usage
__P((void));
73 int Rflag
, ischown
, fflag
;
83 int Hflag
, Lflag
, Pflag
, ch
, fts_options
, hflag
, rval
;
85 int (*change_owner
) __P((const char *, uid_t
, gid_t
));
87 setlocale(LC_ALL
, "");
89 myname
= (cp
= strrchr(*argv
, '/')) ? cp
+ 1 : *argv
;
90 ischown
= myname
[2] == 'o';
92 Hflag
= Lflag
= Pflag
= hflag
= 0;
93 while ((ch
= getopt(argc
, argv
, "HLPRfh")) != -1)
115 * In System V the -h option causes chown/chgrp to
116 * change the owner/group of the symbolic link.
117 * 4.4BSD's symbolic links didn't have owners/groups,
118 * so it was an undocumented noop.
119 * In NetBSD 1.3, lchown(2) is introduced.
133 fts_options
= FTS_PHYSICAL
;
136 fts_options
|= FTS_COMFOLLOW
;
139 errx(1, "the -L and -h options may not be specified together.");
140 fts_options
&= ~FTS_PHYSICAL
;
141 fts_options
|= FTS_LOGICAL
;
146 change_owner
= lchown
;
148 change_owner
= chown
;
150 change_owner
= chown
;
155 if ((cp
= strchr(*argv
, ':')) != NULL
) {
160 else if ((cp
= strchr(*argv
, '.')) != NULL
) {
169 if ((ftsp
= fts_open(++argv
, fts_options
, 0)) == NULL
)
172 for (rval
= 0; (p
= fts_read(ftsp
)) != NULL
;) {
173 switch (p
->fts_info
) {
175 if (!Rflag
) /* Change it at FTS_DP. */
176 fts_set(ftsp
, p
, FTS_SKIP
);
178 case FTS_DNR
: /* Warn, chown, continue. */
179 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
182 case FTS_ERR
: /* Warn, continue. */
184 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
187 case FTS_SL
: /* Ignore. */
190 * The only symlinks that end up here are ones that
191 * don't point to anything and ones that we found
192 * doing a physical walk.
206 if ((*change_owner
)(p
->fts_accpath
, uid
, gid
) && !fflag
) {
207 warn("%s", p
->fts_path
);
222 if (*s
== '\0') /* Argument was "uid[:.]". */
225 gid
= ((gr
= getgrnam(s
)) == NULL
) ? id(s
, "group") : gr
->gr_gid
;
234 if (*s
== '\0') /* Argument was "[:.]gid". */
236 uid
= ((pw
= getpwnam(s
)) == NULL
) ? id(s
, "user") : pw
->pw_uid
;
248 * We know that uid_t's and gid_t's are unsigned longs.
251 val
= strtoul(name
, &ep
, 10);
255 errx(1, "%s: invalid %s name", name
, type
);
262 (void)fprintf(stderr
,
263 "usage: %s [-R [-H | -L | -P]] [-fh] %s file ...\n",
264 myname
, ischown
? "[owner][:group]" : "group");