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