1 /* $NetBSD: chmod.c,v 1.38 2012/10/22 18:00:46 christos 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 "@(#) Copyright (c) 1989, 1993, 1994\
36 The Regents of the University of California. All rights reserved.");
41 static char sccsid
[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
43 __RCSID("$NetBSD: chmod.c,v 1.38 2012/10/22 18:00:46 christos Exp $");
47 #include <sys/param.h>
49 #include <sys/types.h>
62 __dead
static void usage(void);
64 struct option chmod_longopts
[] = {
65 { "reference", required_argument
, 0,
72 main(int argc
, char *argv
[])
78 int Hflag
, Lflag
, Rflag
, ch
, fflag
, fts_options
, hflag
, rval
;
79 char *mode
, *reference
;
80 int (*change_mode
)(const char *, mode_t
);
83 (void)setlocale(LC_ALL
, "");
85 Hflag
= Lflag
= Rflag
= fflag
= hflag
= 0;
87 while ((ch
= getopt_long(argc
, argv
, "HLPRXfghorstuwx",
88 chmod_longopts
, NULL
)) != -1)
112 * In System V the -h option causes chmod to
113 * change the mode of the symbolic link.
114 * 4.4BSD's symbolic links didn't have modes,
115 * so it was an undocumented noop. In NetBSD
116 * 1.3, lchmod(2) is introduced and this
117 * option does real work.
123 * "-[rwx]" are valid mode commands. If they are the entire
124 * argument, getopt has moved past them, so decrement optind.
125 * Regardless, we're done argument processing.
127 case 'g': case 'o': case 'r': case 's':
128 case 't': case 'u': case 'w': case 'X': case 'x':
129 if (argv
[optind
- 1][0] == '-' &&
130 argv
[optind
- 1][1] == ch
&&
131 argv
[optind
- 1][2] == '\0')
138 done
: argv
+= optind
;
141 if (argc
== 0 || (argc
== 1 && reference
== NULL
))
144 fts_options
= FTS_PHYSICAL
;
148 "the -R and -h options may not be specified together.");
152 fts_options
|= FTS_COMFOLLOW
;
154 fts_options
&= ~FTS_PHYSICAL
;
155 fts_options
|= FTS_LOGICAL
;
158 fts_options
|= FTS_COMFOLLOW
;
160 change_mode
= lchmod
;
164 if (reference
== NULL
) {
166 if ((set
= setmode(mode
)) == NULL
) {
167 err(EXIT_FAILURE
, "Cannot set file mode `%s'", mode
);
174 if (stat(reference
, &st
) == -1)
175 err(EXIT_FAILURE
, "Cannot stat `%s'", reference
);
180 if ((ftsp
= fts_open(argv
, fts_options
, 0)) == NULL
) {
181 err(EXIT_FAILURE
, "fts_open");
184 for (rval
= 0; (p
= fts_read(ftsp
)) != NULL
;) {
185 switch (p
->fts_info
) {
188 (void)fts_set(ftsp
, p
, FTS_SKIP
);
190 case FTS_DNR
: /* Warn, chmod, continue. */
191 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
194 case FTS_DP
: /* Already changed at FTS_D. */
196 case FTS_ERR
: /* Warn, continue. */
198 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
201 case FTS_SL
: /* Ignore. */
204 * The only symlinks that end up here are ones that
205 * don't point to anything and ones that we found
206 * doing a physical walk.
215 if ((*change_mode
)(p
->fts_accpath
,
216 set
? getmode(set
, p
->fts_statp
->st_mode
) : mval
)
218 warn("%s", p
->fts_path
);
223 err(EXIT_FAILURE
, "fts_read");
233 (void)fprintf(stderr
,
234 "Usage: %s [-R [-H | -L | -P]] [-fh] mode file ...\n"
235 "\t%s [-R [-H | -L | -P]] [-fh] --reference=rfile file ...\n",
236 getprogname(), getprogname());