Conditionalize the lchflags() call since chflags(1) is a bootstrap tool.
[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.7 2008/11/11 05:53:07 pavalos Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42
43 #include <err.h>
44 #include <errno.h>
45 #include <fts.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 static 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, Rflag, hflag, ch, fts_options, oct, rval;
61         char *flags, *ep;
62         int (*change_flags)(const char *, unsigned long);
63
64         Hflag = Lflag = Rflag = hflag = 0;
65         while ((ch = getopt(argc, argv, "HLPRh")) != -1)
66                 switch (ch) {
67                 case 'H':
68                         Hflag = 1;
69                         Lflag = 0;
70                         break;
71                 case 'L':
72                         Lflag = 1;
73                         Hflag = 0;
74                         break;
75                 case 'P':
76                         Hflag = Lflag = 0;
77                         break;
78                 case 'R':
79                         Rflag = 1;
80                         break;
81                 case 'h':
82                         hflag = 1;
83                         break;
84                 default:
85                         usage();
86                 }
87         argv += optind;
88         argc -= optind;
89
90         if (argc < 2)
91                 usage();
92
93         if (Rflag) {
94                 fts_options = FTS_PHYSICAL;
95                 if (hflag) {
96                         errx(1, "the -R and -h options "
97                                 "may not be specified together");
98                 }
99                 if (Hflag)
100                         fts_options |= FTS_COMFOLLOW;
101                 if (Lflag) {
102                         fts_options &= ~FTS_PHYSICAL;
103                         fts_options |= FTS_LOGICAL;
104                 }
105         } else
106                 fts_options = FTS_LOGICAL;
107
108 #if __DragonFly_version >= 200101
109         if (hflag)
110                 change_flags = lchflags;
111         else
112 #endif
113                 change_flags = chflags;
114
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;
132         }
133
134         if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL)
135                 err(1, NULL);
136
137         for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
138                 switch (p->fts_info) {
139                 case FTS_D:
140                         if (!Rflag)             /* Change it at FTS_DP if we're recursive. */   
141                                 fts_set(ftsp, p, FTS_SKIP);
142                         continue;
143                 case FTS_DNR:                   /* Warn, chflag, continue. */
144                         warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
145                         rval = 1;
146                         break;
147                 case FTS_ERR:                   /* Warn, continue. */
148                 case FTS_NS:
149                         warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
150                         rval = 1;
151                         continue;
152                 case FTS_SL:                    /* Ignore. */
153                 case FTS_SLNONE:
154                         /*
155                          * The only symlinks that end up here are ones that
156                          * don't point to anything and ones that we found
157                          * doing a physical walk.
158                          */
159                         if (!hflag)
160                                 continue;
161                         /* FALLTHROUGH */
162                 default:
163                         break;
164                 }
165                 if (oct) {
166                         if (!(*change_flags)(p->fts_accpath, set))
167                                 continue;
168                 } else {
169                         p->fts_statp->st_flags |= set;
170                         p->fts_statp->st_flags &= clear;
171                         if (!(*change_flags)(p->fts_accpath,
172                                     (u_long)p->fts_statp->st_flags))
173                                 continue;
174                 }
175                 warn("%s", p->fts_path);
176                 rval = 1;
177         }
178         if (errno)
179                 err(1, "fts_read");
180         exit(rval);
181 }
182
183 static void
184 usage(void)
185 {
186         fprintf(stderr,
187             "usage: chflags [-h] [-R [-H | -L | -P]] flags file ...\n");
188         exit(1);
189 }