Home | History | Annotate | Line # | Download | only in chmod
chmod.c revision 1.39
      1 /* $NetBSD: chmod.c,v 1.39 2023/05/05 04:14:02 kre 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.39 2023/05/05 04:14:02 kre 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 #include <getopt.h>
     61 
     62 __dead static void	usage(void);
     63 
     64 struct option chmod_longopts[] = {
     65 	{ "reference",		required_argument,	0,
     66 						1 },
     67 	{ NULL,			0,			0,
     68 						0 },
     69 };
     70 
     71 int
     72 main(int argc, char *argv[])
     73 {
     74 	FTS *ftsp;
     75 	FTSENT *p;
     76 	void *set;
     77 	mode_t mval, nval;
     78 	int Hflag, Lflag, Rflag, ch, dflag, fflag, fts_options, hflag, rval;
     79 	char *mode, *reference;
     80 	int (*change_mode)(const char *, mode_t);
     81 
     82 	setprogname(argv[0]);
     83 	(void)setlocale(LC_ALL, "");
     84 
     85 	Hflag = Lflag = Rflag = dflag = fflag = hflag = 0;
     86 	reference = NULL;
     87 	while ((ch = getopt_long(argc, argv, "HLPRXdfghorstuwx",
     88 	    chmod_longopts, NULL)) != -1)
     89 		switch (ch) {
     90 		case 1:
     91 			reference = optarg;
     92 			break;
     93 		case 'H':
     94 			Hflag = 1;
     95 			Lflag = 0;
     96 			break;
     97 		case 'L':
     98 			Lflag = 1;
     99 			Hflag = 0;
    100 			break;
    101 		case 'P':
    102 			Hflag = Lflag = 0;
    103 			break;
    104 		case 'R':
    105 			Rflag = 1;
    106 			break;
    107 		case 'd':
    108 			dflag = 1;
    109 			break;
    110 		case 'f':
    111 			fflag = 1;
    112 			break;
    113 		case 'h':
    114 			/*
    115 			 * In System V the -h option causes chmod to
    116 			 * change the mode of the symbolic link.
    117 			 * 4.4BSD's symbolic links didn't have modes,
    118 			 * so it was an undocumented noop.  In NetBSD
    119 			 * 1.3, lchmod(2) is introduced and this
    120 			 * option does real work.
    121 			 */
    122 			hflag = 1;
    123 			break;
    124 		/*
    125 		 * XXX
    126 		 * "-[rwx]" are valid mode commands.  If they are the entire
    127 		 * argument, getopt has moved past them, so decrement optind.
    128 		 * Regardless, we're done argument processing.
    129 		 */
    130 		case 'g': case 'o': case 'r': case 's':
    131 		case 't': case 'u': case 'w': case 'X': case 'x':
    132 			if (argv[optind - 1][0] == '-' &&
    133 			    argv[optind - 1][1] == ch &&
    134 			    argv[optind - 1][2] == '\0')
    135 				--optind;
    136 			goto done;
    137 		case '?':
    138 		default:
    139 			usage();
    140 		}
    141 done:	argv += optind;
    142 	argc -= optind;
    143 
    144 	if (argc == 0 || (argc == 1 && reference == NULL))
    145 		usage();
    146 
    147 	fts_options = FTS_PHYSICAL;
    148 	if (Rflag) {
    149 		if (hflag) {
    150 			errx(EXIT_FAILURE,
    151 		"the -R and -h options may not be specified together.");
    152 			/* NOTREACHED */
    153 		}
    154 		if (Hflag)
    155 			fts_options |= FTS_COMFOLLOW;
    156 		if (Lflag) {
    157 			fts_options &= ~FTS_PHYSICAL;
    158 			fts_options |= FTS_LOGICAL;
    159 		}
    160 	} else if (!hflag)
    161 		fts_options |= FTS_COMFOLLOW;
    162 	if (hflag)
    163 		change_mode = lchmod;
    164 	else
    165 		change_mode = chmod;
    166 
    167 	if (reference == NULL) {
    168 		mode = *argv++;
    169 		if ((set = setmode(mode)) == NULL) {
    170 			err(EXIT_FAILURE, "Cannot set file mode `%s'", mode);
    171 			/* NOTREACHED */
    172 		}
    173 		mval = 0;
    174 	} else {
    175 		struct stat st;
    176 
    177 		if (stat(reference, &st) == -1)
    178 			err(EXIT_FAILURE, "Cannot stat `%s'", reference);
    179 		mval = st.st_mode;
    180 		set = NULL;
    181 	}
    182 
    183 	if ((ftsp = fts_open(argv, fts_options, 0)) == NULL) {
    184 		err(EXIT_FAILURE, "fts_open");
    185 		/* NOTREACHED */
    186 	}
    187 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
    188 		switch (p->fts_info) {
    189 		case FTS_D:
    190 			if (!Rflag)
    191 				(void)fts_set(ftsp, p, FTS_SKIP);
    192 			break;
    193 		case FTS_DNR:			/* Warn, chmod, continue. */
    194 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
    195 			rval = 1;
    196 			break;
    197 		case FTS_DP:			/* Already changed at FTS_D. */
    198 			continue;
    199 		case FTS_ERR:			/* Warn, continue. */
    200 		case FTS_NS:
    201 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
    202 			rval = 1;
    203 			continue;
    204 		case FTS_SL:			/* Ignore. */
    205 		case FTS_SLNONE:
    206 			/*
    207 			 * The only symlinks that end up here are ones that
    208 			 * don't point to anything and ones that we found
    209 			 * doing a physical walk.
    210 			 */
    211 			if (!hflag)
    212 				continue;
    213 			/* else */
    214 			/* FALLTHROUGH */
    215 		default:
    216 			break;
    217 		}
    218 		nval = set ? getmode(set, p->fts_statp->st_mode) : mval;
    219 		if (dflag && nval == p->fts_statp->st_mode)
    220 			continue;
    221 		if ((*change_mode)(p->fts_accpath, nval) && !fflag) {
    222 			warn("%s", p->fts_path);
    223 			rval = 1;
    224 		}
    225 	}
    226 	if (errno) {
    227 		err(EXIT_FAILURE, "fts_read");
    228 		/* NOTREACHED */
    229 	}
    230 	exit(rval);
    231 	/* NOTREACHED */
    232 }
    233 
    234 static void
    235 usage(void)
    236 {
    237 	(void)fprintf(stderr,
    238 	    "Usage: %s [-R [-H | -L | -P]] [-dfh] mode file ...\n"
    239 	    "\t%s [-R [-H | -L | -P]] [-dfh] --reference=rfile file ...\n",
    240 	    getprogname(), getprogname());
    241 	exit(1);
    242 	/* NOTREACHED */
    243 }
    244