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