chmod.c revision 1.7 1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * 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
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)chmod.c 5.21 (Berkeley) 1/27/92";*/
42 static char rcsid[] = "$Id: chmod.c,v 1.7 1993/08/06 01:52:36 mycroft Exp $";
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <errno.h>
48 #include <fts.h>
49 #include <unistd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 int retval;
55
56 void err __P((const char *, ...));
57 void error __P((char *));
58 void usage __P((void));
59
60 int
61 main(argc, argv)
62 int argc;
63 char *argv[];
64 {
65 register FTS *fts;
66 register FTSENT *p;
67 register int oct, omode;
68 struct stat sb;
69 mode_t *set;
70 int ch, fflag, rflag;
71 char *ep, *mode;
72
73 fflag = rflag = 0;
74 while ((ch = getopt(argc, argv, "Rfrwx")) != EOF)
75 switch((char)ch) {
76 case 'R':
77 rflag = 1;
78 break;
79 case 'f': /* no longer documented */
80 fflag = 1;
81 break;
82 case 'r': /* "-[rwx]" are valid file modes */
83 case 'w':
84 case 'x':
85 --optind;
86 goto done;
87 case '?':
88 default:
89 usage();
90 }
91 done: argv += optind;
92 argc -= optind;
93
94 if (argc < 2)
95 usage();
96
97 mode = *argv;
98 if (*mode >= '0' && *mode <= '7') {
99 omode = (int)strtol(mode, &ep, 8);
100 if (omode < 0 || *ep)
101 err("invalid file mode: %s", mode);
102 oct = 1;
103 } else {
104 if (!(set = setmode(mode)))
105 err("invalid file mode: %s", mode);
106 oct = 0;
107 }
108
109 retval = 0;
110 if (rflag) {
111 if ((fts = fts_open(++argv, FTS_PHYSICAL, 0)) == NULL)
112 err("%s", strerror(errno));
113 while (p = fts_read(fts))
114 switch(p->fts_info) {
115 case FTS_D:
116 case FTS_SL:
117 break;
118 case FTS_DNR:
119 case FTS_ERR:
120 case FTS_NS:
121 err("%s: %s", p->fts_path, strerror(errno));
122 default:
123 if (chmod(p->fts_accpath, oct ? omode :
124 getmode(set, p->fts_statp->st_mode)) &&
125 !fflag)
126 error(p->fts_path);
127 break;
128 }
129 exit(retval);
130 }
131 while (*++argv) {
132 if (lstat(*argv, &sb) < 0) {
133 if (!fflag)
134 error(*argv);
135 continue;
136 }
137 if (S_ISLNK(sb.st_mode))
138 continue;
139 if (chmod(*argv, oct ? omode : getmode(set, sb.st_mode)) &&
140 !fflag)
141 error(*argv);
142 }
143 exit(retval);
144 }
145
146 void
147 error(name)
148 char *name;
149 {
150 (void)fprintf(stderr, "chmod: %s: %s\n", name, strerror(errno));
151 retval = 1;
152 }
153
154 void
155 usage()
156 {
157 (void)fprintf(stderr, "usage: chmod [-R] mode file ...\n");
158 exit(1);
159 }
160
161 #if __STDC__
162 #include <stdarg.h>
163 #else
164 #include <varargs.h>
165 #endif
166
167 void
168 #if __STDC__
169 err(const char *fmt, ...)
170 #else
171 err(fmt, va_alist)
172 char *fmt;
173 va_dcl
174 #endif
175 {
176 va_list ap;
177 #if __STDC__
178 va_start(ap, fmt);
179 #else
180 va_start(ap);
181 #endif
182 (void)fprintf(stderr, "chmod: ");
183 (void)vfprintf(stderr, fmt, ap);
184 va_end(ap);
185 (void)fprintf(stderr, "\n");
186 exit(1);
187 /* NOTREACHED */
188 }
189