file_cmds-82.tar.gz
[apple/file_cmds.git] / ls / stat_flags.c
1 /* $NetBSD: stat_flags.c,v 1.6 1997/07/20 18:53:12 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)stat_flags.c 8.2 (Berkeley) 7/28/94";
40 #else
41 __RCSID("$NetBSD: stat_flags.c,v 1.6 1997/07/20 18:53:12 christos Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47
48 #include <stddef.h>
49 #include <string.h>
50 #include <fts.h>
51
52 #include "ls.h"
53 #include "extern.h"
54
55 #define SAPPEND(s) { \
56 if (prefix != NULL) \
57 (void)strcat(string, prefix); \
58 (void)strcat(string, s); \
59 prefix = ","; \
60 }
61
62 /*
63 * flags_to_string --
64 * Convert stat flags to a comma-separated string. If no flags
65 * are set, return the default string.
66 */
67 char *
68 flags_to_string(flags, def)
69 u_long flags;
70 char *def;
71 {
72 static char string[128];
73 char *prefix;
74
75 string[0] = '\0';
76 prefix = NULL;
77 if (flags & UF_APPEND)
78 SAPPEND("uappnd");
79 if (flags & UF_IMMUTABLE)
80 SAPPEND("uchg");
81 if (flags & UF_NODUMP)
82 SAPPEND("nodump");
83 if (flags & UF_OPAQUE)
84 SAPPEND("opaque");
85 if (flags & SF_APPEND)
86 SAPPEND("sappnd");
87 if (flags & SF_ARCHIVED)
88 SAPPEND("arch");
89 if (flags & SF_IMMUTABLE)
90 SAPPEND("schg");
91 return (prefix == NULL && def != NULL ? def : string);
92 }
93
94 #define TEST(a, b, f) { \
95 if (!memcmp(a, b, sizeof(b))) { \
96 if (clear) { \
97 if (clrp) \
98 *clrp |= (f); \
99 } else if (setp) \
100 *setp |= (f); \
101 break; \
102 } \
103 }
104
105 /*
106 * string_to_flags --
107 * Take string of arguments and return stat flags. Return 0 on
108 * success, 1 on failure. On failure, stringp is set to point
109 * to the offending token.
110 */
111 int
112 string_to_flags(stringp, setp, clrp)
113 char **stringp;
114 u_long *setp, *clrp;
115 {
116 int clear;
117 char *string, *p;
118
119 clear = 0;
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 if (p[0] == 'n' && p[1] == 'o') {
130 clear = 1;
131 p += 2;
132 }
133 switch (p[0]) {
134 case 'a':
135 TEST(p, "arch", SF_ARCHIVED);
136 TEST(p, "archived", SF_ARCHIVED);
137 return (1);
138 case 'd':
139 clear = !clear;
140 TEST(p, "dump", UF_NODUMP);
141 return (1);
142 case 'o':
143 TEST(p, "opaque", UF_OPAQUE);
144 return (1);
145 case 's':
146 TEST(p, "sappnd", SF_APPEND);
147 TEST(p, "sappend", SF_APPEND);
148 TEST(p, "schg", SF_IMMUTABLE);
149 TEST(p, "schange", SF_IMMUTABLE);
150 TEST(p, "simmutable", SF_IMMUTABLE);
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 /* FALLTHROUGH */
159 default:
160 return (1);
161 }
162 }
163 return (0);
164 }