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