2 * Copyright (c) 1989, 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
35 static char const copyright
[] =
36 "@(#) Copyright (c) 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid
[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
45 #include <sys/cdefs.h>
46 __RCSID("$FreeBSD: src/bin/chmod/chmod.c,v 1.27 2002/08/04 05:29:13 obrien Exp $");
48 #include <sys/types.h>
61 #include "chmod_acl.h"
67 int main(int, char *[]);
71 main(int argc
, char *argv
[])
78 int Hflag
, Lflag
, Rflag
, ch
, fts_options
, hflag
, rval
;
81 mode_t newmode
, omode
;
83 unsigned int acloptflags
= 0;
84 int aclpos
= -1, inheritance_level
= 0;
85 int index
= 0, acloptlen
= 0, ace_arg_not_required
= 0;
86 acl_t acl_input
= NULL
;
88 int (*change_mode
)(const char *, mode_t
);
92 Hflag
= Lflag
= Rflag
= fflag
= hflag
= vflag
= 0;
94 while ((ch
= getopt(argc
, argv
, "HLPRXfghorstuvwx")) != -1)
96 while ((ch
= getopt(argc
, argv
, "ACEHILPRVXafginorstuvwx")) != -1)
119 * In System V (and probably POSIX.2) the -h option
120 * causes chmod to change the mode of the symbolic
121 * link. 4.4BSD's symbolic links didn't have modes,
122 * so it was an undocumented noop. In FreeBSD 3.0,
123 * lchmod(2) is introduced and this option does real
130 if (argv
[optind
- 1][0] == '-' &&
131 argv
[optind
- 1][1] == ch
)
135 acloptflags
|= ACL_FLAG
| ACL_FROM_STDIN
;
138 acloptflags
|= ACL_FLAG
| ACL_CHECK_CANONICITY
;
139 ace_arg_not_required
= 1;
142 acloptflags
|= ACL_FLAG
| ACL_REMOVE_INHERIT_FLAG
;
143 ace_arg_not_required
= 1;
146 acloptflags
|= ACL_FLAG
| ACL_REMOVE_INHERITED_ENTRIES
;
147 ace_arg_not_required
= 1;
150 acloptflags
|= ACL_FLAG
| ACL_NO_TRANSLATE
;
153 acloptflags
|= ACL_FLAG
| ACL_INVOKE_EDITOR
;
154 ace_arg_not_required
= 1;
156 #endif /* __APPLE__ */
159 * "-[rwx]" are valid mode commands. If they are the entire
160 * argument, getopt has moved past them, so decrement optind.
161 * Regardless, we're done argument processing.
163 case 'g': case 'o': case 'r': case 's':
164 case 't': case 'u': case 'w': case 'X': case 'x':
165 if (argv
[optind
- 1][0] == '-' &&
166 argv
[optind
- 1][1] == ch
&&
167 argv
[optind
- 1][2] == '\0')
177 done
: argv
+= optind
;
181 if (argc
< ((acloptflags
& ACL_FLAG
) ? 1 : 2))
189 if (!(acloptflags
& ACL_FLAG
) && ((acloptlen
= strlen(argv
[0])) > 1) && (argv
[0][1] == 'a')) {
190 acloptflags
|= ACL_FLAG
;
191 switch (argv
[0][0]) {
193 acloptflags
|= ACL_SET_FLAG
;
196 acloptflags
|= ACL_DELETE_FLAG
;
199 acloptflags
|= ACL_REWRITE_FLAG
;
202 acloptflags
&= ~ACL_FLAG
;
210 for (index
= 2; index
< acloptlen
; index
++) {
211 switch (argv
[0][index
]) {
213 acloptflags
|= ACL_ORDER_FLAG
;
215 if (argc
< ((acloptflags
& ACL_DELETE_FLAG
)
221 aclpos
= strtol(argv
[0], &ep
, 0);
223 if (aclpos
> ACL_MAX_ENTRIES
227 err(1, "Invalid ACL entry number: %s", aclpos
);
228 if (acloptflags
& ACL_DELETE_FLAG
)
229 ace_arg_not_required
= 1;
233 acloptflags
|= ACL_INHERIT_FLAG
;
234 /* The +aii.. syntax to specify
235 * inheritance level is rather unwieldy,
236 * find an alternative.
239 if (inheritance_level
> 1)
240 warn("Inheritance across more than one generation is not currently supported");
241 if (inheritance_level
>= MAX_INHERITANCE_LEVEL
)
258 fts_options
= FTS_PHYSICAL
;
261 "the -R and -h options may not be specified together.");
263 fts_options
|= FTS_COMFOLLOW
;
265 fts_options
&= ~FTS_PHYSICAL
;
266 fts_options
|= FTS_LOGICAL
;
269 fts_options
= hflag
? FTS_PHYSICAL
: FTS_LOGICAL
;
273 change_mode
= lchmod
;
278 #endif /* __APPLE__ */
280 if (acloptflags
& ACL_FROM_STDIN
) {
281 int readval
= 0, readtotal
= 0;
283 mode
= (char *) malloc(MAX_ACL_TEXT_SIZE
);
286 err(1, "Unable to allocate mode string");
287 /* Read the ACEs from STDIN */
289 readtotal
+= readval
;
290 readval
= read(STDIN_FILENO
, mode
+ readtotal
,
292 } while ((readval
> 0) && (readtotal
<= MAX_ACL_TEXT_SIZE
));
295 err(1, "-E specified, but read from STDIN failed");
297 mode
[readtotal
- 1] = '\0';
305 if ((acloptflags
& ACL_FLAG
)) {
307 /* Are we deleting by entry number, verifying
308 * canonicity or performing some other operation that
309 * does not require an input entry? If so, there's no
312 if (ace_arg_not_required
) {
316 /* Parse the text into an ACL*/
317 acl_input
= parse_acl_entries(mode
);
318 if (acl_input
== NULL
) {
320 err(1, "Invalid ACL specification: %s", mode
);
325 #endif /* __APPLE__*/
326 if (*mode
>= '0' && *mode
<= '7') {
328 val
= strtol(mode
, &ep
, 8);
329 if (val
> USHRT_MAX
|| val
< 0)
332 err(1, "Invalid file mode: %s", mode
);
334 errx(1, "Invalid file mode: %s", mode
);
338 if ((set
= setmode(mode
)) == NULL
)
339 errx(1, "Invalid file mode: %s", mode
);
344 #endif /* __APPLE__*/
345 if ((ftsp
= fts_open(++argv
, fts_options
, 0)) == NULL
)
347 for (rval
= 0; (p
= fts_read(ftsp
)) != NULL
;) {
348 switch (p
->fts_info
) {
349 case FTS_D
: /* Change it at FTS_DP. */
351 fts_set(ftsp
, p
, FTS_SKIP
);
353 case FTS_DNR
: /* Warn, chmod, continue. */
354 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
357 case FTS_ERR
: /* Warn, continue. */
359 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
362 case FTS_SL
: /* Ignore. */
365 * The only symlinks that end up here are ones that
366 * don't point to anything and ones that we found
367 * doing a physical walk.
377 /* If an ACL manipulation option was specified, manipulate */
378 if (acloptflags
& ACL_FLAG
) {
379 if (0 != modify_file_acl(acloptflags
, p
->fts_accpath
, acl_input
, aclpos
, inheritance_level
))
383 #endif /* __APPLE__ */
384 newmode
= oct
? omode
: getmode(set
, p
->fts_statp
->st_mode
);
385 if ((newmode
& ALLPERMS
) == (p
->fts_statp
->st_mode
& ALLPERMS
))
387 if ((*change_mode
)(p
->fts_accpath
, newmode
) && !fflag
) {
388 warn("%s", p
->fts_path
);
392 (void)printf("%s", p
->fts_accpath
);
397 strmode(p
->fts_statp
->st_mode
, m1
);
398 strmode((p
->fts_statp
->st_mode
&
399 S_IFMT
) | newmode
, m2
);
401 (void)printf(": 0%o [%s] -> 0%o [%s]",
402 p
->fts_statp
->st_mode
, m1
,
403 (p
->fts_statp
->st_mode
& S_IFMT
) |
412 #endif /* __APPLE__*/
419 if (mode
&& (acloptflags
& ACL_FROM_STDIN
))
422 #endif /* __APPLE__ */
432 (void)fprintf(stderr
,
433 "usage: chmod [-fv] [-R [-H | -L | -P]] [-a | +a | =a [i][# [ n]]] mode|entry file ...\n");
435 (void)fprintf(stderr
,
436 "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
437 #endif /* __APPLE__ */