1 1.18 kre /* $NetBSD: chflags.c,v 1.18 2023/05/05 04:14:02 kre Exp $ */ 2 1.2 jtc 3 1.1 jtc /* 4 1.1 jtc * Copyright (c) 1992, 1993, 1994 5 1.1 jtc * The Regents of the University of California. All rights reserved. 6 1.1 jtc * 7 1.1 jtc * Redistribution and use in source and binary forms, with or without 8 1.1 jtc * modification, are permitted provided that the following conditions 9 1.1 jtc * are met: 10 1.1 jtc * 1. Redistributions of source code must retain the above copyright 11 1.1 jtc * notice, this list of conditions and the following disclaimer. 12 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jtc * notice, this list of conditions and the following disclaimer in the 14 1.1 jtc * documentation and/or other materials provided with the distribution. 15 1.10 agc * 3. Neither the name of the University nor the names of its contributors 16 1.1 jtc * may be used to endorse or promote products derived from this software 17 1.1 jtc * without specific prior written permission. 18 1.1 jtc * 19 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 jtc * SUCH DAMAGE. 30 1.1 jtc */ 31 1.1 jtc 32 1.5 lukem #include <sys/cdefs.h> 33 1.1 jtc #ifndef lint 34 1.15 lukem __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\ 35 1.15 lukem The Regents of the University of California. All rights reserved."); 36 1.1 jtc #endif /* not lint */ 37 1.1 jtc 38 1.1 jtc #ifndef lint 39 1.4 glass #if 0 40 1.4 glass static char sccsid[] = "from: @(#)chflags.c 8.5 (Berkeley) 4/1/94"; 41 1.4 glass #else 42 1.18 kre __RCSID("$NetBSD: chflags.c,v 1.18 2023/05/05 04:14:02 kre Exp $"); 43 1.4 glass #endif 44 1.1 jtc #endif /* not lint */ 45 1.1 jtc 46 1.1 jtc #include <sys/types.h> 47 1.1 jtc #include <sys/stat.h> 48 1.1 jtc 49 1.1 jtc #include <err.h> 50 1.1 jtc #include <errno.h> 51 1.1 jtc #include <fts.h> 52 1.1 jtc #include <stdio.h> 53 1.1 jtc #include <stdlib.h> 54 1.1 jtc #include <string.h> 55 1.1 jtc #include <unistd.h> 56 1.14 he #include <util.h> 57 1.6 mrg 58 1.16 joerg __dead static void usage(void); 59 1.1 jtc 60 1.1 jtc int 61 1.11 xtraeme main(int argc, char *argv[]) 62 1.1 jtc { 63 1.1 jtc FTS *ftsp; 64 1.1 jtc FTSENT *p; 65 1.8 enami u_long clear, set, newflags; 66 1.1 jtc long val; 67 1.18 kre int Hflag, Lflag, Rflag, ch, fts_options, dflag, hflag, oct, rval; 68 1.1 jtc char *flags, *ep; 69 1.13 xtraeme int (*change_flags)(const char *, u_long); 70 1.1 jtc 71 1.18 kre Hflag = Lflag = Rflag = dflag = hflag = 0; 72 1.18 kre while ((ch = getopt(argc, argv, "HLPRdh")) != -1) 73 1.1 jtc switch (ch) { 74 1.1 jtc case 'H': 75 1.1 jtc Hflag = 1; 76 1.7 enami Lflag = 0; 77 1.1 jtc break; 78 1.1 jtc case 'L': 79 1.1 jtc Lflag = 1; 80 1.7 enami Hflag = 0; 81 1.1 jtc break; 82 1.1 jtc case 'P': 83 1.1 jtc Hflag = Lflag = 0; 84 1.1 jtc break; 85 1.1 jtc case 'R': 86 1.1 jtc Rflag = 1; 87 1.1 jtc break; 88 1.18 kre case 'd': 89 1.18 kre dflag = 1; 90 1.18 kre break; 91 1.8 enami case 'h': 92 1.8 enami hflag = 1; 93 1.8 enami break; 94 1.1 jtc case '?': 95 1.1 jtc default: 96 1.1 jtc usage(); 97 1.1 jtc } 98 1.1 jtc argv += optind; 99 1.1 jtc argc -= optind; 100 1.1 jtc 101 1.1 jtc if (argc < 2) 102 1.1 jtc usage(); 103 1.1 jtc 104 1.1 jtc fts_options = FTS_PHYSICAL; 105 1.1 jtc if (Rflag) { 106 1.1 jtc if (Hflag) 107 1.1 jtc fts_options |= FTS_COMFOLLOW; 108 1.1 jtc if (Lflag) { 109 1.1 jtc fts_options &= ~FTS_PHYSICAL; 110 1.1 jtc fts_options |= FTS_LOGICAL; 111 1.1 jtc } 112 1.9 bjh21 } else if (!hflag) 113 1.9 bjh21 fts_options |= FTS_COMFOLLOW; 114 1.1 jtc 115 1.1 jtc flags = *argv; 116 1.1 jtc if (*flags >= '0' && *flags <= '7') { 117 1.1 jtc errno = 0; 118 1.1 jtc val = strtol(flags, &ep, 8); 119 1.1 jtc if (val < 0) 120 1.1 jtc errno = ERANGE; 121 1.1 jtc if (errno) 122 1.7 enami err(1, "invalid flags: %s", flags); 123 1.7 enami if (*ep) 124 1.7 enami errx(1, "invalid flags: %s", flags); 125 1.1 jtc set = val; 126 1.7 enami oct = 1; 127 1.1 jtc } else { 128 1.1 jtc if (string_to_flags(&flags, &set, &clear)) 129 1.7 enami errx(1, "invalid flag: %s", flags); 130 1.1 jtc clear = ~clear; 131 1.1 jtc oct = 0; 132 1.1 jtc } 133 1.1 jtc 134 1.7 enami if ((ftsp = fts_open(++argv, fts_options, NULL)) == NULL) 135 1.8 enami err(1, "fts_open"); 136 1.1 jtc 137 1.1 jtc for (rval = 0; (p = fts_read(ftsp)) != NULL;) { 138 1.8 enami change_flags = chflags; 139 1.1 jtc switch (p->fts_info) { 140 1.1 jtc case FTS_D: 141 1.1 jtc if (Rflag) /* Change it at FTS_DP. */ 142 1.1 jtc continue; 143 1.1 jtc fts_set(ftsp, p, FTS_SKIP); 144 1.1 jtc break; 145 1.1 jtc case FTS_DNR: /* Warn, chflag, continue. */ 146 1.1 jtc warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 147 1.1 jtc rval = 1; 148 1.1 jtc break; 149 1.1 jtc case FTS_ERR: /* Warn, continue. */ 150 1.1 jtc case FTS_NS: 151 1.1 jtc warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 152 1.1 jtc rval = 1; 153 1.1 jtc continue; 154 1.8 enami case FTS_SL: /* Ignore unless -h. */ 155 1.8 enami /* 156 1.8 enami * All symlinks we found while doing a physical 157 1.8 enami * walk end up here. 158 1.8 enami */ 159 1.8 enami if (!hflag) 160 1.8 enami continue; 161 1.8 enami /* 162 1.8 enami * Note that if we follow a symlink, fts_info is 163 1.8 enami * not FTS_SL but FTS_F or whatever. And we should 164 1.8 enami * use lchflags only for FTS_SL and should use chflags 165 1.8 enami * for others. 166 1.8 enami */ 167 1.8 enami change_flags = lchflags; 168 1.8 enami break; 169 1.8 enami case FTS_SLNONE: /* Ignore. */ 170 1.1 jtc /* 171 1.1 jtc * The only symlinks that end up here are ones that 172 1.8 enami * don't point to anything. Note that if we are 173 1.17 andvar * doing a physical walk, we never reach here unless 174 1.8 enami * we asked to follow explicitly. 175 1.1 jtc */ 176 1.1 jtc continue; 177 1.1 jtc default: 178 1.1 jtc break; 179 1.1 jtc } 180 1.8 enami if (oct) 181 1.8 enami newflags = set; 182 1.8 enami else { 183 1.8 enami newflags = p->fts_statp->st_flags; 184 1.8 enami newflags |= set; 185 1.8 enami newflags &= clear; 186 1.8 enami } 187 1.18 kre if (dflag && newflags == p->fts_statp->st_flags) 188 1.18 kre continue; 189 1.8 enami if ((*change_flags)(p->fts_accpath, newflags)) { 190 1.8 enami warn("%s", p->fts_path); 191 1.8 enami rval = 1; 192 1.1 jtc } 193 1.1 jtc } 194 1.1 jtc if (errno) 195 1.1 jtc err(1, "fts_read"); 196 1.1 jtc exit(rval); 197 1.1 jtc } 198 1.1 jtc 199 1.16 joerg static void 200 1.11 xtraeme usage(void) 201 1.1 jtc { 202 1.8 enami 203 1.1 jtc (void)fprintf(stderr, 204 1.18 kre "usage: chflags [-R [-H | -L | -P]] [-dh] flags file ...\n"); 205 1.1 jtc exit(1); 206 1.1 jtc } 207