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