Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:29:25 dillon 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 __P((int, char *[]));
51 void    usage __P((void));
52
53 int
54 main(argc, argv)
55         int argc;
56         char *argv[];
57 {
58         FTS *ftsp;
59         FTSENT *p;
60         u_long clear, set;
61         long val;
62         int Hflag, Lflag, Pflag, Rflag, ch, fts_options, oct, rval;
63         char *flags, *ep;
64
65         Hflag = Lflag = Pflag = Rflag = 0;
66         while ((ch = getopt(argc, argv, "HLPR")) != -1)
67                 switch (ch) {
68                 case 'H':
69                         Hflag = 1;
70                         Lflag = Pflag = 0;
71                         break;
72                 case 'L':
73                         Lflag = 1;
74                         Hflag = Pflag = 0;
75                         break;
76                 case 'P':
77                         Pflag = 1;
78                         Hflag = Lflag = 0;
79                         break;
80                 case 'R':
81                         Rflag = 1;
82                         break;
83                 case '?':
84                 default:
85                         usage();
86                 }
87         argv += optind;
88         argc -= optind;
89
90         if (argc < 2)
91                 usage();
92
93         fts_options = FTS_PHYSICAL;
94         if (Rflag) {
95                 if (Hflag)
96                         fts_options |= FTS_COMFOLLOW;
97                 if (Lflag) {
98                         fts_options &= ~FTS_PHYSICAL;
99                         fts_options |= FTS_LOGICAL;
100                 }
101         }
102
103         flags = *argv;
104         if (*flags >= '0' && *flags <= '7') {
105                 errno = 0;
106                 val = strtol(flags, &ep, 8);
107                 if (val < 0)
108                         errno = ERANGE;
109                 if (errno)
110                         err(1, "invalid flags: %s", flags);
111                 if (*ep)
112                         errx(1, "invalid flags: %s", flags);
113                 set = val;
114                 oct = 1;
115         } else {
116                 if (strtofflags(&flags, &set, &clear))
117                         errx(1, "invalid flag: %s", flags);
118                 clear = ~clear;
119                 oct = 0;
120         }
121
122         if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL)
123                 err(1, NULL);
124
125         for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
126                 switch (p->fts_info) {
127                 case FTS_D:
128                         if (Rflag)              /* Change it at FTS_DP. */
129                                 continue;
130                         fts_set(ftsp, p, FTS_SKIP);
131                         break;
132                 case FTS_DNR:                   /* Warn, chflag, continue. */
133                         warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
134                         rval = 1;
135                         break;
136                 case FTS_ERR:                   /* Warn, continue. */
137                 case FTS_NS:
138                         warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
139                         rval = 1;
140                         continue;
141                 case FTS_SL:                    /* Ignore. */
142                 case FTS_SLNONE:
143                         /*
144                          * The only symlinks that end up here are ones that
145                          * don't point to anything and ones that we found
146                          * doing a physical walk.
147                          */
148                         continue;
149                 default:
150                         break;
151                 }
152                 if (oct) {
153                         if (!chflags(p->fts_accpath, set))
154                                 continue;
155                 } else {
156                         p->fts_statp->st_flags |= set;
157                         p->fts_statp->st_flags &= clear;
158                         if (!chflags(p->fts_accpath, (u_long)p->fts_statp->st_flags))
159                                 continue;
160                 }
161                 warn("%s", p->fts_path);
162                 rval = 1;
163         }
164         if (errno)
165                 err(1, "fts_read");
166         exit(rval);
167 }
168
169 void
170 usage()
171 {
172         (void)fprintf(stderr,
173             "usage: chflags [-R [-H | -L | -P]] flags file ...\n");
174         exit(1);
175 }