inpcb: Use netisr_ncpus for listing inpcbs.
[dragonfly.git] / usr.bin / chflags / chflags.c
blobf1c9e99c9758c9090b9c8dd5e8bb95c1af4f9f15
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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 * 3. 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.
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.
29 * @(#) Copyright (c) 1992, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)chflags.c 8.5 (Berkeley) 4/1/94
31 * $FreeBSD: head/bin/chflags/chflags.c 282208 2015-04-29 00:49:00Z smh $
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <fts.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
47 static void usage(void);
49 int
50 main(int argc, char *argv[])
52 FTS *ftsp;
53 FTSENT *p;
54 u_long clear, newflags, set;
55 long val;
56 int Hflag, Lflag, Rflag, fflag, hflag, vflag;
57 int ch, fts_options, oct, rval;
58 char *flags, *ep;
60 Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
61 while ((ch = getopt(argc, argv, "HLPRfhv")) != -1)
62 switch (ch) {
63 case 'H':
64 Hflag = 1;
65 Lflag = 0;
66 break;
67 case 'L':
68 Lflag = 1;
69 Hflag = 0;
70 break;
71 case 'P':
72 Hflag = Lflag = 0;
73 break;
74 case 'R':
75 Rflag = 1;
76 break;
77 case 'f':
78 fflag = 1;
79 break;
80 case 'h':
81 hflag = 1;
82 break;
83 case 'v':
84 vflag++;
85 break;
86 case '?':
87 default:
88 usage();
90 argv += optind;
91 argc -= optind;
93 if (argc < 2)
94 usage();
96 if (Rflag) {
97 if (hflag)
98 errx(1, "the -R and -h options may not be "
99 "specified together.");
100 if (Lflag) {
101 fts_options = FTS_LOGICAL;
102 } else {
103 fts_options = FTS_PHYSICAL;
105 if (Hflag) {
106 fts_options |= FTS_COMFOLLOW;
109 } else if (hflag) {
110 fts_options = FTS_PHYSICAL;
111 } else {
112 fts_options = FTS_LOGICAL;
115 flags = *argv;
116 if (*flags >= '0' && *flags <= '7') {
117 errno = 0;
118 val = strtol(flags, &ep, 8);
119 if (val < 0)
120 errno = ERANGE;
121 if (errno)
122 err(1, "invalid flags: %s", flags);
123 if (*ep)
124 errx(1, "invalid flags: %s", flags);
125 set = val;
126 oct = 1;
127 } else {
128 if (strtofflags(&flags, &set, &clear))
129 errx(1, "invalid flag: %s", flags);
130 clear = ~clear;
131 oct = 0;
134 if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL)
135 err(1, NULL);
137 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
138 int atflag;
140 if ((fts_options & FTS_LOGICAL) ||
141 ((fts_options & FTS_COMFOLLOW) &&
142 p->fts_level == FTS_ROOTLEVEL))
143 atflag = 0;
144 else
145 atflag = AT_SYMLINK_NOFOLLOW;
147 switch (p->fts_info) {
148 case FTS_D: /* Change it at FTS_DP if we're recursive. */
149 if (!Rflag)
150 fts_set(ftsp, p, FTS_SKIP);
151 continue;
152 case FTS_DNR: /* Warn, chflags. */
153 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
154 rval = 1;
155 break;
156 case FTS_ERR: /* Warn, continue. */
157 case FTS_NS:
158 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
159 rval = 1;
160 continue;
161 default:
162 break;
164 if (oct)
165 newflags = set;
166 else
167 newflags = (p->fts_statp->st_flags | set) & clear;
168 if (newflags == p->fts_statp->st_flags)
169 continue;
170 if (chflagsat(AT_FDCWD, p->fts_accpath, newflags,
171 atflag) == -1 && !fflag) {
172 warn("%s", p->fts_path);
173 rval = 1;
174 } else if (vflag) {
175 printf("%s", p->fts_path);
176 if (vflag > 1)
177 printf(": 0%lo -> 0%lo",
178 (u_long)p->fts_statp->st_flags,
179 newflags);
180 printf("\n");
183 if (errno)
184 err(1, "fts_read");
185 exit(rval);
188 static void
189 usage(void)
191 fprintf(stderr,
192 "usage: chflags [-fhv] [-R [-H | -L | -P]] flags file ...\n");
193 exit(1);