xref: /freebsd/bin/chflags/chflags.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if 0
31 #ifndef lint
32 static const char copyright[] =
33 "@(#) Copyright (c) 1992, 1993, 1994\n\
34 	The Regents of the University of California.  All rights reserved.\n";
35 #endif
36 
37 #ifndef lint
38 static char sccsid[] = "@(#)chflags.c	8.5 (Berkeley) 4/1/94";
39 #endif
40 #endif
41 
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44 
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 
48 #include <err.h>
49 #include <errno.h>
50 #include <fts.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 void	usage(void);
57 
58 int
59 main(int argc, char *argv[])
60 {
61 	FTS *ftsp;
62 	FTSENT *p;
63 	u_long clear, set;
64 	long val;
65 	int Hflag, Lflag, Rflag, ch, fts_options, oct, rval;
66 	char *flags, *ep;
67 
68 	Hflag = Lflag = Rflag = 0;
69 	while ((ch = getopt(argc, argv, "HLPR")) != -1)
70 		switch (ch) {
71 		case 'H':
72 			Hflag = 1;
73 			Lflag = 0;
74 			break;
75 		case 'L':
76 			Lflag = 1;
77 			Hflag = 0;
78 			break;
79 		case 'P':
80 			Hflag = Lflag = 0;
81 			break;
82 		case 'R':
83 			Rflag = 1;
84 			break;
85 		case '?':
86 		default:
87 			usage();
88 		}
89 	argv += optind;
90 	argc -= optind;
91 
92 	if (argc < 2)
93 		usage();
94 
95 	if (Rflag) {
96 		fts_options = FTS_PHYSICAL;
97 		if (Hflag)
98 			fts_options |= FTS_COMFOLLOW;
99 		if (Lflag) {
100 			fts_options &= ~FTS_PHYSICAL;
101 			fts_options |= FTS_LOGICAL;
102 		}
103 	} else
104 		fts_options = FTS_LOGICAL;
105 
106 	flags = *argv;
107 	if (*flags >= '0' && *flags <= '7') {
108 		errno = 0;
109 		val = strtol(flags, &ep, 8);
110 		if (val < 0)
111 			errno = ERANGE;
112 		if (errno)
113                         err(1, "invalid flags: %s", flags);
114                 if (*ep)
115                         errx(1, "invalid flags: %s", flags);
116 		set = val;
117                 oct = 1;
118 	} else {
119 		if (strtofflags(&flags, &set, &clear))
120                         errx(1, "invalid flag: %s", flags);
121 		clear = ~clear;
122 		oct = 0;
123 	}
124 
125 	if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL)
126 		err(1, NULL);
127 
128 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
129 		switch (p->fts_info) {
130 		case FTS_D:	/* Change it at FTS_DP if we're recursive. */
131 			if (!Rflag)
132 				fts_set(ftsp, p, FTS_SKIP);
133 			continue;
134 		case FTS_DNR:			/* Warn, chflag, continue. */
135 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
136 			rval = 1;
137 			break;
138 		case FTS_ERR:			/* Warn, continue. */
139 		case FTS_NS:
140 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
141 			rval = 1;
142 			continue;
143 		case FTS_SL:			/* Ignore. */
144 		case FTS_SLNONE:
145 			/*
146 			 * The only symlinks that end up here are ones that
147 			 * don't point to anything and ones that we found
148 			 * doing a physical walk.
149 			 */
150 			continue;
151 		default:
152 			break;
153 		}
154 		if (oct) {
155 			if (!chflags(p->fts_accpath, set))
156 				continue;
157 		} else {
158 			p->fts_statp->st_flags |= set;
159 			p->fts_statp->st_flags &= clear;
160 			if (!chflags(p->fts_accpath, (u_long)p->fts_statp->st_flags))
161 				continue;
162 		}
163 		warn("%s", p->fts_path);
164 		rval = 1;
165 	}
166 	if (errno)
167 		err(1, "fts_read");
168 	exit(rval);
169 }
170 
171 void
172 usage(void)
173 {
174 	(void)fprintf(stderr,
175 	    "usage: chflags [-R [-H | -L | -P]] flags file ...\n");
176 	exit(1);
177 }
178