xref: /freebsd/bin/chflags/chflags.c (revision ae83180158c4c937f170e31eff311b18c0286a93)
1 /*
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  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 static const char copyright[] =
36 "@(#) Copyright (c) 1992, 1993, 1994\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif
39 
40 #if 0
41 #ifndef lint
42 static char sccsid[] = "@(#)chflags.c	8.5 (Berkeley) 4/1/94";
43 #endif
44 #endif
45 
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48 
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 
52 #include <err.h>
53 #include <errno.h>
54 #include <fts.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 int	main(int, char *[]);
61 void	usage(void);
62 
63 int
64 main(argc, argv)
65 	int argc;
66 	char *argv[];
67 {
68 	FTS *ftsp;
69 	FTSENT *p;
70 	u_long clear, set;
71 	long val;
72 	int Hflag, Lflag, Pflag, Rflag, ch, fts_options, oct, rval;
73 	char *flags, *ep;
74 
75 	Hflag = Lflag = Pflag = Rflag = 0;
76 	while ((ch = getopt(argc, argv, "HLPR")) != -1)
77 		switch (ch) {
78 		case 'H':
79 			Hflag = 1;
80 			Lflag = Pflag = 0;
81 			break;
82 		case 'L':
83 			Lflag = 1;
84 			Hflag = Pflag = 0;
85 			break;
86 		case 'P':
87 			Pflag = 1;
88 			Hflag = Lflag = 0;
89 			break;
90 		case 'R':
91 			Rflag = 1;
92 			break;
93 		case '?':
94 		default:
95 			usage();
96 		}
97 	argv += optind;
98 	argc -= optind;
99 
100 	if (argc < 2)
101 		usage();
102 
103 	if (Rflag) {
104 		fts_options = FTS_PHYSICAL;
105 		if (Hflag)
106 			fts_options |= FTS_COMFOLLOW;
107 		if (Lflag) {
108 			fts_options &= ~FTS_PHYSICAL;
109 			fts_options |= FTS_LOGICAL;
110 		}
111 	} else
112 		fts_options = FTS_LOGICAL;
113 
114 	flags = *argv;
115 	if (*flags >= '0' && *flags <= '7') {
116 		errno = 0;
117 		val = strtol(flags, &ep, 8);
118 		if (val < 0)
119 			errno = ERANGE;
120 		if (errno)
121                         err(1, "invalid flags: %s", flags);
122                 if (*ep)
123                         errx(1, "invalid flags: %s", flags);
124 		set = val;
125                 oct = 1;
126 	} else {
127 		if (strtofflags(&flags, &set, &clear))
128                         errx(1, "invalid flag: %s", flags);
129 		clear = ~clear;
130 		oct = 0;
131 	}
132 
133 	if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL)
134 		err(1, NULL);
135 
136 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
137 		switch (p->fts_info) {
138 		case FTS_D:
139 			if (Rflag)		/* Change it at FTS_DP. */
140 				continue;
141 			fts_set(ftsp, p, FTS_SKIP);
142 			break;
143 		case FTS_DNR:			/* Warn, chflag, continue. */
144 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
145 			rval = 1;
146 			break;
147 		case FTS_ERR:			/* Warn, continue. */
148 		case FTS_NS:
149 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
150 			rval = 1;
151 			continue;
152 		case FTS_SL:			/* Ignore. */
153 		case FTS_SLNONE:
154 			/*
155 			 * The only symlinks that end up here are ones that
156 			 * don't point to anything and ones that we found
157 			 * doing a physical walk.
158 			 */
159 			continue;
160 		default:
161 			break;
162 		}
163 		if (oct) {
164 			if (!chflags(p->fts_accpath, set))
165 				continue;
166 		} else {
167 			p->fts_statp->st_flags |= set;
168 			p->fts_statp->st_flags &= clear;
169 			if (!chflags(p->fts_accpath, (u_long)p->fts_statp->st_flags))
170 				continue;
171 		}
172 		warn("%s", p->fts_path);
173 		rval = 1;
174 	}
175 	if (errno)
176 		err(1, "fts_read");
177 	exit(rval);
178 }
179 
180 void
181 usage()
182 {
183 	(void)fprintf(stderr,
184 	    "usage: chflags [-R [-H | -L | -P]] flags file ...\n");
185 	exit(1);
186 }
187