* Add this nice filesystem testing tool that I've recently
[dragonfly.git] / bin / chmod / chmod.c
1 /*
2  * Copyright (c) 1989, 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) 1989, 1993, 1994 The Regents of the University of California.  All rights reserved.
34  * @(#)chmod.c  8.8 (Berkeley) 4/1/94
35  * $FreeBSD: src/bin/chmod/chmod.c,v 1.16.2.6 2002/10/18 01:36:38 trhodes Exp $
36  * $DragonFly: src/bin/chmod/chmod.c,v 1.4 2003/09/28 14:39:13 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 <limits.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 int main (int, char **);
52 void usage (void);
53
54 int
55 main(int argc, char **argv)
56 {
57         FTS *ftsp;
58         FTSENT *p;
59         mode_t *set;
60         long val;
61         int oct, omode;
62         int Hflag, Lflag, Pflag, Rflag, ch, fflag, fts_options, hflag, rval;
63         int vflag;
64         char *ep, *mode;
65         int newmode;
66         int (*change_mode) (const char *, mode_t);
67
68         set = NULL;
69         omode = 0;
70         Hflag = Lflag = Pflag = Rflag = fflag = hflag = vflag = 0;
71         while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1)
72                 switch (ch) {
73                 case 'H':
74                         Hflag = 1;
75                         Lflag = Pflag = 0;
76                         break;
77                 case 'L':
78                         Lflag = 1;
79                         Hflag = Pflag = 0;
80                         break;
81                 case 'P':
82                         Pflag = 1;
83                         Hflag = Lflag = 0;
84                         break;
85                 case 'R':
86                         Rflag = 1;
87                         break;
88                 case 'f':
89                         fflag = 1;
90                         break;
91                 case 'h':
92                         /*
93                          * In System V (and probably POSIX.2) the -h option
94                          * causes chmod to change the mode of the symbolic
95                          * link.  4.4BSD's symbolic links didn't have modes,
96                          * so it was an undocumented noop.  In FreeBSD 3.0,
97                          * lchmod(2) is introduced and this option does real
98                          * work.
99                          */
100                         hflag = 1;
101                         break;
102                 /*
103                  * XXX
104                  * "-[rwx]" are valid mode commands.  If they are the entire
105                  * argument, getopt has moved past them, so decrement optind.
106                  * Regardless, we're done argument processing.
107                  */
108                 case 'g': case 'o': case 'r': case 's':
109                 case 't': case 'u': case 'w': case 'X': case 'x':
110                         if (argv[optind - 1][0] == '-' &&
111                             argv[optind - 1][1] == ch &&
112                             argv[optind - 1][2] == '\0')
113                                 --optind;
114                         goto done;
115                 case 'v':
116                         vflag = 1;
117                         break;
118                 case '?':
119                 default:
120                         usage();
121                 }
122 done:   argv += optind;
123         argc -= optind;
124
125         if (argc < 2)
126                 usage();
127
128         fts_options = FTS_PHYSICAL;
129         if (Rflag) {
130                 if (hflag)
131                         errx(1,
132                 "the -R and -h options may not be specified together.");
133                 if (Hflag)
134                         fts_options |= FTS_COMFOLLOW;
135                 if (Lflag) {
136                         fts_options &= ~FTS_PHYSICAL;
137                         fts_options |= FTS_LOGICAL;
138                 }
139         }
140
141         if (hflag)
142                 change_mode = lchmod;
143         else
144                 change_mode = chmod;
145
146         mode = *argv;
147         if (*mode >= '0' && *mode <= '7') {
148                 errno = 0;
149                 val = strtol(mode, &ep, 8);
150                 if (val > INT_MAX || val < 0)
151                         errno = ERANGE;
152                 if (errno)
153                         err(1, "invalid file mode: %s", mode);
154                 if (*ep)
155                         errx(1, "invalid file mode: %s", mode);
156                 omode = val;
157                 oct = 1;
158         } else {
159                 if ((set = setmode(mode)) == NULL)
160                         errx(1, "invalid file mode: %s", mode);
161                 oct = 0;
162         }
163
164         if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
165                 err(1, NULL);
166         for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
167                 switch (p->fts_info) {
168                 case FTS_D:                     /* Change it at FTS_DP. */
169                         if (!Rflag)
170                                 fts_set(ftsp, p, FTS_SKIP);
171                         continue;
172                 case FTS_DNR:                   /* Warn, chmod, continue. */
173                         warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
174                         rval = 1;
175                         break;
176                 case FTS_ERR:                   /* Warn, continue. */
177                 case FTS_NS:
178                         warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
179                         rval = 1;
180                         continue;
181                 case FTS_SL:                    /* Ignore. */
182                 case FTS_SLNONE:
183                         /*
184                          * The only symlinks that end up here are ones that
185                          * don't point to anything and ones that we found
186                          * doing a physical walk.
187                          */
188                         if (!hflag)
189                                 continue;
190                         /* else */
191                         /* FALLTHROUGH */
192                 default:
193                         break;
194                 }
195                 newmode = oct ? omode : getmode(set, p->fts_statp->st_mode);
196                 if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
197                         continue;
198                 if ((*change_mode)(p->fts_accpath, newmode) && !fflag) {
199                         warn("%s", p->fts_path);
200                         rval = 1;
201                 } else {
202                         if (vflag)
203                                 (void)printf("%s\n", p->fts_accpath);
204                 }
205         }
206         if (errno)
207                 err(1, "fts_read");
208         free(set);
209         exit(rval);
210 }
211
212 void
213 usage()
214 {
215         (void)fprintf(stderr,
216             "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
217         exit(1);
218 }