1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/types.h>
33 #include <sys/stat.h>
34
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <fts.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 static volatile sig_atomic_t siginfo;
46
47 static void usage(void) __dead2;
48
49 static void
siginfo_handler(int sig __unused)50 siginfo_handler(int sig __unused)
51 {
52
53 siginfo = 1;
54 }
55
56 int
main(int argc,char * argv[])57 main(int argc, char *argv[])
58 {
59 FTS *ftsp;
60 FTSENT *p;
61 u_long clear, newflags, set;
62 long val;
63 int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
64 int ch, e, fts_options, oct, rval;
65 char *flags, *ep;
66
67 Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0;
68 while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1)
69 switch (ch) {
70 case 'H':
71 Hflag = 1;
72 Lflag = 0;
73 break;
74 case 'L':
75 Lflag = 1;
76 Hflag = 0;
77 break;
78 case 'P':
79 Hflag = Lflag = 0;
80 break;
81 case 'R':
82 Rflag = 1;
83 break;
84 case 'f':
85 fflag = 1;
86 break;
87 case 'h':
88 hflag = 1;
89 break;
90 case 'v':
91 vflag++;
92 break;
93 case 'x':
94 xflag = 1;
95 break;
96 case '?':
97 default:
98 usage();
99 }
100 argv += optind;
101 argc -= optind;
102
103 if (argc < 2)
104 usage();
105
106 (void)signal(SIGINFO, siginfo_handler);
107
108 if (Rflag) {
109 if (hflag)
110 errx(1, "the -R and -h options may not be "
111 "specified together.");
112 if (Lflag) {
113 fts_options = FTS_LOGICAL;
114 } else {
115 fts_options = FTS_PHYSICAL;
116
117 if (Hflag) {
118 fts_options |= FTS_COMFOLLOW;
119 }
120 }
121 } else if (hflag) {
122 fts_options = FTS_PHYSICAL;
123 } else {
124 fts_options = FTS_LOGICAL;
125 }
126 if (xflag)
127 fts_options |= FTS_XDEV;
128
129 flags = *argv;
130 if (*flags >= '0' && *flags <= '7') {
131 errno = 0;
132 val = strtol(flags, &ep, 8);
133 if (val < 0)
134 errno = ERANGE;
135 if (errno)
136 err(1, "invalid flags: %s", flags);
137 if (*ep)
138 errx(1, "invalid flags: %s", flags);
139 set = val;
140 oct = 1;
141 } else {
142 if (strtofflags(&flags, &set, &clear))
143 errx(1, "invalid flag: %s", flags);
144 clear = ~clear;
145 oct = 0;
146 }
147
148 if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL)
149 err(1, NULL);
150
151 for (rval = 0; errno = 0, (p = fts_read(ftsp)) != NULL;) {
152 int atflag;
153
154 if ((fts_options & FTS_LOGICAL) ||
155 ((fts_options & FTS_COMFOLLOW) &&
156 p->fts_level == FTS_ROOTLEVEL))
157 atflag = 0;
158 else
159 atflag = AT_SYMLINK_NOFOLLOW;
160
161 switch (p->fts_info) {
162 case FTS_D: /* Change it at FTS_DP if we're recursive. */
163 if (!Rflag)
164 fts_set(ftsp, p, FTS_SKIP);
165 continue;
166 case FTS_DNR: /* Warn, chflags. */
167 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
168 rval = 1;
169 break;
170 case FTS_ERR: /* Warn, continue. */
171 case FTS_NS:
172 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
173 rval = 1;
174 continue;
175 default:
176 break;
177 }
178 if (oct)
179 newflags = set;
180 else
181 newflags = (p->fts_statp->st_flags | set) & clear;
182 if (newflags == p->fts_statp->st_flags)
183 continue;
184 if (chflagsat(AT_FDCWD, p->fts_accpath, newflags,
185 atflag) == -1) {
186 e = errno;
187 if (!fflag) {
188 warnc(e, "%s", p->fts_path);
189 rval = 1;
190 }
191 if (siginfo) {
192 (void)printf("%s: %s\n", p->fts_path,
193 strerror(e));
194 siginfo = 0;
195 }
196 } else if (vflag || siginfo) {
197 (void)printf("%s", p->fts_path);
198 if (vflag > 1 || siginfo)
199 (void)printf(": 0%lo -> 0%lo",
200 (u_long)p->fts_statp->st_flags,
201 newflags);
202 (void)printf("\n");
203 siginfo = 0;
204 }
205 }
206 if (errno)
207 err(1, "fts_read");
208 exit(rval);
209 }
210
211 static void
usage(void)212 usage(void)
213 {
214 (void)fprintf(stderr,
215 "usage: chflags [-fhvx] [-R [-H | -L | -P]] flags file ...\n");
216 exit(1);
217 }
218