chmod.c revision 1.22 1 /* $NetBSD: chmod.c,v 1.22 2000/01/20 02:50:54 mycroft 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.22 2000/01/20 02:50:54 mycroft Exp $");
48 #endif
49 #endif /* not lint */
50
51 #include <sys/types.h>
52 #include <sys/stat.h>
53
54 #include <err.h>
55 #include <errno.h>
56 #include <fts.h>
57 #include <locale.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <limits.h>
63
64 int main __P((int, char *[]));
65 void usage __P((void));
66
67 int
68 main(argc, argv)
69 int argc;
70 char *argv[];
71 {
72 FTS *ftsp;
73 FTSENT *p;
74 mode_t *set;
75 int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
76 char *mode;
77 int (*change_mode) __P((const char *, mode_t));
78
79 set = NULL; /* XXX gcc -Wuninitialized */
80
81 (void)setlocale(LC_ALL, "");
82
83 Hflag = Lflag = Rflag = fflag = hflag = 0;
84 while ((ch = getopt(argc, argv, "HLPRXfghorstuwx")) != -1)
85 switch (ch) {
86 case 'H':
87 Hflag = 1;
88 Lflag = 0;
89 break;
90 case 'L':
91 Lflag = 1;
92 Hflag = 0;
93 break;
94 case 'P':
95 Hflag = Lflag = 0;
96 break;
97 case 'R':
98 Rflag = 1;
99 break;
100 case 'f': /* XXX: undocumented. */
101 fflag = 1;
102 break;
103 case 'h':
104 /*
105 * In System V (and probably POSIX.2) the -h option
106 * causes chmod to change the mode of the symbolic
107 * link. 4.4BSD's symbolic links didn't have modes,
108 * so it was an undocumented noop. In NetBSD 1.3,
109 * lchmod(2) is introduced and this option does real
110 * work.
111 */
112 hflag = 1;
113 break;
114 /*
115 * XXX
116 * "-[rwx]" are valid mode commands. If they are the entire
117 * argument, getopt has moved past them, so decrement optind.
118 * Regardless, we're done argument processing.
119 */
120 case 'g': case 'o': case 'r': case 's':
121 case 't': case 'u': case 'w': case 'X': case 'x':
122 if (argv[optind - 1][0] == '-' &&
123 argv[optind - 1][1] == ch &&
124 argv[optind - 1][2] == '\0')
125 --optind;
126 goto done;
127 case '?':
128 default:
129 usage();
130 }
131 done: argv += optind;
132 argc -= optind;
133
134 if (argc < 2)
135 usage();
136
137 fts_options = FTS_PHYSICAL;
138 if (Rflag) {
139 if (hflag)
140 errx(1,
141 "the -R and -h options may not be specified together.");
142 if (Hflag)
143 fts_options |= FTS_COMFOLLOW;
144 if (Lflag) {
145 fts_options &= ~FTS_PHYSICAL;
146 fts_options |= FTS_LOGICAL;
147 }
148 }
149 if (hflag)
150 change_mode = lchmod;
151 else
152 change_mode = chmod;
153
154 mode = *argv;
155 if ((set = setmode(mode)) == NULL)
156 errx(1, "invalid file mode: %s", mode);
157
158 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
159 err(1, argv[0]);
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(1, "fts_read");
199 exit(rval);
200 /* NOTREACHED */
201 }
202
203 void
204 usage()
205 {
206 (void)fprintf(stderr,
207 "usage: chmod [-R [-H | -L | -P]] [-h] mode file ...\n");
208 exit(1);
209 /* NOTREACHED */
210 }
211