chmod.c revision 1.29 1 /* $NetBSD: chmod.c,v 1.29 2003/08/04 22:31:22 jschauma 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT(
39 "@(#) Copyright (c) 1989, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n");
41 #endif /* not lint */
42
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
46 #else
47 __RCSID("$NetBSD: chmod.c,v 1.29 2003/08/04 22:31:22 jschauma Exp $");
48 #endif
49 #endif /* not lint */
50
51 #include <sys/param.h>
52 #include <sys/stat.h>
53 #include <sys/types.h>
54
55 #include <err.h>
56 #include <errno.h>
57 #include <fts.h>
58 #include <limits.h>
59 #include <locale.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include <vis.h>
65
66 int stdout_ok;
67
68 int main(int, char *[]);
69 void usage(void);
70 char *printescaped(const char *);
71
72 int
73 main(int argc, char *argv[])
74 {
75 FTS *ftsp;
76 FTSENT *p;
77 mode_t *set;
78 int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
79 char *mode, *fn;
80 int (*change_mode)(const char *, mode_t);
81
82 setprogname(argv[0]);
83 (void)setlocale(LC_ALL, "");
84
85 Hflag = Lflag = Rflag = fflag = hflag = 0;
86 while ((ch = getopt(argc, argv, "HLPRXfghorstuwx")) != -1)
87 switch (ch) {
88 case 'H':
89 Hflag = 1;
90 Lflag = 0;
91 break;
92 case 'L':
93 Lflag = 1;
94 Hflag = 0;
95 break;
96 case 'P':
97 Hflag = Lflag = 0;
98 break;
99 case 'R':
100 Rflag = 1;
101 break;
102 case 'f': /* XXX: undocumented. */
103 fflag = 1;
104 break;
105 case 'h':
106 /*
107 * In System V the -h option causes chmod to
108 * change the mode of the symbolic link.
109 * 4.4BSD's symbolic links didn't have modes,
110 * so it was an undocumented noop. In NetBSD
111 * 1.3, lchmod(2) is introduced and this
112 * option does real work.
113 */
114 hflag = 1;
115 break;
116 /*
117 * XXX
118 * "-[rwx]" are valid mode commands. If they are the entire
119 * argument, getopt has moved past them, so decrement optind.
120 * Regardless, we're done argument processing.
121 */
122 case 'g': case 'o': case 'r': case 's':
123 case 't': case 'u': case 'w': case 'X': case 'x':
124 if (argv[optind - 1][0] == '-' &&
125 argv[optind - 1][1] == ch &&
126 argv[optind - 1][2] == '\0')
127 --optind;
128 goto done;
129 case '?':
130 default:
131 usage();
132 }
133 done: argv += optind;
134 argc -= optind;
135
136 if (argc < 2)
137 usage();
138
139 stdout_ok = isatty(STDOUT_FILENO);
140
141 fts_options = FTS_PHYSICAL;
142 if (Rflag) {
143 if (hflag) {
144 errx(EXIT_FAILURE,
145 "the -R and -h options may not be specified together.");
146 /* NOTREACHED */
147 }
148 if (Hflag)
149 fts_options |= FTS_COMFOLLOW;
150 if (Lflag) {
151 fts_options &= ~FTS_PHYSICAL;
152 fts_options |= FTS_LOGICAL;
153 }
154 } else if (!hflag)
155 fts_options |= FTS_COMFOLLOW;
156 if (hflag)
157 change_mode = lchmod;
158 else
159 change_mode = chmod;
160
161 mode = *argv;
162 if ((set = setmode(mode)) == NULL) {
163 errx(EXIT_FAILURE, "invalid file mode: %s", mode);
164 /* NOTREACHED */
165 }
166
167 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) {
168 err(EXIT_FAILURE, "fts_open");
169 /* NOTREACHED */
170 }
171 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
172 switch (p->fts_info) {
173 case FTS_D:
174 if (!Rflag)
175 (void)fts_set(ftsp, p, FTS_SKIP);
176 break;
177 case FTS_DNR: /* Warn, chmod, continue. */
178 fn = printescaped(p->fts_path);
179 warnx("%s: %s", fn, strerror(p->fts_errno));
180 free(fn);
181 rval = 1;
182 break;
183 case FTS_DP: /* Already changed at FTS_D. */
184 continue;
185 case FTS_ERR: /* Warn, continue. */
186 case FTS_NS:
187 fn = printescaped(p->fts_path);
188 warnx("%s: %s", fn, strerror(p->fts_errno));
189 free(fn);
190 rval = 1;
191 continue;
192 case FTS_SL: /* Ignore. */
193 case FTS_SLNONE:
194 /*
195 * The only symlinks that end up here are ones that
196 * don't point to anything and ones that we found
197 * doing a physical walk.
198 */
199 if (!hflag)
200 continue;
201 /* else */
202 /* FALLTHROUGH */
203 default:
204 break;
205 }
206 if ((*change_mode)(p->fts_accpath,
207 getmode(set, p->fts_statp->st_mode)) && !fflag) {
208 fn = printescaped(p->fts_path);
209 warn("%s", fn);
210 free(fn);
211 rval = 1;
212 }
213 }
214 if (errno) {
215 err(EXIT_FAILURE, "fts_read");
216 /* NOTREACHED */
217 }
218 exit(rval);
219 /* NOTREACHED */
220 }
221
222 void
223 usage(void)
224 {
225 (void)fprintf(stderr,
226 "usage: %s [-R [-H | -L | -P]] [-h] mode file ...\n",
227 getprogname());
228 exit(1);
229 /* NOTREACHED */
230 }
231
232 char *
233 printescaped(const char *src)
234 {
235 size_t len;
236 char *retval;
237
238 len = strlen(src);
239 if (len != 0 && SIZE_T_MAX/len <= 4) {
240 errx(EXIT_FAILURE, "%s: name too long", src);
241 /* NOTREACHED */
242 }
243
244 retval = (char *)malloc(4*len+1);
245 if (retval != NULL) {
246 if (stdout_ok)
247 (void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
248 else
249 (void)strcpy(retval, src);
250 return retval;
251 } else
252 errx(EXIT_FAILURE, "out of memory!");
253 /* NOTREACHED */
254 }
255