xref: /freebsd/bin/chflags/chflags.c (revision 6fd05b64b5b65dd4ba9b86482a0634a5f0b96c29)
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, Pflag, Rflag, ch, fts_options, oct, rval;
66 	char *flags, *ep;
67 
68 	Hflag = Lflag = Pflag = Rflag = 0;
69 	while ((ch = getopt(argc, argv, "HLPR")) != -1)
70 		switch (ch) {
71 		case 'H':
72 			Hflag = 1;
73 			Lflag = Pflag = 0;
74 			break;
75 		case 'L':
76 			Lflag = 1;
77 			Hflag = Pflag = 0;
78 			break;
79 		case 'P':
80 			Pflag = 1;
81 			Hflag = Lflag = 0;
82 			break;
83 		case 'R':
84 			Rflag = 1;
85 			break;
86 		case '?':
87 		default:
88 			usage();
89 		}
90 	argv += optind;
91 	argc -= optind;
92 
93 	if (argc < 2)
94 		usage();
95 
96 	if (Rflag) {
97 		fts_options = FTS_PHYSICAL;
98 		if (Hflag)
99 			fts_options |= FTS_COMFOLLOW;
100 		if (Lflag) {
101 			fts_options &= ~FTS_PHYSICAL;
102 			fts_options |= FTS_LOGICAL;
103 		}
104 	} else
105 		fts_options = FTS_LOGICAL;
106 
107 	flags = *argv;
108 	if (*flags >= '0' && *flags <= '7') {
109 		errno = 0;
110 		val = strtol(flags, &ep, 8);
111 		if (val < 0)
112 			errno = ERANGE;
113 		if (errno)
114                         err(1, "invalid flags: %s", flags);
115                 if (*ep)
116                         errx(1, "invalid flags: %s", flags);
117 		set = val;
118                 oct = 1;
119 	} else {
120 		if (strtofflags(&flags, &set, &clear))
121                         errx(1, "invalid flag: %s", flags);
122 		clear = ~clear;
123 		oct = 0;
124 	}
125 
126 	if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL)
127 		err(1, NULL);
128 
129 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
130 		switch (p->fts_info) {
131 		case FTS_D:	/* Change it at FTS_DP if we're recursive. */
132 			if (!Rflag)
133 				fts_set(ftsp, p, FTS_SKIP);
134 			continue;
135 		case FTS_DNR:			/* Warn, chflag, continue. */
136 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
137 			rval = 1;
138 			break;
139 		case FTS_ERR:			/* Warn, continue. */
140 		case FTS_NS:
141 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
142 			rval = 1;
143 			continue;
144 		case FTS_SL:			/* Ignore. */
145 		case FTS_SLNONE:
146 			/*
147 			 * The only symlinks that end up here are ones that
148 			 * don't point to anything and ones that we found
149 			 * doing a physical walk.
150 			 */
151 			continue;
152 		default:
153 			break;
154 		}
155 		if (oct) {
156 			if (!chflags(p->fts_accpath, set))
157 				continue;
158 		} else {
159 			p->fts_statp->st_flags |= set;
160 			p->fts_statp->st_flags &= clear;
161 			if (!chflags(p->fts_accpath, (u_long)p->fts_statp->st_flags))
162 				continue;
163 		}
164 		warn("%s", p->fts_path);
165 		rval = 1;
166 	}
167 	if (errno)
168 		err(1, "fts_read");
169 	exit(rval);
170 }
171 
172 void
173 usage(void)
174 {
175 	(void)fprintf(stderr,
176 	    "usage: chflags [-R [-H | -L | -P]] flags file ...\n");
177 	exit(1);
178 }
179