Home | History | Annotate | Line # | Download | only in chmod
chmod.c revision 1.9
      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.9 1994/09/20 04:07:04 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 
     56 void usage __P((void));
     57 
     58 int
     59 main(argc, argv)
     60 	int argc;
     61 	char *argv[];
     62 {
     63 	FTS *ftsp;
     64 	FTSENT *p;
     65 	mode_t *set;
     66 	long val;
     67 	int oct, omode;
     68 	int Hflag, Lflag, Pflag, Rflag, ch, fflag, fts_options, hflag, rval;
     69 	char *ep, *mode;
     70 
     71 	Hflag = Lflag = Pflag = Rflag = fflag = hflag = 0;
     72 	while ((ch = getopt(argc, argv, "HLPRXfgorstuwx")) != -1)
     73 		switch (ch) {
     74 		case 'H':
     75 			Hflag = 1;
     76 			Lflag = Pflag = 0;
     77 			break;
     78 		case 'L':
     79 			Lflag = 1;
     80 			Hflag = Pflag = 0;
     81 			break;
     82 		case 'P':
     83 			Pflag = 1;
     84 			Hflag = Lflag = 0;
     85 			break;
     86 		case 'R':
     87 			Rflag = 1;
     88 			break;
     89 		case 'f':		/* XXX: undocumented. */
     90 			fflag = 1;
     91 			break;
     92 		case 'h':
     93 			/*
     94 			 * In System V (and probably POSIX.2) the -h option
     95 			 * causes chmod to change the mode of the symbolic
     96 			 * link.  4.4BSD's symbolic links don't have modes,
     97 			 * so it's an undocumented noop.  Do syntax checking,
     98 			 * though.
     99 			 */
    100 			hflag = 1;
    101 			break;
    102 		/*
    103 		 * XXX
    104 		 * "-[rwx]" are valid mode commands.  If they are the entire
    105 		 * argument, getopt has moved past them, so decrement optind.
    106 		 * Regardless, we're done argument processing.
    107 		 */
    108 		case 'g': case 'o': case 'r': case 's':
    109 		case 't': case 'u': case 'w': case 'X': case 'x':
    110 			if (argv[optind - 1][0] == '-' &&
    111 			    argv[optind - 1][1] == ch &&
    112 			    argv[optind - 1][2] == '\0')
    113 				--optind;
    114 			goto done;
    115 		case '?':
    116 		default:
    117 			usage();
    118 		}
    119 done:	argv += optind;
    120 	argc -= optind;
    121 
    122 	if (argc < 2)
    123 		usage();
    124 
    125 	fts_options = FTS_PHYSICAL;
    126 	if (Rflag) {
    127 		if (hflag)
    128 			errx(1,
    129 		"the -R and -h options may not be specified together.");
    130 		if (Hflag)
    131 			fts_options |= FTS_COMFOLLOW;
    132 		if (Lflag) {
    133 			fts_options &= ~FTS_PHYSICAL;
    134 			fts_options |= FTS_LOGICAL;
    135 		}
    136 	}
    137 
    138 	mode = *argv;
    139 	if (*mode >= '0' && *mode <= '7') {
    140 		errno = 0;
    141 		val = strtol(mode, &ep, 8);
    142 		if (val > INT_MAX || val < 0)
    143 			errno = ERANGE;
    144 		if (errno)
    145 			err(1, "invalid file mode: %s", mode);
    146 		if (*ep)
    147 			errx(1, "invalid file mode: %s", mode);
    148 		omode = val;
    149 		oct = 1;
    150 	} else {
    151 		if ((set = setmode(mode)) == NULL)
    152 			errx(1, "invalid file mode: %s", mode);
    153 		oct = 0;
    154 	}
    155 
    156 	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
    157 		err(1, NULL);
    158 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
    159 		switch (p->fts_info) {
    160 		case FTS_D:
    161 			if (Rflag)		/* Change it at FTS_DP. */
    162 				continue;
    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_ERR:			/* Warn, continue. */
    170 		case FTS_NS:
    171 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
    172 			rval = 1;
    173 			continue;
    174 		case FTS_SL:			/* Ignore. */
    175 		case FTS_SLNONE:
    176 			/*
    177 			 * The only symlinks that end up here are ones that
    178 			 * don't point to anything and ones that we found
    179 			 * doing a physical walk.
    180 			 */
    181 			continue;
    182 		default:
    183 			break;
    184 		}
    185 		if (chmod(p->fts_accpath, oct ? omode :
    186 		    getmode(set, p->fts_statp->st_mode)) && !fflag) {
    187 			warn(p->fts_path);
    188 			rval = 1;
    189 		}
    190 	}
    191 	if (errno)
    192 		err(1, "fts_read");
    193 	exit(rval);
    194 }
    195 
    196 void
    197 usage()
    198 {
    199 	(void)fprintf(stderr,
    200 	    "usage: chmod [-R [-H | -L | -P]] mode file ...\n");
    201 	exit(1);
    202 }
    203