xref: /freebsd/bin/chflags/chflags.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
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 __FBSDID("$FreeBSD$");
46 
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <fts.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 static void usage(void);
60 
61 int
62 main(int argc, char *argv[])
63 {
64 	FTS *ftsp;
65 	FTSENT *p;
66 	u_long clear, newflags, set;
67 	long val;
68 	int Hflag, Lflag, Rflag, fflag, hflag, vflag;
69 	int ch, fts_options, oct, rval;
70 	char *flags, *ep;
71 
72 	Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
73 	while ((ch = getopt(argc, argv, "HLPRfhv")) != -1)
74 		switch (ch) {
75 		case 'H':
76 			Hflag = 1;
77 			Lflag = 0;
78 			break;
79 		case 'L':
80 			Lflag = 1;
81 			Hflag = 0;
82 			break;
83 		case 'P':
84 			Hflag = Lflag = 0;
85 			break;
86 		case 'R':
87 			Rflag = 1;
88 			break;
89 		case 'f':
90 			fflag = 1;
91 			break;
92 		case 'h':
93 			hflag = 1;
94 			break;
95 		case 'v':
96 			vflag++;
97 			break;
98 		case '?':
99 		default:
100 			usage();
101 		}
102 	argv += optind;
103 	argc -= optind;
104 
105 	if (argc < 2)
106 		usage();
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 
127 	flags = *argv;
128 	if (*flags >= '0' && *flags <= '7') {
129 		errno = 0;
130 		val = strtol(flags, &ep, 8);
131 		if (val < 0)
132 			errno = ERANGE;
133 		if (errno)
134                         err(1, "invalid flags: %s", flags);
135                 if (*ep)
136                         errx(1, "invalid flags: %s", flags);
137 		set = val;
138                 oct = 1;
139 	} else {
140 		if (strtofflags(&flags, &set, &clear))
141                         errx(1, "invalid flag: %s", flags);
142 		clear = ~clear;
143 		oct = 0;
144 	}
145 
146 	if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL)
147 		err(1, NULL);
148 
149 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
150 		int atflag;
151 
152 		if ((fts_options & FTS_LOGICAL) ||
153 		    ((fts_options & FTS_COMFOLLOW) &&
154 		    p->fts_level == FTS_ROOTLEVEL))
155 			atflag = 0;
156 		else
157 			atflag = AT_SYMLINK_NOFOLLOW;
158 
159 		switch (p->fts_info) {
160 		case FTS_D:	/* Change it at FTS_DP if we're recursive. */
161 			if (!Rflag)
162 				fts_set(ftsp, p, FTS_SKIP);
163 			continue;
164 		case FTS_DNR:			/* Warn, chflags. */
165 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
166 			rval = 1;
167 			break;
168 		case FTS_ERR:			/* Warn, continue. */
169 		case FTS_NS:
170 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
171 			rval = 1;
172 			continue;
173 		default:
174 			break;
175 		}
176 		if (oct)
177 			newflags = set;
178 		else
179 			newflags = (p->fts_statp->st_flags | set) & clear;
180 		if (newflags == p->fts_statp->st_flags)
181 			continue;
182 		if (chflagsat(AT_FDCWD, p->fts_accpath, newflags,
183 		    atflag) == -1 && !fflag) {
184 			warn("%s", p->fts_path);
185 			rval = 1;
186 		} else if (vflag) {
187 			(void)printf("%s", p->fts_path);
188 			if (vflag > 1)
189 				(void)printf(": 0%lo -> 0%lo",
190 				    (u_long)p->fts_statp->st_flags,
191 				    newflags);
192 			(void)printf("\n");
193 		}
194 	}
195 	if (errno)
196 		err(1, "fts_read");
197 	exit(rval);
198 }
199 
200 static void
201 usage(void)
202 {
203 	(void)fprintf(stderr,
204 	    "usage: chflags [-fhv] [-R [-H | -L | -P]] flags file ...\n");
205 	exit(1);
206 }
207