]>
git.saurik.com Git - apple/file_cmds.git/blob - chmod/chmod.c
4dae785bb260e19b18a3f238df44737610ba2863
1 /* $NetBSD: chmod.c,v 1.20 1998/07/28 05:31:22 mycroft Exp $ */
4 * Copyright (c) 1989, 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
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
39 "@(#) Copyright (c) 1989, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n");
45 static char sccsid
[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
47 __RCSID("$NetBSD: chmod.c,v 1.20 1998/07/28 05:31:22 mycroft Exp $");
51 #include <sys/types.h>
64 int main
__P((int, char *[]));
65 void usage
__P((void));
77 int Hflag
, Lflag
, Rflag
, ch
, fflag
, fts_options
, hflag
, rval
;
79 int (*change_mode
) __P((const char *, mode_t
));
81 set
= NULL
; /* XXX gcc -Wuninitialized */
82 omode
= 0; /* XXX gcc -Wuninitialized */
84 (void)setlocale(LC_ALL
, "");
86 Hflag
= Lflag
= Rflag
= fflag
= hflag
= 0;
87 while ((ch
= getopt(argc
, argv
, "HLPRXfghorstuwx")) != -1)
103 case 'f': /* XXX: undocumented. */
109 * In System V (and probably POSIX.2) the -h option
110 * causes chmod to change the mode of the symbolic
111 * link. 4.4BSD's symbolic links didn't have modes,
112 * so it was an undocumented noop. In NetBSD 1.3,
113 * lchmod(2) is introduced and this option does real
121 * "-[rwx]" are valid mode commands. If they are the entire
122 * argument, getopt has moved past them, so decrement optind.
123 * Regardless, we're done argument processing.
125 case 'g': case 'o': case 'r': case 's':
126 case 't': case 'u': case 'w': case 'X': case 'x':
127 if (argv
[optind
- 1][0] == '-' &&
128 argv
[optind
- 1][1] == ch
&&
129 argv
[optind
- 1][2] == '\0')
136 done
: argv
+= optind
;
142 fts_options
= FTS_PHYSICAL
;
146 "the -R and -h options may not be specified together.");
148 fts_options
|= FTS_COMFOLLOW
;
150 fts_options
&= ~FTS_PHYSICAL
;
151 fts_options
|= FTS_LOGICAL
;
156 change_mode
= lchmod
;
164 if (*mode
>= '0' && *mode
<= '7') {
166 val
= strtol(mode
, &ep
, 8);
167 if (val
> INT_MAX
|| val
< 0)
170 err(1, "invalid file mode: %s", mode
);
172 errx(1, "invalid file mode: %s", mode
);
176 if ((set
= setmode(mode
)) == NULL
)
177 errx(1, "invalid file mode: %s", mode
);
181 if ((ftsp
= fts_open(++argv
, fts_options
, 0)) == NULL
)
183 for (rval
= 0; (p
= fts_read(ftsp
)) != NULL
;) {
184 switch (p
->fts_info
) {
187 (void)fts_set(ftsp
, p
, FTS_SKIP
);
189 case FTS_DNR
: /* Warn, chmod, continue. */
190 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
193 case FTS_DP
: /* Already changed at FTS_D. */
195 case FTS_ERR
: /* Warn, continue. */
197 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
200 case FTS_SL
: /* Ignore. */
203 * The only symlinks that end up here are ones that
204 * don't point to anything and ones that we found
205 * doing a physical walk.
218 if ((*change_mode
)(p
->fts_accpath
, oct
? omode
:
219 getmode(set
, p
->fts_statp
->st_mode
)) && !fflag
) {
220 warn("%s", p
->fts_path
);
234 (void)fprintf(stderr
,
235 "usage: chmod [-R [-H | -L | -P]] [-h] mode file ...\n");
237 (void)fprintf(stderr
,
238 "usage: chmod [-R [-H | -L | -P]] mode file ...\n");