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