1 /* $OpenBSD: strtofflags.c,v 1.3 2003/06/25 21:15:40 deraadt 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 #if 0 34 static char sccsid[] = "@(#)stat_flags.c 8.1 (Berkeley) 5/31/93"; 35 static const char rcsid[] = 36 "$FreeBSD: src/lib/libc/gen/strtofflags.c,v 1.18 2000/06/17 11:09:24 joe Exp $"; 37 #else 38 static const char rcsid[] = 39 "$OpenBSD: strtofflags.c,v 1.3 2003/06/25 21:15:40 deraadt Exp $"; 40 #endif 41 #endif /* not lint */ 42 43 #include <sys/types.h> 44 #include <sys/stat.h> 45 46 #include <stddef.h> 47 #include <stdlib.h> 48 #include <unistd.h> 49 #include <string.h> 50 51 static const struct { 52 char *name; 53 u_int32_t flag; 54 int invert; 55 } mapping[] = { 56 /* shorter names per flag first, all prefixed by "no" */ 57 { "nosappnd", SF_APPEND, 0 }, 58 { "nosappend", SF_APPEND, 0 }, 59 { "noarch", SF_ARCHIVED, 0 }, 60 { "noarchived", SF_ARCHIVED, 0 }, 61 { "noschg", SF_IMMUTABLE, 0 }, 62 { "noschange", SF_IMMUTABLE, 0 }, 63 { "nosimmutable", SF_IMMUTABLE, 0 }, 64 #ifdef __FreeBSD__ 65 { "nosunlnk", SF_NOUNLINK, 0 }, 66 { "nosunlink", SF_NOUNLINK, 0 }, 67 #endif 68 { "nouappnd", UF_APPEND, 0 }, 69 { "nouappend", UF_APPEND, 0 }, 70 { "nouchg", UF_IMMUTABLE, 0 }, 71 { "nouchange", UF_IMMUTABLE, 0 }, 72 { "nouimmutable", UF_IMMUTABLE, 0 }, 73 { "nodump", UF_NODUMP, 1 }, 74 { "noopaque", UF_OPAQUE, 0 }, 75 #ifdef __FreeBSD__ 76 { "nouunlnk", UF_NOUNLINK, 0 }, 77 { "nouunlink", UF_NOUNLINK, 0 } 78 #endif 79 }; 80 #define longestflaglen 12 81 #define nmappings (sizeof(mapping) / sizeof(mapping[0])) 82 83 /* 84 * fflagstostr -- 85 * Convert file flags to a comma-separated string. If no flags 86 * are set, return the empty string. 87 */ 88 char * 89 fflagstostr(flags) 90 u_int32_t flags; 91 { 92 char *string; 93 char *sp, *dp; 94 u_int32_t setflags; 95 int i; 96 97 if ((string = (char *)malloc(nmappings * (longestflaglen + 1))) == NULL) 98 return (NULL); 99 100 setflags = flags; 101 dp = string; 102 for (i = 0; i < nmappings; i++) { 103 if (setflags & mapping[i].flag) { 104 if (dp > string) 105 *dp++ = ','; 106 for (sp = mapping[i].invert ? mapping[i].name : 107 mapping[i].name + 2; *sp; *dp++ = *sp++) ; 108 setflags &= ~mapping[i].flag; 109 } 110 } 111 *dp = '\0'; 112 return (string); 113 } 114 115 /* 116 * strtofflags -- 117 * Take string of arguments and return file flags. Return 0 on 118 * success, 1 on failure. On failure, stringp is set to point 119 * to the offending token. 120 */ 121 int 122 strtofflags(stringp, setp, clrp) 123 char **stringp; 124 u_int32_t *setp, *clrp; 125 { 126 char *string, *p; 127 int i; 128 129 if (setp) 130 *setp = 0; 131 if (clrp) 132 *clrp = 0; 133 string = *stringp; 134 while ((p = strsep(&string, "\t ,")) != NULL) { 135 *stringp = p; 136 if (*p == '\0') 137 continue; 138 for (i = 0; i < nmappings; i++) { 139 if (strcmp(p, mapping[i].name + 2) == 0) { 140 if (mapping[i].invert) { 141 if (clrp) 142 *clrp |= mapping[i].flag; 143 } else { 144 if (setp) 145 *setp |= mapping[i].flag; 146 } 147 break; 148 } else if (strcmp(p, mapping[i].name) == 0) { 149 if (mapping[i].invert) { 150 if (setp) 151 *setp |= mapping[i].flag; 152 } else { 153 if (clrp) 154 *clrp |= mapping[i].flag; 155 } 156 break; 157 } 158 } 159 if (i == nmappings) 160 return 1; 161 } 162 return 0; 163 } 164