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[] = "from: @(#)chmod.c 5.21 (Berkeley) 1/27/92";*/ 42 static char rcsid[] = "$Id: chmod.c,v 1.8 1994/02/11 02:57:24 cgd Exp $"; 43 #endif /* not lint */ 44 45 #include <sys/types.h> 46 #include <sys/stat.h> 47 #include <errno.h> 48 #include <fts.h> 49 #include <unistd.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 54 int retval; 55 56 void err __P((const char *, ...)); 57 void error __P((char *)); 58 void usage __P((void)); 59 60 int 61 main(argc, argv) 62 int argc; 63 char *argv[]; 64 { 65 register FTS *fts; 66 register FTSENT *p; 67 register int oct, omode; 68 struct stat sb; 69 mode_t *set; 70 int ch, fflag, rflag; 71 char *ep, *mode; 72 73 fflag = rflag = 0; 74 while ((ch = getopt(argc, argv, "Rfrwx")) != EOF) 75 switch((char)ch) { 76 case 'R': 77 rflag = 1; 78 break; 79 case 'f': /* no longer documented */ 80 fflag = 1; 81 break; 82 case 'r': /* "-[rwx]" are valid file modes */ 83 case 'w': 84 case 'x': 85 case 'u': /* as are -[ugo] */ 86 case 'g': 87 case 'o': 88 case 'X': /* -[Xts] valid but ignored later */ 89 case 't': 90 case 's': 91 --optind; 92 goto done; 93 case '?': 94 default: 95 usage(); 96 } 97 done: argv += optind; 98 argc -= optind; 99 100 if (argc < 2) 101 usage(); 102 103 mode = *argv; 104 if (*mode >= '0' && *mode <= '7') { 105 omode = (int)strtol(mode, &ep, 8); 106 if (omode < 0 || *ep) 107 err("invalid file mode: %s", mode); 108 oct = 1; 109 } else { 110 if (!(set = setmode(mode))) 111 err("invalid file mode: %s", mode); 112 oct = 0; 113 } 114 115 retval = 0; 116 if (rflag) { 117 if ((fts = fts_open(++argv, FTS_PHYSICAL, 0)) == NULL) 118 err("%s", strerror(errno)); 119 while (p = fts_read(fts)) 120 switch(p->fts_info) { 121 case FTS_D: 122 case FTS_SL: 123 break; 124 case FTS_DNR: 125 case FTS_ERR: 126 case FTS_NS: 127 err("%s: %s", p->fts_path, strerror(errno)); 128 default: 129 if (chmod(p->fts_accpath, oct ? omode : 130 getmode(set, p->fts_statp->st_mode)) && 131 !fflag) 132 error(p->fts_path); 133 break; 134 } 135 exit(retval); 136 } 137 while (*++argv) { 138 if (lstat(*argv, &sb) < 0) { 139 if (!fflag) 140 error(*argv); 141 continue; 142 } 143 if (S_ISLNK(sb.st_mode)) 144 continue; 145 if (chmod(*argv, oct ? omode : getmode(set, sb.st_mode)) && 146 !fflag) 147 error(*argv); 148 } 149 exit(retval); 150 } 151 152 void 153 error(name) 154 char *name; 155 { 156 (void)fprintf(stderr, "chmod: %s: %s\n", name, strerror(errno)); 157 retval = 1; 158 } 159 160 void 161 usage() 162 { 163 (void)fprintf(stderr, "usage: chmod [-R] mode file ...\n"); 164 exit(1); 165 } 166 167 #if __STDC__ 168 #include <stdarg.h> 169 #else 170 #include <varargs.h> 171 #endif 172 173 void 174 #if __STDC__ 175 err(const char *fmt, ...) 176 #else 177 err(fmt, va_alist) 178 char *fmt; 179 va_dcl 180 #endif 181 { 182 va_list ap; 183 #if __STDC__ 184 va_start(ap, fmt); 185 #else 186 va_start(ap); 187 #endif 188 (void)fprintf(stderr, "chmod: "); 189 (void)vfprintf(stderr, fmt, ap); 190 va_end(ap); 191 (void)fprintf(stderr, "\n"); 192 exit(1); 193 /* NOTREACHED */ 194 } 195