xref: /freebsd/lib/libc/gen/strtofflags.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
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 #if 0
36 static char sccsid[] = "@(#)stat_flags.c	8.1 (Berkeley) 5/31/93";
37 #else
38 static const char rcsid[] =
39 	"$Id: stat_flags.c,v 1.7 1997/08/07 15:33:49 steve 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 <string.h>
48 
49 #define	SAPPEND(s) {							\
50 	if (prefix != NULL)						\
51 		(void)strcat(string, prefix);				\
52 	(void)strcat(string, s);					\
53 	prefix = ",";							\
54 }
55 
56 /*
57  * flags_to_string --
58  *	Convert stat flags to a comma-separated string.  If no flags
59  *	are set, return the default string.
60  */
61 char *
62 flags_to_string(flags, def)
63 	u_long flags;
64 	char *def;
65 {
66 	static char string[128];
67 	char *prefix;
68 
69 	string[0] = '\0';
70 	prefix = NULL;
71 	if (flags & UF_APPEND)
72 		SAPPEND("uappnd");
73 	if (flags & UF_IMMUTABLE)
74 		SAPPEND("uchg");
75 	if (flags & UF_NOUNLINK)
76 		SAPPEND("uunlnk");
77 	if (flags & UF_NODUMP)
78 		SAPPEND("nodump");
79 	if (flags & UF_OPAQUE)
80 		SAPPEND("opaque");
81 	if (flags & SF_APPEND)
82 		SAPPEND("sappnd");
83 	if (flags & SF_ARCHIVED)
84 		SAPPEND("arch");
85 	if (flags & SF_IMMUTABLE)
86 		SAPPEND("schg");
87 	if (flags & SF_NOUNLINK)
88 		SAPPEND("sunlnk");
89 	return (prefix == NULL && def != NULL ? def : string);
90 }
91 
92 #define	TEST(a, b, f) {							\
93 	if (!memcmp(a, b, sizeof(b))) {					\
94 		if (clear) {						\
95 			if (clrp)					\
96 				*clrp |= (f);				\
97 		} else if (setp)					\
98 			*setp |= (f);					\
99 		break;							\
100 	}								\
101 }
102 
103 /*
104  * string_to_flags --
105  *	Take string of arguments and return stat flags.  Return 0 on
106  *	success, 1 on failure.  On failure, stringp is set to point
107  *	to the offending token.
108  */
109 int
110 string_to_flags(stringp, setp, clrp)
111 	char **stringp;
112 	u_long *setp, *clrp;
113 {
114 	int clear;
115 	char *string, *p;
116 
117 	clear = 0;
118 	if (setp)
119 		*setp = 0;
120 	if (clrp)
121 		*clrp = 0;
122 	string = *stringp;
123 	while ((p = strsep(&string, "\t ,")) != NULL) {
124 		*stringp = p;
125 		if (*p == '\0')
126 			continue;
127 		if (p[0] == 'n' && p[1] == 'o') {
128 			clear = 1;
129 			p += 2;
130 		}
131 		switch (p[0]) {
132 		case 'a':
133 			TEST(p, "arch", SF_ARCHIVED);
134 			TEST(p, "archived", SF_ARCHIVED);
135 			return (1);
136 		case 'd':
137 			clear = !clear;
138 			TEST(p, "dump", UF_NODUMP);
139 			return (1);
140 		case 'o':
141 			TEST(p, "opaque", UF_OPAQUE);
142  			return (1);
143 		case 's':
144 			TEST(p, "sappnd", SF_APPEND);
145 			TEST(p, "sappend", SF_APPEND);
146 			TEST(p, "schg", SF_IMMUTABLE);
147 			TEST(p, "schange", SF_IMMUTABLE);
148 			TEST(p, "simmutable", SF_IMMUTABLE);
149 			TEST(p, "sunlnk", SF_NOUNLINK);
150 			TEST(p, "sunlink", SF_NOUNLINK);
151 			return (1);
152 		case 'u':
153 			TEST(p, "uappnd", UF_APPEND);
154 			TEST(p, "uappend", UF_APPEND);
155 			TEST(p, "uchg", UF_IMMUTABLE);
156 			TEST(p, "uchange", UF_IMMUTABLE);
157 			TEST(p, "uimmutable", UF_IMMUTABLE);
158 			TEST(p, "uunlnk", UF_NOUNLINK);
159 			TEST(p, "uunlink", UF_NOUNLINK);
160 			/* FALLTHROUGH */
161 		default:
162 			return (1);
163 		}
164 	}
165 	return (0);
166 }
167