chmod.c revision 1.6 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.19 (Berkeley) 3/12/91";*/
42 static char rcsid[] = "$Id: chmod.c,v 1.6 1993/08/05 23:59:25 deraadt Exp $";
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <fts.h>
48 #include <stdio.h>
49 #include <string.h>
50
51 extern int errno;
52 int retval;
53
54 main(argc, argv)
55 int argc;
56 char **argv;
57 {
58 extern int optind;
59 register FTS *fts;
60 register FTSENT *p;
61 register int oct, omode;
62 register char *mode;
63 mode_t *set, *setmode();
64 struct stat sb;
65 int ch, fflag, rflag;
66
67 fflag = rflag = 0;
68 while ((ch = getopt(argc, argv, "Rfrwx")) != EOF)
69 switch((char)ch) {
70 case 'R':
71 rflag = 1;
72 break;
73 case 'f': /* no longer documented */
74 fflag = 1;
75 break;
76 case 'r': /* "-[rwx]" are valid file modes */
77 case 'w':
78 case 'x':
79 --optind;
80 goto done;
81 case '?':
82 default:
83 usage();
84 }
85 done: argv += optind;
86 argc -= optind;
87
88 if (argc < 2)
89 usage();
90
91 mode = *argv;
92 if (*mode >= '0' && *mode <= '7') {
93 omode = (int)strtol(mode, (char **)NULL, 8);
94 oct = 1;
95 } else {
96 if (!(set = setmode(mode))) {
97 (void)fprintf(stderr, "chmod: invalid file mode.\n");
98 exit(1);
99 }
100 oct = 0;
101 }
102
103 retval = 0;
104 if (rflag) {
105 if (!(fts = fts_open(++argv, FTS_PHYSICAL, 0))) {
106 (void)fprintf(stderr, "chmod: %s.\n", strerror(errno));
107 exit(1);
108 }
109 while (p = fts_read(fts))
110 switch(p->fts_info) {
111 case FTS_D:
112 case FTS_SL:
113 break;
114 case FTS_DNR:
115 case FTS_ERR:
116 case FTS_NS:
117 (void)fprintf(stderr, "chmod: %s: %s.\n",
118 p->fts_path, strerror(errno));
119 exit(1);
120 default:
121 if (chmod(p->fts_accpath,
122 oct ? omode : getmode(set, p->fts_statp->st_mode))
123 && !fflag)
124 error(p->fts_path);
125 break;
126 }
127 exit(retval);
128 }
129
130 while (*++argv) {
131 if (lstat(*argv, &sb) < 0) {
132 if(!fflag)
133 error(*argv);
134 continue;
135 }
136 if (S_ISLNK(sb.st_mode))
137 continue;
138 if (chmod(*argv, oct ? omode : getmode(set, sb.st_mode)) && !fflag)
139 error(*argv);
140 }
141 exit(retval);
142 }
143
144 error(name)
145 char *name;
146 {
147 (void)fprintf(stderr, "chmod: %s: %s.\n", name, strerror(errno));
148 retval = 1;
149 }
150
151 usage()
152 {
153 (void)fprintf(stderr, "chmod: chmod [-R] mode file ...\n");
154 exit(1);
155 }
156