xref: /dragonfly/usr.sbin/mtree/stat_flags.c (revision 8e11cefe)
1 /*	@(#)stat_flags.c	8.2 (Berkeley) 7/28/94	*/
2 /*	$NetBSD: stat_flags.c,v 1.2 2007/01/16 17:34:02 cbiere Exp $	*/
3 
4 /*-
5  * Copyright (c) 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #define HAVE_STRUCT_STAT_ST_FLAGS 1
34 
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fts.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 
43 #include "extern.h"
44 
45 #define	SAPPEND(s) do {							\
46 	if (prefix != NULL)						\
47 		(void)strlcat(string, prefix, sizeof(string));		\
48 	(void)strlcat(string, s, sizeof(string));			\
49 	prefix = ",";							\
50 } while (/* CONSTCOND */ 0)
51 
52 /*
53  * flags_to_string --
54  *	Convert stat flags to a comma-separated string.  If no flags
55  *	are set, return the default string.
56  */
57 char *
58 flags_to_string(u_long flags, const char *def)
59 {
60 	char string[128];
61 	const char *prefix;
62 
63 	string[0] = '\0';
64 	prefix = NULL;
65 #if HAVE_STRUCT_STAT_ST_FLAGS
66 	if (flags & UF_APPEND)
67 		SAPPEND("uappnd");
68 	if (flags & UF_IMMUTABLE)
69 		SAPPEND("uchg");
70 	if (flags & UF_NODUMP)
71 		SAPPEND("nodump");
72 	if (flags & UF_OPAQUE)
73 		SAPPEND("opaque");
74 	if (flags & SF_APPEND)
75 		SAPPEND("sappnd");
76 	if (flags & SF_ARCHIVED)
77 		SAPPEND("arch");
78 	if (flags & SF_IMMUTABLE)
79 		SAPPEND("schg");
80 #ifdef SF_SNAPSHOT
81 	if (flags & SF_SNAPSHOT)
82 		SAPPEND("snap");
83 #endif
84 #endif
85 	if (prefix != NULL)
86 		return strdup(string);
87 	return strdup(def);
88 }
89 
90 #define	TEST(a, b, f) {							\
91 	if (!strcmp(a, b)) {						\
92 		if (clear) {						\
93 			if (clrp)					\
94 				*clrp |= (f);				\
95 			if (setp)					\
96 				*setp &= ~(f);				\
97 		} else {						\
98 			if (setp)					\
99 				*setp |= (f);				\
100 			if (clrp)					\
101 				*clrp &= ~(f);				\
102 		}							\
103 		break;							\
104 	}								\
105 }
106 
107 /*
108  * string_to_flags --
109  *	Take string of arguments and return stat flags.  Return 0 on
110  *	success, 1 on failure.  On failure, stringp is set to point
111  *	to the offending token.
112  */
113 int
114 string_to_flags(char **stringp, u_long *setp, u_long *clrp)
115 {
116 	int clear;
117 	char *string, *p;
118 
119 	if (setp)
120 		*setp = 0;
121 	if (clrp)
122 		*clrp = 0;
123 
124 #if HAVE_STRUCT_STAT_ST_FLAGS
125 	string = *stringp;
126 	while ((p = strsep(&string, "\t ,")) != NULL) {
127 		clear = 0;
128 		*stringp = p;
129 		if (*p == '\0')
130 			continue;
131 		if (p[0] == 'n' && p[1] == 'o') {
132 			clear = 1;
133 			p += 2;
134 		}
135 		switch (p[0]) {
136 		case 'a':
137 			TEST(p, "arch", SF_ARCHIVED);
138 			TEST(p, "archived", SF_ARCHIVED);
139 			return (1);
140 		case 'd':
141 			clear = !clear;
142 			TEST(p, "dump", UF_NODUMP);
143 			return (1);
144 		case 'n':
145 				/*
146 				 * Support `nonodump'. Note that
147 				 * the state of clear is not changed.
148 				 */
149 			TEST(p, "nodump", UF_NODUMP);
150 			return (1);
151 		case 'o':
152 			TEST(p, "opaque", UF_OPAQUE);
153 			return (1);
154 		case 's':
155 			TEST(p, "sappnd", SF_APPEND);
156 			TEST(p, "sappend", SF_APPEND);
157 			TEST(p, "schg", SF_IMMUTABLE);
158 			TEST(p, "schange", SF_IMMUTABLE);
159 			TEST(p, "simmutable", SF_IMMUTABLE);
160 			return (1);
161 		case 'u':
162 			TEST(p, "uappnd", UF_APPEND);
163 			TEST(p, "uappend", UF_APPEND);
164 			TEST(p, "uchg", UF_IMMUTABLE);
165 			TEST(p, "uchange", UF_IMMUTABLE);
166 			TEST(p, "uimmutable", UF_IMMUTABLE);
167 			return (1);
168 		default:
169 			return (1);
170 		}
171 	}
172 #endif
173 
174 	return (0);
175 }
176