misc.c revision 1.30.8.2 1 /* $NetBSD: misc.c,v 1.30.8.2 2013/01/16 05:34:09 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 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 * @(#)misc.c 8.1 (Berkeley) 6/6/93
32 */
33
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(lint)
40 __RCSID("$NetBSD: misc.c,v 1.30.8.2 2013/01/16 05:34:09 yamt Exp $");
41 #endif /* not lint */
42
43 #include <sys/types.h>
44 #include <sys/stat.h>
45
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50
51 #include "extern.h"
52
53 typedef struct _key {
54 const char *name; /* key name */
55 u_int val; /* value */
56
57 #define NEEDVALUE 0x01
58 u_int flags;
59 } KEY;
60
61 /* NB: the following tables must be sorted lexically. */
62 static KEY keylist[] = {
63 {"cksum", F_CKSUM, NEEDVALUE},
64 {"device", F_DEV, NEEDVALUE},
65 {"flags", F_FLAGS, NEEDVALUE},
66 {"gid", F_GID, NEEDVALUE},
67 {"gname", F_GNAME, NEEDVALUE},
68 {"ignore", F_IGN, 0},
69 {"link", F_SLINK, NEEDVALUE},
70 {"md5", F_MD5, NEEDVALUE},
71 {"md5digest", F_MD5, NEEDVALUE},
72 {"mode", F_MODE, NEEDVALUE},
73 {"nlink", F_NLINK, NEEDVALUE},
74 {"nochange", F_NOCHANGE, 0},
75 {"optional", F_OPT, 0},
76 {"ripemd160digest", F_RMD160, NEEDVALUE},
77 {"rmd160", F_RMD160, NEEDVALUE},
78 {"rmd160digest",F_RMD160, NEEDVALUE},
79 {"sha1", F_SHA1, NEEDVALUE},
80 {"sha1digest", F_SHA1, NEEDVALUE},
81 {"sha256", F_SHA256, NEEDVALUE},
82 {"sha256digest",F_SHA256, NEEDVALUE},
83 {"sha384", F_SHA384, NEEDVALUE},
84 {"sha384digest",F_SHA384, NEEDVALUE},
85 {"sha512", F_SHA512, NEEDVALUE},
86 {"sha512digest",F_SHA512, NEEDVALUE},
87 {"size", F_SIZE, NEEDVALUE},
88 {"tags", F_TAGS, NEEDVALUE},
89 {"time", F_TIME, NEEDVALUE},
90 {"type", F_TYPE, NEEDVALUE},
91 {"uid", F_UID, NEEDVALUE},
92 {"uname", F_UNAME, NEEDVALUE}
93 };
94
95 static KEY typelist[] = {
96 {"block", F_BLOCK, 0},
97 {"char", F_CHAR, 0},
98 {"dir", F_DIR, 0},
99 #ifdef S_IFDOOR
100 {"door", F_DOOR, 0},
101 #endif
102 {"fifo", F_FIFO, 0},
103 {"file", F_FILE, 0},
104 {"link", F_LINK, 0},
105 {"socket", F_SOCK, 0},
106 };
107
108 slist_t excludetags, includetags;
109 int keys = KEYDEFAULT;
110
111
112 int keycompare(const void *, const void *);
113
114 u_int
115 parsekey(const char *name, int *needvaluep)
116 {
117 static int allbits;
118 KEY *k, tmp;
119
120 if (allbits == 0) {
121 size_t i;
122
123 for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
124 allbits |= keylist[i].val;
125 }
126 tmp.name = name;
127 if (strcmp(name, "all") == 0)
128 return (allbits);
129 k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
130 sizeof(KEY), keycompare);
131 if (k == NULL)
132 mtree_err("unknown keyword `%s'", name);
133
134 if (needvaluep)
135 *needvaluep = k->flags & NEEDVALUE ? 1 : 0;
136
137 return (k->val);
138 }
139
140 u_int
141 parsetype(const char *name)
142 {
143 KEY *k, tmp;
144
145 tmp.name = name;
146 k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
147 sizeof(KEY), keycompare);
148 if (k == NULL)
149 mtree_err("unknown file type `%s'", name);
150
151 return (k->val);
152 }
153
154 int
155 keycompare(const void *a, const void *b)
156 {
157
158 return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
159 }
160
161 void
162 mtree_err(const char *fmt, ...)
163 {
164 va_list ap;
165
166 va_start(ap, fmt);
167 vwarnx(fmt, ap);
168 va_end(ap);
169 if (mtree_lineno)
170 warnx("failed at line %lu of the specification",
171 (u_long) mtree_lineno);
172 exit(1);
173 /* NOTREACHED */
174 }
175
176 void
177 addtag(slist_t *list, char *elem)
178 {
179
180 #define TAG_CHUNK 20
181
182 if ((list->count % TAG_CHUNK) == 0) {
183 char **new;
184
185 new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
186 * sizeof(char *));
187 if (new == NULL)
188 mtree_err("memory allocation error");
189 list->list = new;
190 }
191 list->list[list->count] = elem;
192 list->count++;
193 }
194
195 void
196 parsetags(slist_t *list, char *args)
197 {
198 char *p, *e;
199 int len;
200
201 if (args == NULL) {
202 addtag(list, NULL);
203 return;
204 }
205 while ((p = strsep(&args, ",")) != NULL) {
206 if (*p == '\0')
207 continue;
208 len = strlen(p) + 3; /* "," + p + ",\0" */
209 if ((e = malloc(len)) == NULL)
210 mtree_err("memory allocation error");
211 snprintf(e, len, ",%s,", p);
212 addtag(list, e);
213 }
214 }
215
216 /*
217 * matchtags
218 * returns 0 if there's a match from the exclude list in the node's tags,
219 * or there's an include list and no match.
220 * return 1 otherwise.
221 */
222 int
223 matchtags(NODE *node)
224 {
225 int i;
226
227 if (node->tags) {
228 for (i = 0; i < excludetags.count; i++)
229 if (strstr(node->tags, excludetags.list[i]))
230 break;
231 if (i < excludetags.count)
232 return (0);
233
234 for (i = 0; i < includetags.count; i++)
235 if (strstr(node->tags, includetags.list[i]))
236 break;
237 if (i > 0 && i == includetags.count)
238 return (0);
239 } else if (includetags.count > 0) {
240 return (0);
241 }
242 return (1);
243 }
244
245 u_int
246 nodetoino(u_int type)
247 {
248
249 switch (type) {
250 case F_BLOCK:
251 return S_IFBLK;
252 case F_CHAR:
253 return S_IFCHR;
254 case F_DIR:
255 return S_IFDIR;
256 case F_FIFO:
257 return S_IFIFO;
258 case F_FILE:
259 return S_IFREG;
260 case F_LINK:
261 return S_IFLNK;
262 #ifdef S_IFSOCK
263 case F_SOCK:
264 return S_IFSOCK;
265 #endif
266 default:
267 printf("unknown type %d", type);
268 abort();
269 }
270 /* NOTREACHED */
271 }
272
273 const char *
274 nodetype(u_int type)
275 {
276
277 return (inotype(nodetoino(type)));
278 }
279
280
281 const char *
282 inotype(u_int type)
283 {
284
285 switch (type & S_IFMT) {
286 case S_IFBLK:
287 return ("block");
288 case S_IFCHR:
289 return ("char");
290 case S_IFDIR:
291 return ("dir");
292 case S_IFIFO:
293 return ("fifo");
294 case S_IFREG:
295 return ("file");
296 case S_IFLNK:
297 return ("link");
298 #ifdef S_IFSOCK
299 case S_IFSOCK:
300 return ("socket");
301 #endif
302 #ifdef S_IFDOOR
303 case S_IFDOOR:
304 return ("door");
305 #endif
306 default:
307 return ("unknown");
308 }
309 /* NOTREACHED */
310 }
311