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