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