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