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