1 /*- 2 * Copyright (c) 1993 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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char sccsid[] = "@(#)stat_flags.c 8.1 (Berkeley) 5/31/93"; 36 #endif /* not lint */ 37 38 #include <sys/types.h> 39 #include <sys/stat.h> 40 41 #include <stddef.h> 42 #include <string.h> 43 44 #define SAPPEND(s) { \ 45 if (prefix != NULL) \ 46 (void)strcat(string, prefix); \ 47 (void)strcat(string, s); \ 48 prefix = ","; \ 49 } 50 51 /* 52 * flags_to_string -- 53 * Convert stat flags to a comma-separated string. If no flags 54 * are set, return the default string. 55 */ 56 char * 57 flags_to_string(flags, def) 58 u_long flags; 59 char *def; 60 { 61 static char string[128]; 62 char *prefix; 63 64 string[0] = '\0'; 65 prefix = NULL; 66 if (flags & UF_APPEND) 67 SAPPEND("uappnd"); 68 if (flags & UF_IMMUTABLE) 69 SAPPEND("uchg"); 70 if (flags & UF_NODUMP) 71 SAPPEND("nodump"); 72 if (flags & SF_APPEND) 73 SAPPEND("sappnd"); 74 if (flags & SF_ARCHIVED) 75 SAPPEND("arch"); 76 if (flags & SF_IMMUTABLE) 77 SAPPEND("schg"); 78 return (prefix == NULL && def != NULL ? def : string); 79 } 80 81 #define TEST(a, b, f) { \ 82 if (!memcmp(a, b, sizeof(b))) { \ 83 if (clear) { \ 84 if (clrp) \ 85 *clrp |= (f); \ 86 } else if (setp) \ 87 *setp |= (f); \ 88 break; \ 89 } \ 90 } 91 92 /* 93 * string_to_flags -- 94 * Take string of arguments and return stat flags. Return 0 on 95 * success, 1 on failure. On failure, stringp is set to point 96 * to the offending token. 97 */ 98 int 99 string_to_flags(stringp, setp, clrp) 100 char **stringp; 101 u_long *setp, *clrp; 102 { 103 int clear; 104 char *string, *p; 105 106 clear = 0; 107 if (setp) 108 *setp = 0; 109 if (clrp) 110 *clrp = 0; 111 string = *stringp; 112 while ((p = strsep(&string, "\t ,")) != NULL) { 113 *stringp = p; 114 if (*p == '\0') 115 continue; 116 if (p[0] == 'n' && p[1] == 'o') { 117 clear = 1; 118 p += 2; 119 } 120 switch (p[0]) { 121 case 'a': 122 TEST(p, "arch", SF_ARCHIVED); 123 TEST(p, "archived", SF_ARCHIVED); 124 return (1); 125 case 'd': 126 clear = !clear; 127 TEST(p, "dump", UF_NODUMP); 128 return (1); 129 case 's': 130 TEST(p, "sappnd", SF_APPEND); 131 TEST(p, "sappend", SF_APPEND); 132 TEST(p, "schg", SF_IMMUTABLE); 133 TEST(p, "schange", SF_IMMUTABLE); 134 TEST(p, "simmutable", SF_IMMUTABLE); 135 return (1); 136 case 'u': 137 TEST(p, "uappnd", UF_APPEND); 138 TEST(p, "uappend", UF_APPEND); 139 TEST(p, "uchg", UF_IMMUTABLE); 140 TEST(p, "uchange", UF_IMMUTABLE); 141 TEST(p, "uimmutable", UF_IMMUTABLE); 142 /* FALLTHROUGH */ 143 default: 144 return (1); 145 } 146 } 147 return (0); 148 } 149