xref: /netbsd-src/bin/chmod/chmod.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: chmod.c,v 1.17 1997/10/11 03:11:04 enami Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT(
39 "@(#) Copyright (c) 1989, 1993, 1994\n\
40 	The Regents of the University of California.  All rights reserved.\n");
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)chmod.c	8.8 (Berkeley) 4/1/94";
46 #else
47 __RCSID("$NetBSD: chmod.c,v 1.17 1997/10/11 03:11:04 enami Exp $");
48 #endif
49 #endif /* not lint */
50 
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 
54 #include <err.h>
55 #include <errno.h>
56 #include <fts.h>
57 #include <locale.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <limits.h>
63 
64 int main __P((int, char *[]));
65 void usage __P((void));
66 
67 int
68 main(argc, argv)
69 	int argc;
70 	char *argv[];
71 {
72 	FTS *ftsp;
73 	FTSENT *p;
74 	mode_t *set;
75 	long val;
76 	int oct, omode;
77 	int Hflag, Lflag, Pflag, Rflag, ch, fflag, fts_options, hflag, rval;
78 	char *ep, *mode;
79 	int (*change_mode) __P((const char *, mode_t));
80 
81 	set = NULL;	/* XXX gcc -Wuninitialized */
82 	omode = 0;	/* XXX gcc -Wuninitialized */
83 
84 	setlocale(LC_ALL, "");
85 
86 	Hflag = Lflag = Pflag = Rflag = fflag = hflag = 0;
87 	while ((ch = getopt(argc, argv, "HLPRXfghorstuwx")) != -1)
88 		switch (ch) {
89 		case 'H':
90 			Hflag = 1;
91 			Lflag = Pflag = 0;
92 			break;
93 		case 'L':
94 			Lflag = 1;
95 			Hflag = Pflag = 0;
96 			break;
97 		case 'P':
98 			Pflag = 1;
99 			Hflag = Lflag = 0;
100 			break;
101 		case 'R':
102 			Rflag = 1;
103 			break;
104 		case 'f':		/* XXX: undocumented. */
105 			fflag = 1;
106 			break;
107 		case 'h':
108 			/*
109 			 * In System V (and probably POSIX.2) the -h option
110 			 * causes chmod to change the mode of the symbolic
111 			 * link.  4.4BSD's symbolic links didn't have modes,
112 			 * so it was an undocumented noop.  In NetBSD 1.3,
113 			 * lchmod(2) is introduced and this option does real
114 			 * work.
115 			 */
116 			hflag = 1;
117 			break;
118 		/*
119 		 * XXX
120 		 * "-[rwx]" are valid mode commands.  If they are the entire
121 		 * argument, getopt has moved past them, so decrement optind.
122 		 * Regardless, we're done argument processing.
123 		 */
124 		case 'g': case 'o': case 'r': case 's':
125 		case 't': case 'u': case 'w': case 'X': case 'x':
126 			if (argv[optind - 1][0] == '-' &&
127 			    argv[optind - 1][1] == ch &&
128 			    argv[optind - 1][2] == '\0')
129 				--optind;
130 			goto done;
131 		case '?':
132 		default:
133 			usage();
134 		}
135 done:	argv += optind;
136 	argc -= optind;
137 
138 	if (argc < 2)
139 		usage();
140 
141 	fts_options = FTS_PHYSICAL;
142 	if (Rflag) {
143 		if (hflag)
144 			errx(1,
145 		"the -R and -h options may not be specified together.");
146 		if (Hflag)
147 			fts_options |= FTS_COMFOLLOW;
148 		if (Lflag) {
149 			fts_options &= ~FTS_PHYSICAL;
150 			fts_options |= FTS_LOGICAL;
151 		}
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, argv[0]);
178 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
179 		switch (p->fts_info) {
180 		case FTS_D:
181 			if (!Rflag)
182 				fts_set(ftsp, p, FTS_SKIP);
183 			break;
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_DP:			/* Already changed at FTS_D. */
189 			continue;
190 		case FTS_ERR:			/* Warn, continue. */
191 		case FTS_NS:
192 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
193 			rval = 1;
194 			continue;
195 		case FTS_SL:			/* Ignore. */
196 		case FTS_SLNONE:
197 			/*
198 			 * The only symlinks that end up here are ones that
199 			 * don't point to anything and ones that we found
200 			 * doing a physical walk.
201 			 */
202 			if (!hflag)
203 				continue;
204 			/* else */
205 			/* FALLTHROUGH */
206 		default:
207 			break;
208 		}
209 		if ((*change_mode)(p->fts_accpath, oct ? omode :
210 		    getmode(set, p->fts_statp->st_mode)) && !fflag) {
211 			warn("%s", p->fts_path);
212 			rval = 1;
213 		}
214 	}
215 	if (errno)
216 		err(1, "fts_read");
217 	exit(rval);
218 }
219 
220 void
221 usage()
222 {
223 	(void)fprintf(stderr,
224 	    "usage: chmod [-R [-H | -L | -P]] [-h] mode file ...\n");
225 	exit(1);
226 }
227