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 #ifdef __DragonFly__ 34 #define HAVE_STRUCT_STAT_ST_FLAGS 1 35 #endif 36 37 #include <sys/types.h> 38 #include <sys/stat.h> 39 #include <fts.h> 40 #include <stddef.h> 41 #include <stdio.h> 42 #include <string.h> 43 #include <stdlib.h> 44 45 #include "extern.h" 46 47 #define SAPPEND(s) do { \ 48 if (prefix != NULL) \ 49 (void)strlcat(string, prefix, sizeof(string)); \ 50 (void)strlcat(string, s, sizeof(string)); \ 51 prefix = ","; \ 52 } while (/* CONSTCOND */ 0) 53 54 /* 55 * flags_to_string -- 56 * Convert stat flags to a comma-separated string. If no flags 57 * are set, return the default string. 58 */ 59 char * 60 flags_to_string(u_long flags, const char *def) 61 { 62 char string[128]; 63 const char *prefix; 64 65 string[0] = '\0'; 66 prefix = NULL; 67 #if HAVE_STRUCT_STAT_ST_FLAGS 68 if (flags & UF_APPEND) 69 SAPPEND("uappnd"); 70 if (flags & UF_IMMUTABLE) 71 SAPPEND("uchg"); 72 if (flags & UF_NODUMP) 73 SAPPEND("nodump"); 74 if (flags & UF_OPAQUE) 75 SAPPEND("opaque"); 76 if (flags & SF_APPEND) 77 SAPPEND("sappnd"); 78 if (flags & SF_ARCHIVED) 79 SAPPEND("arch"); 80 if (flags & SF_IMMUTABLE) 81 SAPPEND("schg"); 82 #ifdef SF_SNAPSHOT 83 if (flags & SF_SNAPSHOT) 84 SAPPEND("snap"); 85 #endif 86 #endif 87 if (prefix != NULL) 88 return strdup(string); 89 return strdup(def); 90 } 91 92 #define TEST(a, b, f) { \ 93 if (!strcmp(a, b)) { \ 94 if (clear) { \ 95 if (clrp) \ 96 *clrp |= (f); \ 97 if (setp) \ 98 *setp &= ~(f); \ 99 } else { \ 100 if (setp) \ 101 *setp |= (f); \ 102 if (clrp) \ 103 *clrp &= ~(f); \ 104 } \ 105 break; \ 106 } \ 107 } 108 109 /* 110 * string_to_flags -- 111 * Take string of arguments and return stat flags. Return 0 on 112 * success, 1 on failure. On failure, stringp is set to point 113 * to the offending token. 114 */ 115 int 116 string_to_flags(char **stringp, u_long *setp, u_long *clrp) 117 { 118 #if HAVE_STRUCT_STAT_ST_FLAGS 119 int clear; 120 char *string, *p; 121 #endif 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