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