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