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