1 /* @(#)stat_flags.c 8.2 (Berkeley) 7/28/94 */ 2 /* $NetBSD: stat_flags.c,v 1.2 2007/01/16 17:34:02 cbiere Exp $ */ 3 4 /*- 5 * Copyright (c) 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #if HAVE_NBTOOL_CONFIG_H 34 #include "nbtool_config.h" 35 #else 36 #define HAVE_STRUCT_STAT_ST_FLAGS 1 37 #endif 38 39 #include <sys/types.h> 40 #include <sys/stat.h> 41 #include <fts.h> 42 #include <stddef.h> 43 #include <stdio.h> 44 #include <string.h> 45 #include <stdlib.h> 46 47 #include "extern.h" 48 49 #define SAPPEND(s) do { \ 50 if (prefix != NULL) \ 51 (void)strlcat(string, prefix, sizeof(string)); \ 52 (void)strlcat(string, s, sizeof(string)); \ 53 prefix = ","; \ 54 } while (/* CONSTCOND */ 0) 55 56 /* 57 * flags_to_string -- 58 * Convert stat flags to a comma-separated string. If no flags 59 * are set, return the default string. 60 */ 61 char * 62 flags_to_string(u_long flags, const char *def) 63 { 64 char string[128]; 65 const char *prefix; 66 67 string[0] = '\0'; 68 prefix = NULL; 69 #if HAVE_STRUCT_STAT_ST_FLAGS 70 if (flags & UF_APPEND) 71 SAPPEND("uappnd"); 72 if (flags & UF_IMMUTABLE) 73 SAPPEND("uchg"); 74 if (flags & UF_NODUMP) 75 SAPPEND("nodump"); 76 if (flags & UF_OPAQUE) 77 SAPPEND("opaque"); 78 if (flags & SF_APPEND) 79 SAPPEND("sappnd"); 80 if (flags & SF_ARCHIVED) 81 SAPPEND("arch"); 82 if (flags & SF_IMMUTABLE) 83 SAPPEND("schg"); 84 #ifdef SF_SNAPSHOT 85 if (flags & SF_SNAPSHOT) 86 SAPPEND("snap"); 87 #endif 88 #endif 89 if (prefix != NULL) 90 return strdup(string); 91 return strdup(def); 92 } 93 94 #define TEST(a, b, f) { \ 95 if (!strcmp(a, b)) { \ 96 if (clear) { \ 97 if (clrp) \ 98 *clrp |= (f); \ 99 if (setp) \ 100 *setp &= ~(f); \ 101 } else { \ 102 if (setp) \ 103 *setp |= (f); \ 104 if (clrp) \ 105 *clrp &= ~(f); \ 106 } \ 107 break; \ 108 } \ 109 } 110 111 /* 112 * string_to_flags -- 113 * Take string of arguments and return stat flags. Return 0 on 114 * success, 1 on failure. On failure, stringp is set to point 115 * to the offending token. 116 */ 117 int 118 string_to_flags(char **stringp, u_long *setp, u_long *clrp) 119 { 120 int clear; 121 char *string, *p; 122 123 if (setp) 124 *setp = 0; 125 if (clrp) 126 *clrp = 0; 127 128 #if HAVE_STRUCT_STAT_ST_FLAGS 129 string = *stringp; 130 while ((p = strsep(&string, "\t ,")) != NULL) { 131 clear = 0; 132 *stringp = p; 133 if (*p == '\0') 134 continue; 135 if (p[0] == 'n' && p[1] == 'o') { 136 clear = 1; 137 p += 2; 138 } 139 switch (p[0]) { 140 case 'a': 141 TEST(p, "arch", SF_ARCHIVED); 142 TEST(p, "archived", SF_ARCHIVED); 143 return (1); 144 case 'd': 145 clear = !clear; 146 TEST(p, "dump", UF_NODUMP); 147 return (1); 148 case 'n': 149 /* 150 * Support `nonodump'. Note that 151 * the state of clear is not changed. 152 */ 153 TEST(p, "nodump", UF_NODUMP); 154 return (1); 155 case 'o': 156 TEST(p, "opaque", UF_OPAQUE); 157 return (1); 158 case 's': 159 TEST(p, "sappnd", SF_APPEND); 160 TEST(p, "sappend", SF_APPEND); 161 TEST(p, "schg", SF_IMMUTABLE); 162 TEST(p, "schange", SF_IMMUTABLE); 163 TEST(p, "simmutable", SF_IMMUTABLE); 164 return (1); 165 case 'u': 166 TEST(p, "uappnd", UF_APPEND); 167 TEST(p, "uappend", UF_APPEND); 168 TEST(p, "uchg", UF_IMMUTABLE); 169 TEST(p, "uchange", UF_IMMUTABLE); 170 TEST(p, "uimmutable", UF_IMMUTABLE); 171 return (1); 172 default: 173 return (1); 174 } 175 } 176 #endif 177 178 return (0); 179 } 180