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