1 /* $NetBSD: chflags.c,v 1.4 1995/03/26 08:49:20 glass 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 #ifndef lint 37 static char copyright[] = 38 "@(#) 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 static char rcsid[] = "$NetBSD: chflags.c,v 1.4 1995/03/26 08:49:20 glass 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 u_long string_to_flags __P((char **, u_long *, u_long *)); 62 void usage __P((void)); 63 64 int 65 main(argc, argv) 66 int argc; 67 char *argv[]; 68 { 69 FTS *ftsp; 70 FTSENT *p; 71 u_long clear, set; 72 long val; 73 int Hflag, Lflag, Pflag, Rflag, ch, fts_options, oct, rval; 74 char *flags, *ep; 75 76 Hflag = Lflag = Pflag = Rflag = 0; 77 while ((ch = getopt(argc, argv, "HLPR")) != -1) 78 switch (ch) { 79 case 'H': 80 Hflag = 1; 81 Lflag = Pflag = 0; 82 break; 83 case 'L': 84 Lflag = 1; 85 Hflag = Pflag = 0; 86 break; 87 case 'P': 88 Pflag = 1; 89 Hflag = Lflag = 0; 90 break; 91 case 'R': 92 Rflag = 1; 93 break; 94 case '?': 95 default: 96 usage(); 97 } 98 argv += optind; 99 argc -= optind; 100 101 if (argc < 2) 102 usage(); 103 104 fts_options = FTS_PHYSICAL; 105 if (Rflag) { 106 if (Hflag) 107 fts_options |= FTS_COMFOLLOW; 108 if (Lflag) { 109 fts_options &= ~FTS_PHYSICAL; 110 fts_options |= FTS_LOGICAL; 111 } 112 } 113 114 flags = *argv; 115 if (*flags >= '0' && *flags <= '7') { 116 errno = 0; 117 val = strtol(flags, &ep, 8); 118 if (val < 0) 119 errno = ERANGE; 120 if (errno) 121 err(1, "invalid flags: %s", flags); 122 if (*ep) 123 errx(1, "invalid flags: %s", flags); 124 set = val; 125 oct = 1; 126 } else { 127 if (string_to_flags(&flags, &set, &clear)) 128 errx(1, "invalid flag: %s", flags); 129 clear = ~clear; 130 oct = 0; 131 } 132 133 if ((ftsp = fts_open(++argv, fts_options , 0)) == NULL) 134 err(1, NULL); 135 136 for (rval = 0; (p = fts_read(ftsp)) != NULL;) { 137 switch (p->fts_info) { 138 case FTS_D: 139 if (Rflag) /* Change it at FTS_DP. */ 140 continue; 141 fts_set(ftsp, p, FTS_SKIP); 142 break; 143 case FTS_DNR: /* Warn, chflag, continue. */ 144 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 145 rval = 1; 146 break; 147 case FTS_ERR: /* Warn, continue. */ 148 case FTS_NS: 149 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 150 rval = 1; 151 continue; 152 case FTS_SL: /* Ignore. */ 153 case FTS_SLNONE: 154 /* 155 * The only symlinks that end up here are ones that 156 * don't point to anything and ones that we found 157 * doing a physical walk. 158 */ 159 continue; 160 default: 161 break; 162 } 163 if (oct) { 164 if (!chflags(p->fts_accpath, set)) 165 continue; 166 } else { 167 p->fts_statp->st_flags |= set; 168 p->fts_statp->st_flags &= clear; 169 if (!chflags(p->fts_accpath, p->fts_statp->st_flags)) 170 continue; 171 } 172 warn("%s", p->fts_path); 173 rval = 1; 174 } 175 if (errno) 176 err(1, "fts_read"); 177 exit(rval); 178 } 179 180 void 181 usage() 182 { 183 (void)fprintf(stderr, 184 "usage: chflags [-R [-H | -L | -P]] flags file ...\n"); 185 exit(1); 186 } 187