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
34 #include <sys/cdefs.h>
36 __used
static char const copyright
[] =
37 "@(#) Copyright (c) 1989, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid
[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
46 #include <sys/cdefs.h>
47 __RCSID("$FreeBSD: src/bin/chmod/chmod.c,v 1.27 2002/08/04 05:29:13 obrien Exp $");
49 #include <sys/types.h>
62 #include "chmod_acl.h"
68 int main(int, char *[]);
72 main(int argc
, char *argv
[])
79 int Hflag
, Lflag
, Pflag
, Rflag
, ch
, fts_options
, hflag
, rval
;
82 mode_t newmode
, omode
;
84 unsigned int acloptflags
= 0;
86 int inheritance_level
= 0;
89 int ace_arg_not_required
= 0;
90 acl_t acl_input
= NULL
;
92 int (*change_mode
)(const char *, mode_t
);
96 Hflag
= Lflag
= Pflag
= Rflag
= fflag
= hflag
= vflag
= 0;
98 while ((ch
= getopt(argc
, argv
, "HLPRXfghorstuvwx")) != -1)
100 while ((ch
= getopt(argc
, argv
, "ACEHILNPRVXafghinorstuvwx")) != -1)
125 * In System V (and probably POSIX.2) the -h option
126 * causes chmod to change the mode of the symbolic
127 * link. 4.4BSD's symbolic links didn't have modes,
128 * so it was an undocumented noop. In FreeBSD 3.0,
129 * lchmod(2) is introduced and this option does real
136 if (argv
[optind
- 1][0] == '-' &&
137 argv
[optind
- 1][1] == ch
)
141 // acloptflags |= ACL_FLAG | ACL_TO_STDOUT;
142 // ace_arg_not_required = 1;
143 errx(1, "-A not implemented");
146 acloptflags
|= ACL_FLAG
| ACL_FROM_STDIN
;
149 acloptflags
|= ACL_FLAG
| ACL_CHECK_CANONICITY
;
150 ace_arg_not_required
= 1;
153 acloptflags
|= ACL_FLAG
| ACL_REMOVE_INHERIT_FLAG
;
154 ace_arg_not_required
= 1;
157 acloptflags
|= ACL_FLAG
| ACL_REMOVE_INHERITED_ENTRIES
;
158 ace_arg_not_required
= 1;
161 acloptflags
|= ACL_FLAG
| ACL_NO_TRANSLATE
;
164 acloptflags
|= ACL_FLAG
| ACL_CLEAR_FLAG
;
165 ace_arg_not_required
= 1;
168 // acloptflags |= ACL_FLAG | ACL_INVOKE_EDITOR;
169 // ace_arg_not_required = 1;
170 errx(1, "-V not implemented");
172 #endif /* __APPLE__ */
175 * "-[rwx]" are valid mode commands. If they are the entire
176 * argument, getopt has moved past them, so decrement optind.
177 * Regardless, we're done argument processing.
179 case 'g': case 'o': case 'r': case 's':
180 case 't': case 'u': case 'w': case 'X': case 'x':
181 if (argv
[optind
- 1][0] == '-' &&
182 argv
[optind
- 1][1] == ch
&&
183 argv
[optind
- 1][2] == '\0')
193 done
: argv
+= optind
;
197 if (argc
< ((acloptflags
& ACL_FLAG
) ? 1 : 2))
199 if (!Rflag
&& (Hflag
|| Lflag
|| Pflag
))
200 warnx("options -H, -L, -P only useful with -R");
201 #else /* !__APPLE__ */
204 #endif /* __APPLE__ */
207 if (!(acloptflags
& ACL_FLAG
) && ((acloptlen
= strlen(argv
[0])) > 1) && (argv
[0][1] == 'a')) {
208 acloptflags
|= ACL_FLAG
;
209 switch (argv
[0][0]) {
211 acloptflags
|= ACL_SET_FLAG
;
214 acloptflags
|= ACL_DELETE_FLAG
;
217 acloptflags
|= ACL_REWRITE_FLAG
;
220 acloptflags
&= ~ACL_FLAG
;
228 for (index
= 2; index
< acloptlen
; index
++) {
229 switch (argv
[0][index
]) {
231 acloptflags
|= ACL_ORDER_FLAG
;
233 if (argc
< ((acloptflags
& ACL_DELETE_FLAG
)
239 aclpos
= strtol(argv
[0], &ep
, 0);
241 if (aclpos
> ACL_MAX_ENTRIES
245 errx(1, "Invalid ACL entry number: %ld", aclpos
);
246 if (acloptflags
& ACL_DELETE_FLAG
)
247 ace_arg_not_required
= 1;
251 acloptflags
|= ACL_INHERIT_FLAG
;
252 /* The +aii.. syntax to specify
253 * inheritance level is rather unwieldy,
254 * find an alternative.
257 if (inheritance_level
> 1)
258 warnx("Inheritance across more than one generation is not currently supported");
259 if (inheritance_level
>= MAX_INHERITANCE_LEVEL
)
276 fts_options
= FTS_PHYSICAL
;
279 "the -R and -h options may not be specified together.");
281 fts_options
|= FTS_COMFOLLOW
;
283 fts_options
&= ~FTS_PHYSICAL
;
284 fts_options
|= FTS_LOGICAL
;
287 fts_options
= hflag
? FTS_PHYSICAL
: FTS_LOGICAL
;
290 change_mode
= lchmod
;
294 if (acloptflags
& ACL_FROM_STDIN
) {
296 size_t readtotal
= 0;
298 mode
= (char *) malloc(MAX_ACL_TEXT_SIZE
);
301 err(1, "Unable to allocate mode string");
302 /* Read the ACEs from STDIN */
304 readtotal
+= readval
;
305 readval
= read(STDIN_FILENO
, mode
+ readtotal
,
307 } while ((readval
> 0) && (readtotal
<= MAX_ACL_TEXT_SIZE
));
310 errx(1, "-E specified, but read from STDIN failed");
312 mode
[readtotal
- 1] = '\0';
320 if ((acloptflags
& ACL_FLAG
)) {
322 /* Are we deleting by entry number, verifying
323 * canonicity or performing some other operation that
324 * does not require an input entry? If so, there's no
327 if (ace_arg_not_required
) {
331 /* Parse the text into an ACL*/
332 acl_input
= parse_acl_entries(mode
);
333 if (acl_input
== NULL
) {
334 errx(1, "Invalid ACL specification: %s", mode
);
339 #endif /* __APPLE__*/
340 if (*mode
>= '0' && *mode
<= '7') {
342 val
= strtol(mode
, &ep
, 8);
343 if (val
> USHRT_MAX
|| val
< 0)
346 err(1, "Invalid file mode: %s", mode
);
348 errx(1, "Invalid file mode: %s", mode
);
352 if ((set
= setmode(mode
)) == NULL
)
353 errx(1, "Invalid file mode: %s", mode
);
358 #endif /* __APPLE__*/
359 if ((ftsp
= fts_open(++argv
, fts_options
, 0)) == NULL
)
361 for (rval
= 0; (p
= fts_read(ftsp
)) != NULL
;) {
362 switch (p
->fts_info
) {
365 (void)fts_set(ftsp
, p
, FTS_SKIP
);
367 case FTS_DNR
: /* Warn, chmod, continue. */
368 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
371 case FTS_DP
: /* Already changed at FTS_D. */
374 if (acloptflags
& ACL_FLAG
) /* don't need stat for -N */
376 case FTS_ERR
: /* Warn, continue. */
377 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
380 case FTS_SL
: /* Ignore. */
383 * The only symlinks that end up here are ones that
384 * don't point to anything and ones that we found
385 * doing a physical walk.
395 /* If an ACL manipulation option was specified, manipulate */
396 if (acloptflags
& ACL_FLAG
) {
397 if (0 != modify_file_acl(acloptflags
, p
->fts_accpath
, acl_input
, (int)aclpos
, inheritance_level
, !hflag
))
401 #endif /* __APPLE__ */
402 newmode
= oct
? omode
: getmode(set
, p
->fts_statp
->st_mode
);
403 if ((newmode
& ALLPERMS
) == (p
->fts_statp
->st_mode
& ALLPERMS
))
405 if ((*change_mode
)(p
->fts_accpath
, newmode
) && !fflag
) {
406 warn("Unable to change file mode on %s", p
->fts_path
);
410 (void)printf("%s", p
->fts_accpath
);
415 strmode(p
->fts_statp
->st_mode
, m1
);
416 strmode((p
->fts_statp
->st_mode
&
417 S_IFMT
) | newmode
, m2
);
419 (void)printf(": 0%o [%s] -> 0%o [%s]",
420 p
->fts_statp
->st_mode
, m1
,
421 (p
->fts_statp
->st_mode
& S_IFMT
) |
430 #endif /* __APPLE__*/
437 if (mode
&& (acloptflags
& ACL_FROM_STDIN
))
440 #endif /* __APPLE__ */
450 (void)fprintf(stderr
,
451 "usage:\tchmod [-fhv] [-R [-H | -L | -P]] [-a | +a | =a [i][# [ n]]] mode|entry file ...\n"
452 "\tchmod [-fhv] [-R [-H | -L | -P]] [-E | -C | -N | -i | -I] file ...\n"); /* add -A and -V when implemented */
454 (void)fprintf(stderr
,
455 "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
456 #endif /* __APPLE__ */