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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)chmod.c 8.8 (Berkeley) 4/1/94
31 * $FreeBSD: head/bin/chmod/chmod.c 283997 2015-06-04 19:18:58Z pluknet $
34 #include <sys/param.h>
47 static void usage(void);
50 main(int argc
, char *argv
[])
55 int Hflag
, Lflag
, Rflag
, ch
, fflag
;
56 int fts_options
, hflag
, rval
, vflag
;
60 Hflag
= Lflag
= Rflag
= fflag
= hflag
= vflag
= 0;
61 while ((ch
= getopt(argc
, argv
, "HLPRXfghorstuvwx")) != -1)
82 * In System V the -h option causes chmod to change the
83 * mode of the symbolic link. 4.4BSD's symbolic links
84 * didn't have modes, so it was an undocumented noop.
85 * In FreeBSD 3.0, lchmod(2) is introduced and this
86 * option does real work.
92 * "-[rwx]" are valid mode commands. If they are the entire
93 * argument, getopt has moved past them, so decrement optind.
94 * Regardless, we're done argument processing.
96 case 'g': case 'o': case 'r': case 's':
97 case 't': case 'u': case 'w': case 'X': case 'x':
98 if (argv
[optind
- 1][0] == '-' &&
99 argv
[optind
- 1][1] == ch
&&
100 argv
[optind
- 1][2] == '\0')
110 done
: argv
+= optind
;
118 errx(1, "the -R and -h options may not be "
119 "specified together.");
121 fts_options
= FTS_LOGICAL
;
123 fts_options
= FTS_PHYSICAL
;
126 fts_options
|= FTS_COMFOLLOW
;
130 fts_options
= FTS_PHYSICAL
;
132 fts_options
= FTS_LOGICAL
;
137 if ((set
= setmode(mode
)) == NULL
) {
139 errx(1, "invalid file mode: %s", mode
);
141 /* malloc for setmode() failed */
142 err(1, "setmode failed");
145 if ((ftsp
= fts_open(++argv
, fts_options
, 0)) == NULL
)
147 for (rval
= 0; (p
= fts_read(ftsp
)) != NULL
;) {
150 if ((fts_options
& FTS_LOGICAL
) ||
151 ((fts_options
& FTS_COMFOLLOW
) &&
152 p
->fts_level
== FTS_ROOTLEVEL
))
155 atflag
= AT_SYMLINK_NOFOLLOW
;
157 switch (p
->fts_info
) {
160 fts_set(ftsp
, p
, FTS_SKIP
);
162 case FTS_DNR
: /* Warn, chmod. */
163 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
166 case FTS_DP
: /* Already changed at FTS_D. */
168 case FTS_ERR
: /* Warn, continue. */
170 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
176 newmode
= getmode(set
, p
->fts_statp
->st_mode
);
177 if ((newmode
& ALLPERMS
) == (p
->fts_statp
->st_mode
& ALLPERMS
))
179 if (fchmodat(AT_FDCWD
, p
->fts_accpath
, newmode
, atflag
) == -1
181 warn("%s", p
->fts_path
);
184 printf("%s", p
->fts_path
);
189 strmode(p
->fts_statp
->st_mode
, m1
);
190 strmode((p
->fts_statp
->st_mode
&
191 S_IFMT
) | newmode
, m2
);
192 printf(": 0%o [%s] -> 0%o [%s]",
193 p
->fts_statp
->st_mode
, m1
,
194 (p
->fts_statp
->st_mode
& S_IFMT
) |
209 "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");