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