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