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