Home | History | Annotate | Line # | Download | only in chmod
chmod.c revision 1.3
      1 /*
      2  * Copyright (c) 1989 The Regents of the University of California.
      3  * 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 char copyright[] =
     36 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 static char sccsid[] = "@(#)chmod.c	5.19 (Berkeley) 3/12/91";
     42 static char rcsid[] = "$Header: /tank/opengrok/rsync2/NetBSD/src/bin/chmod/chmod.c,v 1.3 1993/03/23 00:23:34 cgd Exp $";
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/stat.h>
     47 #include <fts.h>
     48 #include <stdio.h>
     49 #include <string.h>
     50 
     51 extern int errno;
     52 int retval;
     53 
     54 main(argc, argv)
     55 	int argc;
     56 	char **argv;
     57 {
     58 	extern int optind;
     59 	register FTS *fts;
     60 	register FTSENT *p;
     61 	register int oct, omode;
     62 	register char *mode;
     63 	mode_t *set, *setmode();
     64 	struct stat sb;
     65 	int ch, fflag, rflag;
     66 
     67 	fflag = rflag = 0;
     68 	while ((ch = getopt(argc, argv, "Rfrwx")) != EOF)
     69 		switch((char)ch) {
     70 		case 'R':
     71 			rflag = 1;
     72 			break;
     73 		case 'f':		/* no longer documented */
     74 			fflag = 1;
     75 			break;
     76 		case 'r':		/* "-[rwx]" are valid file modes */
     77 		case 'w':
     78 		case 'x':
     79 			--optind;
     80 			goto done;
     81 		case '?':
     82 		default:
     83 			usage();
     84 		}
     85 done:	argv += optind;
     86 	argc -= optind;
     87 
     88 	if (argc < 2)
     89 		usage();
     90 
     91 	mode = *argv;
     92 	if (*mode >= '0' && *mode <= '7') {
     93 		omode = (int)strtol(mode, (char **)NULL, 8);
     94 		oct = 1;
     95 	} else {
     96 		if (!(set = setmode(mode))) {
     97 			(void)fprintf(stderr, "chmod: invalid file mode.\n");
     98 			exit(1);
     99 		}
    100 		oct = 0;
    101 	}
    102 
    103 	retval = 0;
    104 	if (rflag) {
    105 		if (!(fts = fts_open(++argv,
    106 		    oct ? FTS_NOSTAT|FTS_PHYSICAL : FTS_PHYSICAL, 0))) {
    107 			(void)fprintf(stderr, "chmod: %s.\n", strerror(errno));
    108 			exit(1);
    109 		}
    110 		while (p = fts_read(fts))
    111 			switch(p->fts_info) {
    112 			case FTS_D:
    113 				break;
    114 			case FTS_DNR:
    115 			case FTS_ERR:
    116 			case FTS_NS:
    117 				(void)fprintf(stderr, "chmod: %s: %s.\n",
    118 				    p->fts_path, strerror(errno));
    119 				exit(1);
    120 			default:
    121 				if (chmod(p->fts_accpath, oct ? omode :
    122 				    getmode(set, p->fts_statb.st_mode)) &&
    123 				    !fflag)
    124 					error(p->fts_path);
    125 				break;
    126 			}
    127 		exit(retval);
    128 	}
    129 	if (oct) {
    130 		while (*++argv)
    131 			if (chmod(*argv, omode) && !fflag)
    132 				error(*argv);
    133 	} else
    134 		while (*++argv)
    135 			if ((lstat(*argv, &sb) ||
    136 			    chmod(*argv, getmode(set, sb.st_mode))) && !fflag)
    137 				error(*argv);
    138 	exit(retval);
    139 }
    140 
    141 error(name)
    142 	char *name;
    143 {
    144 	(void)fprintf(stderr, "chmod: %s: %s.\n", name, strerror(errno));
    145 	retval = 1;
    146 }
    147 
    148 usage()
    149 {
    150 	(void)fprintf(stderr, "chmod: chmod [-R] mode file ...\n");
    151 	exit(1);
    152 }
    153