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