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