strtofflags.c revision 61737
10Sstevel@tonic-gate/*- 20Sstevel@tonic-gate * Copyright (c) 1993 30Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 60Sstevel@tonic-gate * modification, are permitted provided that the following conditions 70Sstevel@tonic-gate * are met: 80Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 90Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 100Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 110Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 120Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 130Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 140Sstevel@tonic-gate * must display the following acknowledgement: 150Sstevel@tonic-gate * This product includes software developed by the University of 160Sstevel@tonic-gate * California, Berkeley and its contributors. 170Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 180Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 190Sstevel@tonic-gate * without specific prior written permission. 200Sstevel@tonic-gate * 210Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 220Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 230Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 240Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 250Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 260Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 270Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 280Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 290Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 300Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 310Sstevel@tonic-gate * SUCH DAMAGE. 320Sstevel@tonic-gate */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate#ifndef lint 350Sstevel@tonic-gate#if 0 360Sstevel@tonic-gatestatic char sccsid[] = "@(#)stat_flags.c 8.1 (Berkeley) 5/31/93"; 370Sstevel@tonic-gate#else 380Sstevel@tonic-gatestatic const char rcsid[] = 390Sstevel@tonic-gate "$FreeBSD: head/lib/libc/gen/strtofflags.c 61737 2000-06-17 01:28:13Z joe $"; 400Sstevel@tonic-gate#endif 410Sstevel@tonic-gate#endif /* not lint */ 420Sstevel@tonic-gate 430Sstevel@tonic-gate#include <sys/types.h> 440Sstevel@tonic-gate#include <sys/stat.h> 450Sstevel@tonic-gate 460Sstevel@tonic-gate#include <stddef.h> 470Sstevel@tonic-gate#include <string.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gatestatic struct { 500Sstevel@tonic-gate char *name; 510Sstevel@tonic-gate u_long flag; 520Sstevel@tonic-gate int invert; 530Sstevel@tonic-gate} mapping[] = { 540Sstevel@tonic-gate /* shorter names per flag first, all prefixed by "no" */ 550Sstevel@tonic-gate { "nosappnd", SF_APPEND, 0 }, 560Sstevel@tonic-gate { "nosappend", SF_APPEND, 0 }, 570Sstevel@tonic-gate { "noarch", SF_ARCHIVED, 0 }, 580Sstevel@tonic-gate { "noarchived", SF_ARCHIVED, 0 }, 590Sstevel@tonic-gate { "noschg", SF_IMMUTABLE, 0 }, 600Sstevel@tonic-gate { "noschange", SF_IMMUTABLE, 0 }, 610Sstevel@tonic-gate { "nosimmutable", SF_IMMUTABLE, 0 }, 620Sstevel@tonic-gate { "nosunlnk", SF_NOUNLINK, 0 }, 630Sstevel@tonic-gate { "nosunlink", SF_NOUNLINK, 0 }, 640Sstevel@tonic-gate { "nouappnd", UF_APPEND, 0 }, 650Sstevel@tonic-gate { "nouappend", UF_APPEND, 0 }, 660Sstevel@tonic-gate { "nouchg", UF_IMMUTABLE, 0 }, 670Sstevel@tonic-gate { "nouchange", UF_IMMUTABLE, 0 }, 680Sstevel@tonic-gate { "nouimmutable", UF_IMMUTABLE, 0 }, 690Sstevel@tonic-gate { "nodump", UF_NODUMP, 1 }, 700Sstevel@tonic-gate { "noopaque", UF_OPAQUE, 0 }, 710Sstevel@tonic-gate { "nouunlnk", UF_NOUNLINK, 0 }, 720Sstevel@tonic-gate { "nouunlink", UF_NOUNLINK, 0 } 730Sstevel@tonic-gate}; 740Sstevel@tonic-gate#define nmappings (sizeof(mapping) / sizeof(mapping[0])) 752139Sjp161948 762139Sjp161948/* 772139Sjp161948 * fflagstostr -- 782139Sjp161948 * Convert file flags to a comma-separated string. If no flags 790Sstevel@tonic-gate * are set, return the default string. 80 */ 81char * 82fflagstostr(flags, def) 83 u_long flags; 84 char *def; 85{ 86 static char string[128]; 87 char *sp, *dp; 88 u_long setflags; 89 int i; 90 91 setflags = flags; 92 dp = string; 93 for (i = 0; i < nmappings; i++) { 94 if (setflags & mapping[i].flag) { 95 if (dp > string) 96 *dp++ = ','; 97 for (sp = mapping[i].invert ? mapping[i].name : 98 mapping[i].name + 2; *sp; *dp++ = *sp++) ; 99 setflags &= ~mapping[i].flag; 100 } 101 } 102 *dp = '\0'; 103 return (dp == string && def != NULL ? def : string); 104} 105 106/* 107 * strtofflags -- 108 * Take string of arguments and return file flags. Return 0 on 109 * success, 1 on failure. On failure, stringp is set to point 110 * to the offending token. 111 */ 112int 113strtofflags(stringp, setp, clrp) 114 char **stringp; 115 u_long *setp, *clrp; 116{ 117 char *string, *p; 118 int i; 119 120 if (setp) 121 *setp = 0; 122 if (clrp) 123 *clrp = 0; 124 string = *stringp; 125 while ((p = strsep(&string, "\t ,")) != NULL) { 126 *stringp = p; 127 if (*p == '\0') 128 continue; 129 for (i = 0; i < nmappings; i++) { 130 if (strcmp(p, mapping[i].name + 2) == 0) { 131 if (mapping[i].invert) { 132 if (clrp) 133 *clrp |= mapping[i].flag; 134 } else { 135 if (setp) 136 *setp |= mapping[i].flag; 137 } 138 break; 139 } else if (strcmp(p, mapping[i].name) == 0) { 140 if (mapping[i].invert) { 141 if (setp) 142 *setp |= mapping[i].flag; 143 } else { 144 if (clrp) 145 *clrp |= mapping[i].flag; 146 } 147 break; 148 } 149 } 150 if (i == nmappings) 151 return 1; 152 } 153 return 0; 154} 155