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