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