xref: /freebsd/bin/chflags/chflags.c (revision 90aea514c6249118e880d75972d063362f4bf492)
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 #if 0
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1992, 1993, 1994\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif
38 
39 #endif
40 
41 #include <sys/cdefs.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <fts.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 static volatile sig_atomic_t siginfo;
56 
57 static void usage(void) __dead2;
58 
59 static void
60 siginfo_handler(int sig __unused)
61 {
62 
63 	siginfo = 1;
64 }
65 
66 int
67 main(int argc, char *argv[])
68 {
69 	FTS *ftsp;
70 	FTSENT *p;
71 	u_long clear, newflags, set;
72 	long val;
73 	int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
74 	int ch, fts_options, oct, rval;
75 	char *flags, *ep;
76 
77 	Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0;
78 	while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1)
79 		switch (ch) {
80 		case 'H':
81 			Hflag = 1;
82 			Lflag = 0;
83 			break;
84 		case 'L':
85 			Lflag = 1;
86 			Hflag = 0;
87 			break;
88 		case 'P':
89 			Hflag = Lflag = 0;
90 			break;
91 		case 'R':
92 			Rflag = 1;
93 			break;
94 		case 'f':
95 			fflag = 1;
96 			break;
97 		case 'h':
98 			hflag = 1;
99 			break;
100 		case 'v':
101 			vflag++;
102 			break;
103 		case 'x':
104 			xflag = 1;
105 			break;
106 		case '?':
107 		default:
108 			usage();
109 		}
110 	argv += optind;
111 	argc -= optind;
112 
113 	if (argc < 2)
114 		usage();
115 
116 	(void)signal(SIGINFO, siginfo_handler);
117 
118 	if (Rflag) {
119 		if (hflag)
120 			errx(1, "the -R and -h options may not be "
121 			    "specified together.");
122 		if (Lflag) {
123 			fts_options = FTS_LOGICAL;
124 		} else {
125 			fts_options = FTS_PHYSICAL;
126 
127 			if (Hflag) {
128 				fts_options |= FTS_COMFOLLOW;
129 			}
130 		}
131 	} else if (hflag) {
132 		fts_options = FTS_PHYSICAL;
133 	} else {
134 		fts_options = FTS_LOGICAL;
135 	}
136 	if (xflag)
137 		fts_options |= FTS_XDEV;
138 
139 	flags = *argv;
140 	if (*flags >= '0' && *flags <= '7') {
141 		errno = 0;
142 		val = strtol(flags, &ep, 8);
143 		if (val < 0)
144 			errno = ERANGE;
145 		if (errno)
146                         err(1, "invalid flags: %s", flags);
147                 if (*ep)
148                         errx(1, "invalid flags: %s", flags);
149 		set = val;
150                 oct = 1;
151 	} else {
152 		if (strtofflags(&flags, &set, &clear))
153                         errx(1, "invalid flag: %s", flags);
154 		clear = ~clear;
155 		oct = 0;
156 	}
157 
158 	if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL)
159 		err(1, NULL);
160 
161 	for (rval = 0; errno = 0, (p = fts_read(ftsp)) != NULL;) {
162 		int atflag;
163 
164 		if ((fts_options & FTS_LOGICAL) ||
165 		    ((fts_options & FTS_COMFOLLOW) &&
166 		    p->fts_level == FTS_ROOTLEVEL))
167 			atflag = 0;
168 		else
169 			atflag = AT_SYMLINK_NOFOLLOW;
170 
171 		switch (p->fts_info) {
172 		case FTS_D:	/* Change it at FTS_DP if we're recursive. */
173 			if (!Rflag)
174 				fts_set(ftsp, p, FTS_SKIP);
175 			continue;
176 		case FTS_DNR:			/* Warn, chflags. */
177 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
178 			rval = 1;
179 			break;
180 		case FTS_ERR:			/* Warn, continue. */
181 		case FTS_NS:
182 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
183 			rval = 1;
184 			continue;
185 		default:
186 			break;
187 		}
188 		if (oct)
189 			newflags = set;
190 		else
191 			newflags = (p->fts_statp->st_flags | set) & clear;
192 		if (newflags == p->fts_statp->st_flags)
193 			continue;
194 		if (chflagsat(AT_FDCWD, p->fts_accpath, newflags,
195 		    atflag) == -1 && !fflag) {
196 			warn("%s", p->fts_path);
197 			rval = 1;
198 		} else if (vflag || siginfo) {
199 			(void)printf("%s", p->fts_path);
200 			if (vflag > 1 || siginfo)
201 				(void)printf(": 0%lo -> 0%lo",
202 				    (u_long)p->fts_statp->st_flags,
203 				    newflags);
204 			(void)printf("\n");
205 			siginfo = 0;
206 		}
207 	}
208 	if (errno)
209 		err(1, "fts_read");
210 	exit(rval);
211 }
212 
213 static void
214 usage(void)
215 {
216 	(void)fprintf(stderr,
217 	    "usage: chflags [-fhvx] [-R [-H | -L | -P]] flags file ...\n");
218 	exit(1);
219 }
220