misc.c revision 1.29 1 /* $NetBSD: misc.c,v 1.29 2006/10/16 00:11:57 christos 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.29 2006/10/16 00:11:57 christos 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 {"optional", F_OPT, 0},
75 {"rmd160", F_RMD160, NEEDVALUE},
76 {"rmd160digest",F_RMD160, NEEDVALUE},
77 {"sha1", F_SHA1, NEEDVALUE},
78 {"sha1digest", F_SHA1, NEEDVALUE},
79 {"sha256", F_SHA256, NEEDVALUE},
80 {"sha256digest",F_SHA256, NEEDVALUE},
81 {"sha384", F_SHA384, NEEDVALUE},
82 {"sha384digest",F_SHA384, NEEDVALUE},
83 {"sha512", F_SHA512, NEEDVALUE},
84 {"sha512digest",F_SHA512, NEEDVALUE},
85 {"size", F_SIZE, NEEDVALUE},
86 {"tags", F_TAGS, NEEDVALUE},
87 {"time", F_TIME, NEEDVALUE},
88 {"type", F_TYPE, NEEDVALUE},
89 {"uid", F_UID, NEEDVALUE},
90 {"uname", F_UNAME, NEEDVALUE}
91 };
92
93 static KEY typelist[] = {
94 {"block", F_BLOCK, 0},
95 {"char", F_CHAR, 0},
96 {"dir", F_DIR, 0},
97 #ifdef S_IFDOOR
98 {"door", F_DOOR, 0},
99 #endif
100 {"fifo", F_FIFO, 0},
101 {"file", F_FILE, 0},
102 {"link", F_LINK, 0},
103 {"socket", F_SOCK, 0},
104 };
105
106 slist_t excludetags, includetags;
107 int keys = KEYDEFAULT;
108
109
110 int keycompare(const void *, const void *);
111
112 u_int
113 parsekey(const char *name, int *needvaluep)
114 {
115 static int allbits;
116 KEY *k, tmp;
117
118 if (allbits == 0) {
119 int i;
120
121 for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
122 allbits |= keylist[i].val;
123 }
124 tmp.name = name;
125 if (strcmp(name, "all") == 0)
126 return (allbits);
127 k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
128 sizeof(KEY), keycompare);
129 if (k == NULL)
130 mtree_err("unknown keyword `%s'", name);
131
132 if (needvaluep)
133 *needvaluep = k->flags & NEEDVALUE ? 1 : 0;
134
135 return (k->val);
136 }
137
138 u_int
139 parsetype(const char *name)
140 {
141 KEY *k, tmp;
142
143 tmp.name = name;
144 k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
145 sizeof(KEY), keycompare);
146 if (k == NULL)
147 mtree_err("unknown file type `%s'", name);
148
149 return (k->val);
150 }
151
152 int
153 keycompare(const void *a, const void *b)
154 {
155
156 return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
157 }
158
159 void
160 mtree_err(const char *fmt, ...)
161 {
162 va_list ap;
163
164 va_start(ap, fmt);
165 vwarnx(fmt, ap);
166 va_end(ap);
167 if (mtree_lineno)
168 warnx("failed at line %lu of the specification",
169 (u_long) mtree_lineno);
170 exit(1);
171 /* NOTREACHED */
172 }
173
174 void
175 addtag(slist_t *list, char *elem)
176 {
177
178 #define TAG_CHUNK 20
179
180 if ((list->count % TAG_CHUNK) == 0) {
181 char **new;
182
183 new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
184 * sizeof(char *));
185 if (new == NULL)
186 mtree_err("memory allocation error");
187 list->list = new;
188 }
189 list->list[list->count] = elem;
190 list->count++;
191 }
192
193 void
194 parsetags(slist_t *list, char *args)
195 {
196 char *p, *e;
197 int len;
198
199 if (args == NULL) {
200 addtag(list, NULL);
201 return;
202 }
203 while ((p = strsep(&args, ",")) != NULL) {
204 if (*p == '\0')
205 continue;
206 len = strlen(p) + 3; /* "," + p + ",\0" */
207 if ((e = malloc(len)) == NULL)
208 mtree_err("memory allocation error");
209 snprintf(e, len, ",%s,", p);
210 addtag(list, e);
211 }
212 }
213
214 /*
215 * matchtags
216 * returns 0 if there's a match from the exclude list in the node's tags,
217 * or there's an include list and no match.
218 * return 1 otherwise.
219 */
220 int
221 matchtags(NODE *node)
222 {
223 int i;
224
225 if (node->tags) {
226 for (i = 0; i < excludetags.count; i++)
227 if (strstr(node->tags, excludetags.list[i]))
228 break;
229 if (i < excludetags.count)
230 return (0);
231
232 for (i = 0; i < includetags.count; i++)
233 if (strstr(node->tags, includetags.list[i]))
234 break;
235 if (i > 0 && i == includetags.count)
236 return (0);
237 } else if (includetags.count > 0) {
238 return (0);
239 }
240 return (1);
241 }
242
243 u_int
244 nodetoino(u_int type)
245 {
246
247 switch (type) {
248 case F_BLOCK:
249 return S_IFBLK;
250 case F_CHAR:
251 return S_IFCHR;
252 case F_DIR:
253 return S_IFDIR;
254 case F_FIFO:
255 return S_IFIFO;
256 case F_FILE:
257 return S_IFREG;
258 case F_LINK:
259 return S_IFLNK;
260 #ifdef S_IFSOCK
261 case F_SOCK:
262 return S_IFSOCK;
263 #endif
264 default:
265 printf("unknown type %d", type);
266 abort();
267 }
268 /* NOTREACHED */
269 }
270
271 const char *
272 nodetype(u_int type)
273 {
274
275 return (inotype(nodetoino(type)));
276 }
277
278
279 const char *
280 inotype(u_int type)
281 {
282
283 switch (type & S_IFMT) {
284 case S_IFBLK:
285 return ("block");
286 case S_IFCHR:
287 return ("char");
288 case S_IFDIR:
289 return ("dir");
290 case S_IFIFO:
291 return ("fifo");
292 case S_IFREG:
293 return ("file");
294 case S_IFLNK:
295 return ("link");
296 #ifdef S_IFSOCK
297 case S_IFSOCK:
298 return ("socket");
299 #endif
300 #ifdef S_IFDOOR
301 case S_IFDOOR:
302 return ("door");
303 #endif
304 default:
305 return ("unknown");
306 }
307 /* NOTREACHED */
308 }
309