xref: /netbsd-src/bin/chmod/chmod.c (revision da39777e71da2583f9f99e14ec631aa1110387b6)
1*da39777eSkre /* $NetBSD: chmod.c,v 1.39 2023/05/05 04:14:02 kre Exp $ */
249f0ad86Scgd 
361f28255Scgd /*
4a7a05335Smycroft  * Copyright (c) 1989, 1993, 1994
5a7a05335Smycroft  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15b5b29542Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
3285a7e987Sthorpej #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3485a7e987Sthorpej __COPYRIGHT(
352fe2731dSlukem "@(#) Copyright (c) 1989, 1993, 1994\
362fe2731dSlukem  The Regents of the University of California.  All rights reserved.");
3761f28255Scgd #endif /* not lint */
3861f28255Scgd 
3961f28255Scgd #ifndef lint
4049f0ad86Scgd #if 0
4149f0ad86Scgd static char sccsid[] = "@(#)chmod.c	8.8 (Berkeley) 4/1/94";
4249f0ad86Scgd #else
43*da39777eSkre __RCSID("$NetBSD: chmod.c,v 1.39 2023/05/05 04:14:02 kre Exp $");
4449f0ad86Scgd #endif
4561f28255Scgd #endif /* not lint */
4661f28255Scgd 
47458ed234Sjschauma #include <sys/param.h>
4861f28255Scgd #include <sys/stat.h>
49458ed234Sjschauma #include <sys/types.h>
50a7a05335Smycroft 
51a7a05335Smycroft #include <err.h>
5254bfcbbbSmycroft #include <errno.h>
5361f28255Scgd #include <fts.h>
54516b44deSwiz #include <limits.h>
5552a3f396Skleink #include <locale.h>
5661f28255Scgd #include <stdio.h>
5754bfcbbbSmycroft #include <stdlib.h>
5861f28255Scgd #include <string.h>
59a7a05335Smycroft #include <unistd.h>
60bc806954Schristos #include <getopt.h>
6161f28255Scgd 
625bb1ddccSjoerg __dead static void	usage(void);
6354bfcbbbSmycroft 
64bc806954Schristos struct option chmod_longopts[] = {
65bc806954Schristos 	{ "reference",		required_argument,	0,
66bc806954Schristos 						1 },
67bc806954Schristos 	{ NULL,			0,			0,
68bc806954Schristos 						0 },
69bc806954Schristos };
70bc806954Schristos 
7154bfcbbbSmycroft int
main(int argc,char * argv[])72516b44deSwiz main(int argc, char *argv[])
7361f28255Scgd {
74a7a05335Smycroft 	FTS *ftsp;
75a7a05335Smycroft 	FTSENT *p;
76bc806954Schristos 	void *set;
77*da39777eSkre 	mode_t mval, nval;
78*da39777eSkre 	int Hflag, Lflag, Rflag, ch, dflag, fflag, fts_options, hflag, rval;
79bc806954Schristos 	char *mode, *reference;
80516b44deSwiz 	int (*change_mode)(const char *, mode_t);
8161f28255Scgd 
82516b44deSwiz 	setprogname(argv[0]);
8329bf463dSmycroft 	(void)setlocale(LC_ALL, "");
8452a3f396Skleink 
85*da39777eSkre 	Hflag = Lflag = Rflag = dflag = fflag = hflag = 0;
86bc806954Schristos 	reference = NULL;
87*da39777eSkre 	while ((ch = getopt_long(argc, argv, "HLPRXdfghorstuwx",
88bc806954Schristos 	    chmod_longopts, NULL)) != -1)
89a7a05335Smycroft 		switch (ch) {
90bc806954Schristos 		case 1:
91bc806954Schristos 			reference = optarg;
92bc806954Schristos 			break;
93a7a05335Smycroft 		case 'H':
94a7a05335Smycroft 			Hflag = 1;
9529bf463dSmycroft 			Lflag = 0;
9661f28255Scgd 			break;
97a7a05335Smycroft 		case 'L':
98a7a05335Smycroft 			Lflag = 1;
9929bf463dSmycroft 			Hflag = 0;
100a7a05335Smycroft 			break;
101a7a05335Smycroft 		case 'P':
102a7a05335Smycroft 			Hflag = Lflag = 0;
103a7a05335Smycroft 			break;
104a7a05335Smycroft 		case 'R':
105a7a05335Smycroft 			Rflag = 1;
106a7a05335Smycroft 			break;
107*da39777eSkre 		case 'd':
108*da39777eSkre 			dflag = 1;
109*da39777eSkre 			break;
110f3bbc67dSsnj 		case 'f':
11161f28255Scgd 			fflag = 1;
11261f28255Scgd 			break;
113a7a05335Smycroft 		case 'h':
114a7a05335Smycroft 			/*
11528ac946cSbjh21 			 * In System V the -h option causes chmod to
11628ac946cSbjh21 			 * change the mode of the symbolic link.
11728ac946cSbjh21 			 * 4.4BSD's symbolic links didn't have modes,
11828ac946cSbjh21 			 * so it was an undocumented noop.  In NetBSD
11928ac946cSbjh21 			 * 1.3, lchmod(2) is introduced and this
12028ac946cSbjh21 			 * option does real work.
121a7a05335Smycroft 			 */
122a7a05335Smycroft 			hflag = 1;
123a7a05335Smycroft 			break;
124a7a05335Smycroft 		/*
125a7a05335Smycroft 		 * XXX
126a7a05335Smycroft 		 * "-[rwx]" are valid mode commands.  If they are the entire
127a7a05335Smycroft 		 * argument, getopt has moved past them, so decrement optind.
128a7a05335Smycroft 		 * Regardless, we're done argument processing.
129a7a05335Smycroft 		 */
130a7a05335Smycroft 		case 'g': case 'o': case 'r': case 's':
131a7a05335Smycroft 		case 't': case 'u': case 'w': case 'X': case 'x':
132a7a05335Smycroft 			if (argv[optind - 1][0] == '-' &&
133a7a05335Smycroft 			    argv[optind - 1][1] == ch &&
134a7a05335Smycroft 			    argv[optind - 1][2] == '\0')
13561f28255Scgd 				--optind;
13661f28255Scgd 			goto done;
13761f28255Scgd 		case '?':
13861f28255Scgd 		default:
13961f28255Scgd 			usage();
14061f28255Scgd 		}
14161f28255Scgd done:	argv += optind;
14261f28255Scgd 	argc -= optind;
14361f28255Scgd 
144c8bb8a7aSchristos 	if (argc == 0 || (argc == 1 && reference == NULL))
14561f28255Scgd 		usage();
14661f28255Scgd 
147a7a05335Smycroft 	fts_options = FTS_PHYSICAL;
148a7a05335Smycroft 	if (Rflag) {
149458ed234Sjschauma 		if (hflag) {
150458ed234Sjschauma 			errx(EXIT_FAILURE,
151a7a05335Smycroft 		"the -R and -h options may not be specified together.");
152458ed234Sjschauma 			/* NOTREACHED */
153458ed234Sjschauma 		}
154a7a05335Smycroft 		if (Hflag)
155a7a05335Smycroft 			fts_options |= FTS_COMFOLLOW;
156a7a05335Smycroft 		if (Lflag) {
157a7a05335Smycroft 			fts_options &= ~FTS_PHYSICAL;
158a7a05335Smycroft 			fts_options |= FTS_LOGICAL;
159a7a05335Smycroft 		}
160525b0502Sbjh21 	} else if (!hflag)
161525b0502Sbjh21 		fts_options |= FTS_COMFOLLOW;
16216747b0eSenami 	if (hflag)
16316747b0eSenami 		change_mode = lchmod;
16416747b0eSenami 	else
16516747b0eSenami 		change_mode = chmod;
166a7a05335Smycroft 
167bc806954Schristos 	if (reference == NULL) {
168bc806954Schristos 		mode = *argv++;
169458ed234Sjschauma 		if ((set = setmode(mode)) == NULL) {
170037f0a7bSchristos 			err(EXIT_FAILURE, "Cannot set file mode `%s'", mode);
171458ed234Sjschauma 			/* NOTREACHED */
172458ed234Sjschauma 		}
173bc806954Schristos 		mval = 0;
174bc806954Schristos 	} else {
175bc806954Schristos 		struct stat st;
17661f28255Scgd 
177bc806954Schristos 		if (stat(reference, &st) == -1)
178bc806954Schristos 			err(EXIT_FAILURE, "Cannot stat `%s'", reference);
179bc806954Schristos 		mval = st.st_mode;
180bc806954Schristos 		set = NULL;
181bc806954Schristos 	}
182bc806954Schristos 
183bc806954Schristos 	if ((ftsp = fts_open(argv, fts_options, 0)) == NULL) {
184458ed234Sjschauma 		err(EXIT_FAILURE, "fts_open");
185458ed234Sjschauma 		/* NOTREACHED */
186458ed234Sjschauma 	}
187a7a05335Smycroft 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
18861f28255Scgd 		switch (p->fts_info) {
18961f28255Scgd 		case FTS_D:
19071dcdbe0Smycroft 			if (!Rflag)
191a05983beSmycroft 				(void)fts_set(ftsp, p, FTS_SKIP);
19261f28255Scgd 			break;
193a7a05335Smycroft 		case FTS_DNR:			/* Warn, chmod, continue. */
1946a75fbb6Sjschauma 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
195a7a05335Smycroft 			rval = 1;
196a7a05335Smycroft 			break;
19771dcdbe0Smycroft 		case FTS_DP:			/* Already changed at FTS_D. */
19871dcdbe0Smycroft 			continue;
199a7a05335Smycroft 		case FTS_ERR:			/* Warn, continue. */
20061f28255Scgd 		case FTS_NS:
2016a75fbb6Sjschauma 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
202a7a05335Smycroft 			rval = 1;
203a7a05335Smycroft 			continue;
204a7a05335Smycroft 		case FTS_SL:			/* Ignore. */
205a7a05335Smycroft 		case FTS_SLNONE:
206a7a05335Smycroft 			/*
207a7a05335Smycroft 			 * The only symlinks that end up here are ones that
208a7a05335Smycroft 			 * don't point to anything and ones that we found
209a7a05335Smycroft 			 * doing a physical walk.
210a7a05335Smycroft 			 */
21116747b0eSenami 			if (!hflag)
212a7a05335Smycroft 				continue;
21316747b0eSenami 			/* else */
21416747b0eSenami 			/* FALLTHROUGH */
21561f28255Scgd 		default:
21661f28255Scgd 			break;
21761f28255Scgd 		}
218*da39777eSkre 		nval = set ? getmode(set, p->fts_statp->st_mode) : mval;
219*da39777eSkre 		if (dflag && nval == p->fts_statp->st_mode)
220*da39777eSkre 			continue;
221*da39777eSkre 		if ((*change_mode)(p->fts_accpath, nval) && !fflag) {
2226a75fbb6Sjschauma 			warn("%s", p->fts_path);
223a7a05335Smycroft 			rval = 1;
22461f28255Scgd 		}
225032eb355Sderaadt 	}
226458ed234Sjschauma 	if (errno) {
227458ed234Sjschauma 		err(EXIT_FAILURE, "fts_read");
228458ed234Sjschauma 		/* NOTREACHED */
229458ed234Sjschauma 	}
230a7a05335Smycroft 	exit(rval);
23129bf463dSmycroft 	/* NOTREACHED */
23261f28255Scgd }
23361f28255Scgd 
2345bb1ddccSjoerg static void
usage(void)235516b44deSwiz usage(void)
23661f28255Scgd {
237a7a05335Smycroft 	(void)fprintf(stderr,
238*da39777eSkre 	    "Usage: %s [-R [-H | -L | -P]] [-dfh] mode file ...\n"
239*da39777eSkre 	    "\t%s [-R [-H | -L | -P]] [-dfh] --reference=rfile file ...\n",
240bc806954Schristos 	    getprogname(), getprogname());
24161f28255Scgd 	exit(1);
2429dc385beSmycroft 	/* NOTREACHED */
24361f28255Scgd }
244