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