chmod.c revision 1.36 1 /* $NetBSD: chmod.c,v 1.36 2011/08/29 14:51:17 joerg Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT(
35 "@(#) Copyright (c) 1989, 1993, 1994\
36 The Regents of the University of California. All rights reserved.");
37 #endif /* not lint */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
42 #else
43 __RCSID("$NetBSD: chmod.c,v 1.36 2011/08/29 14:51:17 joerg Exp $");
44 #endif
45 #endif /* not lint */
46
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 #include <sys/types.h>
50
51 #include <err.h>
52 #include <errno.h>
53 #include <fts.h>
54 #include <limits.h>
55 #include <locale.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 __dead static void usage(void);
62
63 int
64 main(int argc, char *argv[])
65 {
66 FTS *ftsp;
67 FTSENT *p;
68 mode_t *set;
69 int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
70 char *mode;
71 int (*change_mode)(const char *, mode_t);
72
73 setprogname(argv[0]);
74 (void)setlocale(LC_ALL, "");
75
76 Hflag = Lflag = Rflag = fflag = hflag = 0;
77 while ((ch = getopt(argc, argv, "HLPRXfghorstuwx")) != -1)
78 switch (ch) {
79 case 'H':
80 Hflag = 1;
81 Lflag = 0;
82 break;
83 case 'L':
84 Lflag = 1;
85 Hflag = 0;
86 break;
87 case 'P':
88 Hflag = Lflag = 0;
89 break;
90 case 'R':
91 Rflag = 1;
92 break;
93 case 'f':
94 fflag = 1;
95 break;
96 case 'h':
97 /*
98 * In System V the -h option causes chmod to
99 * change the mode of the symbolic link.
100 * 4.4BSD's symbolic links didn't have modes,
101 * so it was an undocumented noop. In NetBSD
102 * 1.3, lchmod(2) is introduced and this
103 * option does real work.
104 */
105 hflag = 1;
106 break;
107 /*
108 * XXX
109 * "-[rwx]" are valid mode commands. If they are the entire
110 * argument, getopt has moved past them, so decrement optind.
111 * Regardless, we're done argument processing.
112 */
113 case 'g': case 'o': case 'r': case 's':
114 case 't': case 'u': case 'w': case 'X': case 'x':
115 if (argv[optind - 1][0] == '-' &&
116 argv[optind - 1][1] == ch &&
117 argv[optind - 1][2] == '\0')
118 --optind;
119 goto done;
120 case '?':
121 default:
122 usage();
123 }
124 done: argv += optind;
125 argc -= optind;
126
127 if (argc < 2)
128 usage();
129
130 fts_options = FTS_PHYSICAL;
131 if (Rflag) {
132 if (hflag) {
133 errx(EXIT_FAILURE,
134 "the -R and -h options may not be specified together.");
135 /* NOTREACHED */
136 }
137 if (Hflag)
138 fts_options |= FTS_COMFOLLOW;
139 if (Lflag) {
140 fts_options &= ~FTS_PHYSICAL;
141 fts_options |= FTS_LOGICAL;
142 }
143 } else if (!hflag)
144 fts_options |= FTS_COMFOLLOW;
145 if (hflag)
146 change_mode = lchmod;
147 else
148 change_mode = chmod;
149
150 mode = *argv;
151 if ((set = setmode(mode)) == NULL) {
152 err(EXIT_FAILURE, "Cannot set file mode `%s'", mode);
153 /* NOTREACHED */
154 }
155
156 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) {
157 err(EXIT_FAILURE, "fts_open");
158 /* NOTREACHED */
159 }
160 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
161 switch (p->fts_info) {
162 case FTS_D:
163 if (!Rflag)
164 (void)fts_set(ftsp, p, FTS_SKIP);
165 break;
166 case FTS_DNR: /* Warn, chmod, continue. */
167 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
168 rval = 1;
169 break;
170 case FTS_DP: /* Already changed at FTS_D. */
171 continue;
172 case FTS_ERR: /* Warn, continue. */
173 case FTS_NS:
174 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
175 rval = 1;
176 continue;
177 case FTS_SL: /* Ignore. */
178 case FTS_SLNONE:
179 /*
180 * The only symlinks that end up here are ones that
181 * don't point to anything and ones that we found
182 * doing a physical walk.
183 */
184 if (!hflag)
185 continue;
186 /* else */
187 /* FALLTHROUGH */
188 default:
189 break;
190 }
191 if ((*change_mode)(p->fts_accpath,
192 getmode(set, p->fts_statp->st_mode)) && !fflag) {
193 warn("%s", p->fts_path);
194 rval = 1;
195 }
196 }
197 if (errno) {
198 err(EXIT_FAILURE, "fts_read");
199 /* NOTREACHED */
200 }
201 exit(rval);
202 /* NOTREACHED */
203 }
204
205 static void
206 usage(void)
207 {
208 (void)fprintf(stderr,
209 "usage: %s [-R [-H | -L | -P]] [-fh] mode file ...\n",
210 getprogname());
211 exit(1);
212 /* NOTREACHED */
213 }
214