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