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