xref: /dragonfly/bin/chmod/chmod.c (revision 9f7604d7)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California.  All rights reserved.
30  * @(#)chmod.c	8.8 (Berkeley) 4/1/94
31  * $FreeBSD: src/bin/chmod/chmod.c,v 1.16.2.6 2002/10/18 01:36:38 trhodes Exp $
32  * $DragonFly: src/bin/chmod/chmod.c,v 1.9 2008/09/02 22:13:11 swildner Exp $
33  */
34 
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <fts.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 static void usage (void);
48 
49 int
50 main(int argc, char **argv)
51 {
52 	FTS *ftsp;
53 	FTSENT *p;
54 	mode_t newmode;
55 	void *set;
56 	int Hflag, Lflag, Rflag, ch, fflag;
57 	int fts_options, hflag, rval, vflag;
58 	char *mode;
59 	int (*change_mode) (const char *, mode_t);
60 
61 	Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
62 	while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1)
63 		switch (ch) {
64 		case 'H':
65 			Hflag = 1;
66 			Lflag = 0;
67 			break;
68 		case 'L':
69 			Lflag = 1;
70 			Hflag = 0;
71 			break;
72 		case 'P':
73 			Hflag = Lflag = 0;
74 			break;
75 		case 'R':
76 			Rflag = 1;
77 			break;
78 		case 'f':
79 			fflag = 1;
80 			break;
81 		case 'h':
82 			/*
83 			 * In System V (and probably POSIX.2) the -h option
84 			 * causes chmod to change the mode of the symbolic
85 			 * link.  4.4BSD's symbolic links didn't have modes,
86 			 * so it was an undocumented noop.  In FreeBSD 3.0,
87 			 * lchmod(2) is introduced and this option does real
88 			 * work.
89 			 */
90 			hflag = 1;
91 			break;
92 		/*
93 		 * XXX
94 		 * "-[rwx]" are valid mode commands.  If they are the entire
95 		 * argument, getopt has moved past them, so decrement optind.
96 		 * Regardless, we're done argument processing.
97 		 */
98 		case 'g': case 'o': case 'r': case 's':
99 		case 't': case 'u': case 'w': case 'X': case 'x':
100 			if (argv[optind - 1][0] == '-' &&
101 			    argv[optind - 1][1] == ch &&
102 			    argv[optind - 1][2] == '\0')
103 				--optind;
104 			goto done;
105 		case 'v':
106 			vflag = 1;
107 			break;
108 		default:
109 			usage();
110 		}
111 done:	argv += optind;
112 	argc -= optind;
113 
114 	if (argc < 2)
115 		usage();
116 
117 	fts_options = FTS_PHYSICAL;
118 	if (Rflag) {
119 		if (hflag)
120 			errx(1,
121 		"the -R and -h options may not be specified together.");
122 		if (Hflag)
123 			fts_options |= FTS_COMFOLLOW;
124 		if (Lflag) {
125 			fts_options &= ~FTS_PHYSICAL;
126 			fts_options |= FTS_LOGICAL;
127 		}
128 	}
129 
130 	if (hflag)
131 		change_mode = lchmod;
132 	else
133 		change_mode = chmod;
134 
135 	mode = *argv;
136 	errno = 0;
137 	if ((set = setmode(mode)) == NULL) {
138 		if (!errno)
139 			errx(1, "invalid file mode: %s", mode);
140 		else
141 			/* malloc for setmode() failed */
142 			err(1, "setmode failed");
143 	}
144 
145 	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
146 		err(1, NULL);
147 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
148 		switch (p->fts_info) {
149 		case FTS_D:			/* Change it at FTS_DP. */
150 			if (!Rflag)
151 				fts_set(ftsp, p, FTS_SKIP);
152 			continue;
153 		case FTS_DNR:			/* Warn, chmod, continue. */
154 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
155 			rval = 1;
156 			break;
157 		case FTS_ERR:			/* Warn, continue. */
158 		case FTS_NS:
159 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
160 			rval = 1;
161 			continue;
162 		case FTS_SL:			/* Ignore. */
163 		case FTS_SLNONE:
164 			/*
165 			 * The only symlinks that end up here are ones that
166 			 * don't point to anything and ones that we found
167 			 * doing a physical walk.
168 			 */
169 			if (!hflag)
170 				continue;
171 			/* else */
172 			/* FALLTHROUGH */
173 		default:
174 			break;
175 		}
176 		newmode = getmode(set, p->fts_statp->st_mode);
177 		if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
178 			continue;
179 		if ((*change_mode)(p->fts_accpath, newmode) && !fflag) {
180 			warn("%s", p->fts_path);
181 			rval = 1;
182 		} else {
183 		    	if (vflag)
184 				printf("%s\n", p->fts_accpath);
185 		}
186 	}
187 	if (errno)
188 		err(1, "fts_read");
189 	free(set);
190 	exit(rval);
191 }
192 
193 static void
194 usage(void)
195 {
196 	fprintf(stderr,
197 	    "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
198 	exit(1);
199 }
200