1 /*- 2 * Copyright (c) 1992, 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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#) Copyright (c) 1992, 1993, 1994 The Regents of the University of California. All rights reserved. 30 * @(#)chflags.c 8.5 (Berkeley) 4/1/94 31 * $FreeBSD: src/bin/chflags/chflags.c,v 1.24 2008/03/09 12:10:24 rwatson Exp $ 32 * $DragonFly: src/usr.bin/chflags/chflags.c,v 1.8 2008/11/11 06:56:47 pavalos Exp $ 33 */ 34 35 #include <sys/param.h> 36 #include <sys/types.h> 37 #include <sys/stat.h> 38 39 #include <err.h> 40 #include <errno.h> 41 #include <fts.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <unistd.h> 46 47 static void usage(void); 48 49 int 50 main(int argc, char **argv) 51 { 52 FTS *ftsp; 53 FTSENT *p; 54 u_long clear, newflags, set; 55 long val; 56 int Hflag, Lflag, Rflag, fflag, hflag, vflag; 57 int ch, fts_options, oct, rval; 58 char *flags, *ep; 59 int (*change_flags)(const char *, unsigned long); 60 61 Hflag = Lflag = Rflag = fflag = hflag = vflag = 0; 62 while ((ch = getopt(argc, argv, "HLPRfhv")) != -1) 63 switch (ch) { 64 case 'H': 65 Hflag = 1; 66 Lflag = 0; 67 break; 68 case 'L': 69 Lflag = 1; 70 Hflag = 0; 71 break; 72 case 'P': 73 Hflag = Lflag = 0; 74 break; 75 case 'R': 76 Rflag = 1; 77 break; 78 case 'f': 79 fflag = 1; 80 break; 81 case 'h': 82 hflag = 1; 83 break; 84 case 'v': 85 vflag++; 86 break; 87 default: 88 usage(); 89 } 90 argv += optind; 91 argc -= optind; 92 93 if (argc < 2) 94 usage(); 95 96 if (Rflag) { 97 fts_options = FTS_PHYSICAL; 98 if (hflag) { 99 errx(1, "the -R and -h options " 100 "may not be specified together"); 101 } 102 if (Hflag) 103 fts_options |= FTS_COMFOLLOW; 104 if (Lflag) { 105 fts_options &= ~FTS_PHYSICAL; 106 fts_options |= FTS_LOGICAL; 107 } 108 } else 109 fts_options = FTS_LOGICAL; 110 111 #if __DragonFly_version >= 200101 112 if (hflag) 113 change_flags = lchflags; 114 else 115 #endif 116 change_flags = chflags; 117 118 flags = *argv; 119 if (*flags >= '0' && *flags <= '7') { 120 errno = 0; 121 val = strtol(flags, &ep, 8); 122 if (val < 0) 123 errno = ERANGE; 124 if (errno) 125 err(1, "invalid flags: %s", flags); 126 if (*ep) 127 errx(1, "invalid flags: %s", flags); 128 set = val; 129 oct = 1; 130 } else { 131 if (strtofflags(&flags, &set, &clear)) 132 errx(1, "invalid flag: %s", flags); 133 clear = ~clear; 134 oct = 0; 135 } 136 137 if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL) 138 err(1, NULL); 139 140 for (rval = 0; (p = fts_read(ftsp)) != NULL;) { 141 switch (p->fts_info) { 142 case FTS_D: 143 if (!Rflag) /* Change it at FTS_DP if we're recursive. */ 144 fts_set(ftsp, p, FTS_SKIP); 145 continue; 146 case FTS_DNR: /* Warn, chflag, continue. */ 147 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 148 rval = 1; 149 break; 150 case FTS_ERR: /* Warn, continue. */ 151 case FTS_NS: 152 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 153 rval = 1; 154 continue; 155 case FTS_SL: /* Ignore. */ 156 case FTS_SLNONE: 157 /* 158 * The only symlinks that end up here are ones that 159 * don't point to anything and ones that we found 160 * doing a physical walk. 161 */ 162 if (!hflag) 163 continue; 164 /* FALLTHROUGH */ 165 default: 166 break; 167 } 168 if (oct) 169 newflags = set; 170 else 171 newflags = (p->fts_statp->st_flags | set) & clear; 172 if (newflags == p->fts_statp->st_flags) 173 continue; 174 if ((*change_flags)(p->fts_accpath, newflags) && !fflag) { 175 warn("%s", p->fts_path); 176 rval = 1; 177 } else if (vflag) { 178 printf("%s", p->fts_path); 179 if (vflag > 1) 180 printf(": 0%lo -> 0%lo", 181 (u_long)p->fts_statp->st_flags, 182 newflags); 183 printf("\n"); 184 } 185 } 186 if (errno) 187 err(1, "fts_read"); 188 exit(rval); 189 } 190 191 static void 192 usage(void) 193 { 194 fprintf(stderr, 195 "usage: chflags [-fhv] [-R [-H | -L | -P]] flags file ...\n"); 196 exit(1); 197 } 198