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