1 |
|
|
/* $OpenBSD: strtofflags.c,v 1.7 2015/08/20 21:49:29 deraadt 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. Neither the name of the University nor the names of its contributors |
16 |
|
|
* may be used to endorse or promote products derived from this software |
17 |
|
|
* without specific prior written permission. |
18 |
|
|
* |
19 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
20 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
23 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 |
|
|
* SUCH DAMAGE. |
30 |
|
|
*/ |
31 |
|
|
|
32 |
|
|
#include <sys/types.h> |
33 |
|
|
#include <sys/stat.h> |
34 |
|
|
|
35 |
|
|
#include <stddef.h> |
36 |
|
|
#include <stdlib.h> |
37 |
|
|
#include <unistd.h> |
38 |
|
|
#include <string.h> |
39 |
|
|
|
40 |
|
|
static const struct { |
41 |
|
|
char *name; |
42 |
|
|
u_int32_t flag; |
43 |
|
|
int invert; |
44 |
|
|
} mapping[] = { |
45 |
|
|
/* shorter names per flag first, all prefixed by "no" */ |
46 |
|
|
{ "nosappnd", SF_APPEND, 0 }, |
47 |
|
|
{ "nosappend", SF_APPEND, 0 }, |
48 |
|
|
{ "noarch", SF_ARCHIVED, 0 }, |
49 |
|
|
{ "noarchived", SF_ARCHIVED, 0 }, |
50 |
|
|
{ "noschg", SF_IMMUTABLE, 0 }, |
51 |
|
|
{ "noschange", SF_IMMUTABLE, 0 }, |
52 |
|
|
{ "nosimmutable", SF_IMMUTABLE, 0 }, |
53 |
|
|
#ifdef __FreeBSD__ |
54 |
|
|
{ "nosunlnk", SF_NOUNLINK, 0 }, |
55 |
|
|
{ "nosunlink", SF_NOUNLINK, 0 }, |
56 |
|
|
#endif |
57 |
|
|
{ "nouappnd", UF_APPEND, 0 }, |
58 |
|
|
{ "nouappend", UF_APPEND, 0 }, |
59 |
|
|
{ "nouchg", UF_IMMUTABLE, 0 }, |
60 |
|
|
{ "nouchange", UF_IMMUTABLE, 0 }, |
61 |
|
|
{ "nouimmutable", UF_IMMUTABLE, 0 }, |
62 |
|
|
{ "nodump", UF_NODUMP, 1 }, |
63 |
|
|
{ "noopaque", UF_OPAQUE, 0 }, |
64 |
|
|
#ifdef __FreeBSD__ |
65 |
|
|
{ "nouunlnk", UF_NOUNLINK, 0 }, |
66 |
|
|
{ "nouunlink", UF_NOUNLINK, 0 } |
67 |
|
|
#endif |
68 |
|
|
}; |
69 |
|
|
#define longestflaglen 12 |
70 |
|
|
#define nmappings (sizeof(mapping) / sizeof(mapping[0])) |
71 |
|
|
|
72 |
|
|
/* |
73 |
|
|
* fflagstostr -- |
74 |
|
|
* Convert file flags to a comma-separated string. If no flags |
75 |
|
|
* are set, return the empty string. |
76 |
|
|
*/ |
77 |
|
|
char * |
78 |
|
|
fflagstostr(u_int32_t flags) |
79 |
|
|
{ |
80 |
|
|
char *string; |
81 |
|
|
char *sp, *dp; |
82 |
|
|
u_int32_t setflags; |
83 |
|
|
int i; |
84 |
|
|
|
85 |
|
|
if ((string = calloc(nmappings, longestflaglen + 1)) == NULL) |
86 |
|
|
return (NULL); |
87 |
|
|
|
88 |
|
|
setflags = flags; |
89 |
|
|
dp = string; |
90 |
|
|
for (i = 0; i < nmappings; i++) { |
91 |
|
|
if (setflags & mapping[i].flag) { |
92 |
|
|
if (dp > string) |
93 |
|
|
*dp++ = ','; |
94 |
|
|
for (sp = mapping[i].invert ? mapping[i].name : |
95 |
|
|
mapping[i].name + 2; *sp; *dp++ = *sp++) ; |
96 |
|
|
setflags &= ~mapping[i].flag; |
97 |
|
|
} |
98 |
|
|
} |
99 |
|
|
*dp = '\0'; |
100 |
|
|
return (string); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
/* |
104 |
|
|
* strtofflags -- |
105 |
|
|
* Take string of arguments and return file 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 |
|
|
strtofflags(char **stringp, u_int32_t *setp, u_int32_t *clrp) |
111 |
|
|
{ |
112 |
|
380 |
char *string, *p; |
113 |
|
|
int i; |
114 |
|
|
|
115 |
✓✗ |
190 |
if (setp) |
116 |
|
190 |
*setp = 0; |
117 |
✓✗ |
190 |
if (clrp) |
118 |
|
190 |
*clrp = 0; |
119 |
|
190 |
string = *stringp; |
120 |
✓✓ |
560 |
while ((p = strsep(&string, "\t ,")) != NULL) { |
121 |
|
190 |
*stringp = p; |
122 |
✗✓ |
190 |
if (*p == '\0') |
123 |
|
|
continue; |
124 |
✓✓ |
4860 |
for (i = 0; i < nmappings; i++) { |
125 |
✓✓ |
2420 |
if (strcmp(p, mapping[i].name + 2) == 0) { |
126 |
✓✓ |
100 |
if (mapping[i].invert) { |
127 |
✓✗ |
90 |
if (clrp) |
128 |
|
90 |
*clrp |= mapping[i].flag; |
129 |
|
|
} else { |
130 |
✓✗ |
10 |
if (setp) |
131 |
|
10 |
*setp |= mapping[i].flag; |
132 |
|
|
} |
133 |
|
|
break; |
134 |
✓✓ |
2320 |
} else if (strcmp(p, mapping[i].name) == 0) { |
135 |
✓✓ |
80 |
if (mapping[i].invert) { |
136 |
✓✗ |
70 |
if (setp) |
137 |
|
70 |
*setp |= mapping[i].flag; |
138 |
|
|
} else { |
139 |
✓✗ |
10 |
if (clrp) |
140 |
|
10 |
*clrp |= mapping[i].flag; |
141 |
|
|
} |
142 |
|
|
break; |
143 |
|
|
} |
144 |
|
|
} |
145 |
✓✓ |
190 |
if (i == nmappings) |
146 |
|
10 |
return 1; |
147 |
|
|
} |
148 |
|
180 |
return 0; |
149 |
|
190 |
} |