1 /* $NetBSD: chflags.c,v 1.9 2002/07/07 11:44:03 bjh21 Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 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 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1992, 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[] = "from: @(#)chflags.c 8.5 (Berkeley) 4/1/94"; 45 #else 46 __RCSID("$NetBSD: chflags.c,v 1.9 2002/07/07 11:44:03 bjh21 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 <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <unistd.h> 60 61 #include "stat_flags.h" 62 63 int main __P((int, char **)); 64 void usage __P((void)); 65 66 int 67 main(argc, argv) 68 int argc; 69 char *argv[]; 70 { 71 FTS *ftsp; 72 FTSENT *p; 73 u_long clear, set, newflags; 74 long val; 75 int Hflag, Lflag, Rflag, ch, fts_options, hflag, oct, rval; 76 char *flags, *ep; 77 int (*change_flags) __P((const char *, u_long)); 78 79 Hflag = Lflag = Rflag = hflag = 0; 80 while ((ch = getopt(argc, argv, "HLPRh")) != -1) 81 switch (ch) { 82 case 'H': 83 Hflag = 1; 84 Lflag = 0; 85 break; 86 case 'L': 87 Lflag = 1; 88 Hflag = 0; 89 break; 90 case 'P': 91 Hflag = Lflag = 0; 92 break; 93 case 'R': 94 Rflag = 1; 95 break; 96 case 'h': 97 hflag = 1; 98 break; 99 case '?': 100 default: 101 usage(); 102 } 103 argv += optind; 104 argc -= optind; 105 106 if (argc < 2) 107 usage(); 108 109 fts_options = FTS_PHYSICAL; 110 if (Rflag) { 111 if (Hflag) 112 fts_options |= FTS_COMFOLLOW; 113 if (Lflag) { 114 fts_options &= ~FTS_PHYSICAL; 115 fts_options |= FTS_LOGICAL; 116 } 117 } else if (!hflag) 118 fts_options |= FTS_COMFOLLOW; 119 120 flags = *argv; 121 if (*flags >= '0' && *flags <= '7') { 122 errno = 0; 123 val = strtol(flags, &ep, 8); 124 if (val < 0) 125 errno = ERANGE; 126 if (errno) 127 err(1, "invalid flags: %s", flags); 128 if (*ep) 129 errx(1, "invalid flags: %s", flags); 130 set = val; 131 oct = 1; 132 } else { 133 if (string_to_flags(&flags, &set, &clear)) 134 errx(1, "invalid flag: %s", flags); 135 clear = ~clear; 136 oct = 0; 137 } 138 139 if ((ftsp = fts_open(++argv, fts_options, NULL)) == NULL) 140 err(1, "fts_open"); 141 142 for (rval = 0; (p = fts_read(ftsp)) != NULL;) { 143 change_flags = chflags; 144 switch (p->fts_info) { 145 case FTS_D: 146 if (Rflag) /* Change it at FTS_DP. */ 147 continue; 148 fts_set(ftsp, p, FTS_SKIP); 149 break; 150 case FTS_DNR: /* Warn, chflag, continue. */ 151 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 152 rval = 1; 153 break; 154 case FTS_ERR: /* Warn, continue. */ 155 case FTS_NS: 156 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 157 rval = 1; 158 continue; 159 case FTS_SL: /* Ignore unless -h. */ 160 /* 161 * All symlinks we found while doing a physical 162 * walk end up here. 163 */ 164 if (!hflag) 165 continue; 166 /* 167 * Note that if we follow a symlink, fts_info is 168 * not FTS_SL but FTS_F or whatever. And we should 169 * use lchflags only for FTS_SL and should use chflags 170 * for others. 171 */ 172 change_flags = lchflags; 173 break; 174 case FTS_SLNONE: /* Ignore. */ 175 /* 176 * The only symlinks that end up here are ones that 177 * don't point to anything. Note that if we are 178 * doing a phisycal walk, we never reach here unless 179 * we asked to follow explicitly. 180 */ 181 continue; 182 default: 183 break; 184 } 185 if (oct) 186 newflags = set; 187 else { 188 newflags = p->fts_statp->st_flags; 189 newflags |= set; 190 newflags &= clear; 191 } 192 if ((*change_flags)(p->fts_accpath, newflags)) { 193 warn("%s", p->fts_path); 194 rval = 1; 195 } 196 } 197 if (errno) 198 err(1, "fts_read"); 199 exit(rval); 200 } 201 202 void 203 usage() 204 { 205 206 (void)fprintf(stderr, 207 "usage: chflags [-R [-H | -L | -P]] [-h] flags file ...\n"); 208 exit(1); 209 } 210