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