xref: /freebsd/bin/chmod/chmod.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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
35 static 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
42 static char sccsid[] = "@(#)chmod.c	8.8 (Berkeley) 4/1/94";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
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 
60 void usage __P((void));
61 
62 int
63 main(argc, argv)
64 	int argc;
65 	char *argv[];
66 {
67 	FTS *ftsp;
68 	FTSENT *p;
69 	mode_t *set;
70 	long val;
71 	int oct, omode;
72 	int Hflag, Lflag, Pflag, Rflag, ch, fflag, fts_options, hflag, rval;
73 	int vflag;
74 	char *ep, *mode;
75 
76 	set = NULL;
77 	omode = 0;
78 	Hflag = Lflag = Pflag = Rflag = fflag = hflag = vflag = 0;
79 	while ((ch = getopt(argc, argv, "HLPRXfgorstuvwx")) != -1)
80 		switch (ch) {
81 		case 'H':
82 			Hflag = 1;
83 			Lflag = Pflag = 0;
84 			break;
85 		case 'L':
86 			Lflag = 1;
87 			Hflag = Pflag = 0;
88 			break;
89 		case 'P':
90 			Pflag = 1;
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 don't have modes,
104 			 * so it's an undocumented noop.  Do syntax checking,
105 			 * though.
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 = 1;
124 			break;
125 		case '?':
126 		default:
127 			usage();
128 		}
129 done:	argv += optind;
130 	argc -= optind;
131 
132 	if (argc < 2)
133 		usage();
134 
135 	fts_options = FTS_PHYSICAL;
136 	if (Rflag) {
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 	}
147 
148 	mode = *argv;
149 	if (*mode >= '0' && *mode <= '7') {
150 		errno = 0;
151 		val = strtol(mode, &ep, 8);
152 		if (val > INT_MAX || val < 0)
153 			errno = ERANGE;
154 		if (errno)
155 			err(1, "invalid file mode: %s", mode);
156 		if (*ep)
157 			errx(1, "invalid file mode: %s", mode);
158 		omode = val;
159 		oct = 1;
160 	} else {
161 		if ((set = setmode(mode)) == NULL)
162 			errx(1, "invalid file mode: %s", mode);
163 		oct = 0;
164 	}
165 
166 	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
167 		err(1, NULL);
168 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
169 		switch (p->fts_info) {
170 		case FTS_D:			/* Change it at FTS_DP. */
171 			if (!Rflag)
172 				fts_set(ftsp, p, FTS_SKIP);
173 			continue;
174 		case FTS_DNR:			/* Warn, chmod, continue. */
175 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
176 			rval = 1;
177 			break;
178 		case FTS_ERR:			/* Warn, continue. */
179 		case FTS_NS:
180 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
181 			rval = 1;
182 			continue;
183 		case FTS_SL:			/* Ignore. */
184 		case FTS_SLNONE:
185 			/*
186 			 * The only symlinks that end up here are ones that
187 			 * don't point to anything and ones that we found
188 			 * doing a physical walk.
189 			 */
190 			continue;
191 		default:
192 			break;
193 		}
194 		if (chmod(p->fts_accpath, oct ? omode :
195 		    getmode(set, p->fts_statp->st_mode)) && !fflag) {
196 			warn(p->fts_path);
197 			rval = 1;
198 		} else {
199 		    	if (vflag)
200 				(void)printf("%s\n", p->fts_accpath);
201 		}
202 	}
203 	if (errno)
204 		err(1, "fts_read");
205 	free(set);
206 	exit(rval);
207 }
208 
209 void
210 usage()
211 {
212 	(void)fprintf(stderr,
213 	    "usage: chmod [-fv] [-R [-H | -L | -P]] mode file ...\n");
214 	exit(1);
215 }
216