1 /* $OpenBSD: strtofflags.c,v 1.1 2000/07/19 19:26:04 mickey Exp $ */ 2 3 /*- 4 * Copyright (c) 1993 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 #if 0 38 static char sccsid[] = "@(#)stat_flags.c 8.1 (Berkeley) 5/31/93"; 39 static const char rcsid[] = 40 "$FreeBSD: src/lib/libc/gen/strtofflags.c,v 1.18 2000/06/17 11:09:24 joe Exp $"; 41 #else 42 static const char rcsid[] = 43 "$OpenBSD: strtofflags.c,v 1.1 2000/07/19 19:26:04 mickey Exp $"; 44 #endif 45 #endif /* not lint */ 46 47 #include <sys/types.h> 48 #include <sys/stat.h> 49 50 #include <stddef.h> 51 #include <stdlib.h> 52 #include <string.h> 53 54 static const struct { 55 char *name; 56 u_int32_t flag; 57 int invert; 58 } mapping[] = { 59 /* shorter names per flag first, all prefixed by "no" */ 60 { "nosappnd", SF_APPEND, 0 }, 61 { "nosappend", SF_APPEND, 0 }, 62 { "noarch", SF_ARCHIVED, 0 }, 63 { "noarchived", SF_ARCHIVED, 0 }, 64 { "noschg", SF_IMMUTABLE, 0 }, 65 { "noschange", SF_IMMUTABLE, 0 }, 66 { "nosimmutable", SF_IMMUTABLE, 0 }, 67 #ifdef __FreeBSD__ 68 { "nosunlnk", SF_NOUNLINK, 0 }, 69 { "nosunlink", SF_NOUNLINK, 0 }, 70 #endif 71 { "nouappnd", UF_APPEND, 0 }, 72 { "nouappend", UF_APPEND, 0 }, 73 { "nouchg", UF_IMMUTABLE, 0 }, 74 { "nouchange", UF_IMMUTABLE, 0 }, 75 { "nouimmutable", UF_IMMUTABLE, 0 }, 76 { "nodump", UF_NODUMP, 1 }, 77 { "noopaque", UF_OPAQUE, 0 }, 78 #ifdef __FreeBSD__ 79 { "nouunlnk", UF_NOUNLINK, 0 }, 80 { "nouunlink", UF_NOUNLINK, 0 } 81 #endif 82 }; 83 #define longestflaglen 12 84 #define nmappings (sizeof(mapping) / sizeof(mapping[0])) 85 86 /* 87 * fflagstostr -- 88 * Convert file flags to a comma-separated string. If no flags 89 * are set, return the empty string. 90 */ 91 char * 92 fflagstostr(flags) 93 u_int32_t flags; 94 { 95 char *string; 96 char *sp, *dp; 97 u_int32_t setflags; 98 int i; 99 100 if ((string = (char *)malloc(nmappings * (longestflaglen + 1))) == NULL) 101 return (NULL); 102 103 setflags = flags; 104 dp = string; 105 for (i = 0; i < nmappings; i++) { 106 if (setflags & mapping[i].flag) { 107 if (dp > string) 108 *dp++ = ','; 109 for (sp = mapping[i].invert ? mapping[i].name : 110 mapping[i].name + 2; *sp; *dp++ = *sp++) ; 111 setflags &= ~mapping[i].flag; 112 } 113 } 114 *dp = '\0'; 115 return (string); 116 } 117 118 /* 119 * strtofflags -- 120 * Take string of arguments and return file flags. Return 0 on 121 * success, 1 on failure. On failure, stringp is set to point 122 * to the offending token. 123 */ 124 int 125 strtofflags(stringp, setp, clrp) 126 char **stringp; 127 u_int32_t *setp, *clrp; 128 { 129 char *string, *p; 130 int i; 131 132 if (setp) 133 *setp = 0; 134 if (clrp) 135 *clrp = 0; 136 string = *stringp; 137 while ((p = strsep(&string, "\t ,")) != NULL) { 138 *stringp = p; 139 if (*p == '\0') 140 continue; 141 for (i = 0; i < nmappings; i++) { 142 if (strcmp(p, mapping[i].name + 2) == 0) { 143 if (mapping[i].invert) { 144 if (clrp) 145 *clrp |= mapping[i].flag; 146 } else { 147 if (setp) 148 *setp |= mapping[i].flag; 149 } 150 break; 151 } else if (strcmp(p, mapping[i].name) == 0) { 152 if (mapping[i].invert) { 153 if (setp) 154 *setp |= mapping[i].flag; 155 } else { 156 if (clrp) 157 *clrp |= mapping[i].flag; 158 } 159 break; 160 } 161 } 162 if (i == nmappings) 163 return 1; 164 } 165 return 0; 166 } 167