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