misc.c revision 1.30.8.2 1 1.30.8.2 yamt /* $NetBSD: misc.c,v 1.30.8.2 2013/01/16 05:34:09 yamt Exp $ */
2 1.3 cgd
3 1.1 cgd /*-
4 1.2 cgd * Copyright (c) 1991, 1993
5 1.2 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.24 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd *
31 1.2 cgd * @(#)misc.c 8.1 (Berkeley) 6/6/93
32 1.1 cgd */
33 1.1 cgd
34 1.25 jmc #if HAVE_NBTOOL_CONFIG_H
35 1.25 jmc #include "nbtool_config.h"
36 1.25 jmc #endif
37 1.25 jmc
38 1.5 lukem #include <sys/cdefs.h>
39 1.22 tv #if defined(__RCSID) && !defined(lint)
40 1.30.8.2 yamt __RCSID("$NetBSD: misc.c,v 1.30.8.2 2013/01/16 05:34:09 yamt Exp $");
41 1.5 lukem #endif /* not lint */
42 1.5 lukem
43 1.1 cgd #include <sys/types.h>
44 1.1 cgd #include <sys/stat.h>
45 1.9 simonb
46 1.9 simonb #include <stdarg.h>
47 1.1 cgd #include <stdio.h>
48 1.18 lukem #include <stdlib.h>
49 1.18 lukem #include <string.h>
50 1.9 simonb
51 1.1 cgd #include "extern.h"
52 1.1 cgd
53 1.1 cgd typedef struct _key {
54 1.10 lukem const char *name; /* key name */
55 1.10 lukem u_int val; /* value */
56 1.1 cgd
57 1.1 cgd #define NEEDVALUE 0x01
58 1.10 lukem u_int flags;
59 1.1 cgd } KEY;
60 1.1 cgd
61 1.10 lukem /* NB: the following tables must be sorted lexically. */
62 1.1 cgd static KEY keylist[] = {
63 1.5 lukem {"cksum", F_CKSUM, NEEDVALUE},
64 1.16 lukem {"device", F_DEV, NEEDVALUE},
65 1.7 mrg {"flags", F_FLAGS, NEEDVALUE},
66 1.5 lukem {"gid", F_GID, NEEDVALUE},
67 1.5 lukem {"gname", F_GNAME, NEEDVALUE},
68 1.5 lukem {"ignore", F_IGN, 0},
69 1.5 lukem {"link", F_SLINK, NEEDVALUE},
70 1.8 jwise {"md5", F_MD5, NEEDVALUE},
71 1.20 lukem {"md5digest", F_MD5, NEEDVALUE},
72 1.5 lukem {"mode", F_MODE, NEEDVALUE},
73 1.5 lukem {"nlink", F_NLINK, NEEDVALUE},
74 1.30.8.1 yamt {"nochange", F_NOCHANGE, 0},
75 1.5 lukem {"optional", F_OPT, 0},
76 1.30.8.1 yamt {"ripemd160digest", F_RMD160, NEEDVALUE},
77 1.20 lukem {"rmd160", F_RMD160, NEEDVALUE},
78 1.20 lukem {"rmd160digest",F_RMD160, NEEDVALUE},
79 1.20 lukem {"sha1", F_SHA1, NEEDVALUE},
80 1.20 lukem {"sha1digest", F_SHA1, NEEDVALUE},
81 1.26 elad {"sha256", F_SHA256, NEEDVALUE},
82 1.26 elad {"sha256digest",F_SHA256, NEEDVALUE},
83 1.26 elad {"sha384", F_SHA384, NEEDVALUE},
84 1.26 elad {"sha384digest",F_SHA384, NEEDVALUE},
85 1.26 elad {"sha512", F_SHA512, NEEDVALUE},
86 1.26 elad {"sha512digest",F_SHA512, NEEDVALUE},
87 1.27 tron {"size", F_SIZE, NEEDVALUE},
88 1.27 tron {"tags", F_TAGS, NEEDVALUE},
89 1.27 tron {"time", F_TIME, NEEDVALUE},
90 1.27 tron {"type", F_TYPE, NEEDVALUE},
91 1.27 tron {"uid", F_UID, NEEDVALUE},
92 1.27 tron {"uname", F_UNAME, NEEDVALUE}
93 1.1 cgd };
94 1.1 cgd
95 1.10 lukem static KEY typelist[] = {
96 1.29 christos {"block", F_BLOCK, 0},
97 1.29 christos {"char", F_CHAR, 0},
98 1.29 christos {"dir", F_DIR, 0},
99 1.28 christos #ifdef S_IFDOOR
100 1.29 christos {"door", F_DOOR, 0},
101 1.28 christos #endif
102 1.29 christos {"fifo", F_FIFO, 0},
103 1.29 christos {"file", F_FILE, 0},
104 1.29 christos {"link", F_LINK, 0},
105 1.29 christos {"socket", F_SOCK, 0},
106 1.10 lukem };
107 1.10 lukem
108 1.18 lukem slist_t excludetags, includetags;
109 1.18 lukem int keys = KEYDEFAULT;
110 1.18 lukem
111 1.18 lukem
112 1.9 simonb int keycompare(const void *, const void *);
113 1.5 lukem
114 1.1 cgd u_int
115 1.10 lukem parsekey(const char *name, int *needvaluep)
116 1.1 cgd {
117 1.15 lukem static int allbits;
118 1.1 cgd KEY *k, tmp;
119 1.1 cgd
120 1.15 lukem if (allbits == 0) {
121 1.30 lukem size_t i;
122 1.15 lukem
123 1.15 lukem for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
124 1.15 lukem allbits |= keylist[i].val;
125 1.15 lukem }
126 1.1 cgd tmp.name = name;
127 1.15 lukem if (strcmp(name, "all") == 0)
128 1.15 lukem return (allbits);
129 1.1 cgd k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
130 1.1 cgd sizeof(KEY), keycompare);
131 1.1 cgd if (k == NULL)
132 1.13 lukem mtree_err("unknown keyword `%s'", name);
133 1.1 cgd
134 1.1 cgd if (needvaluep)
135 1.1 cgd *needvaluep = k->flags & NEEDVALUE ? 1 : 0;
136 1.10 lukem
137 1.10 lukem return (k->val);
138 1.10 lukem }
139 1.10 lukem
140 1.10 lukem u_int
141 1.10 lukem parsetype(const char *name)
142 1.10 lukem {
143 1.10 lukem KEY *k, tmp;
144 1.10 lukem
145 1.10 lukem tmp.name = name;
146 1.10 lukem k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
147 1.10 lukem sizeof(KEY), keycompare);
148 1.10 lukem if (k == NULL)
149 1.13 lukem mtree_err("unknown file type `%s'", name);
150 1.8 jwise
151 1.1 cgd return (k->val);
152 1.1 cgd }
153 1.1 cgd
154 1.1 cgd int
155 1.9 simonb keycompare(const void *a, const void *b)
156 1.1 cgd {
157 1.9 simonb
158 1.17 lukem return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
159 1.1 cgd }
160 1.1 cgd
161 1.1 cgd void
162 1.6 wsanchez mtree_err(const char *fmt, ...)
163 1.1 cgd {
164 1.1 cgd va_list ap;
165 1.9 simonb
166 1.1 cgd va_start(ap, fmt);
167 1.18 lukem vwarnx(fmt, ap);
168 1.1 cgd va_end(ap);
169 1.19 lukem if (mtree_lineno)
170 1.18 lukem warnx("failed at line %lu of the specification",
171 1.19 lukem (u_long) mtree_lineno);
172 1.1 cgd exit(1);
173 1.1 cgd /* NOTREACHED */
174 1.14 lukem }
175 1.14 lukem
176 1.14 lukem void
177 1.14 lukem addtag(slist_t *list, char *elem)
178 1.14 lukem {
179 1.14 lukem
180 1.14 lukem #define TAG_CHUNK 20
181 1.14 lukem
182 1.14 lukem if ((list->count % TAG_CHUNK) == 0) {
183 1.14 lukem char **new;
184 1.14 lukem
185 1.14 lukem new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
186 1.14 lukem * sizeof(char *));
187 1.14 lukem if (new == NULL)
188 1.14 lukem mtree_err("memory allocation error");
189 1.14 lukem list->list = new;
190 1.14 lukem }
191 1.14 lukem list->list[list->count] = elem;
192 1.14 lukem list->count++;
193 1.14 lukem }
194 1.14 lukem
195 1.14 lukem void
196 1.14 lukem parsetags(slist_t *list, char *args)
197 1.14 lukem {
198 1.14 lukem char *p, *e;
199 1.14 lukem int len;
200 1.14 lukem
201 1.14 lukem if (args == NULL) {
202 1.14 lukem addtag(list, NULL);
203 1.14 lukem return;
204 1.14 lukem }
205 1.14 lukem while ((p = strsep(&args, ",")) != NULL) {
206 1.14 lukem if (*p == '\0')
207 1.14 lukem continue;
208 1.14 lukem len = strlen(p) + 3; /* "," + p + ",\0" */
209 1.14 lukem if ((e = malloc(len)) == NULL)
210 1.14 lukem mtree_err("memory allocation error");
211 1.14 lukem snprintf(e, len, ",%s,", p);
212 1.14 lukem addtag(list, e);
213 1.14 lukem }
214 1.14 lukem }
215 1.14 lukem
216 1.14 lukem /*
217 1.14 lukem * matchtags
218 1.14 lukem * returns 0 if there's a match from the exclude list in the node's tags,
219 1.14 lukem * or there's an include list and no match.
220 1.14 lukem * return 1 otherwise.
221 1.14 lukem */
222 1.14 lukem int
223 1.14 lukem matchtags(NODE *node)
224 1.14 lukem {
225 1.14 lukem int i;
226 1.14 lukem
227 1.14 lukem if (node->tags) {
228 1.14 lukem for (i = 0; i < excludetags.count; i++)
229 1.14 lukem if (strstr(node->tags, excludetags.list[i]))
230 1.14 lukem break;
231 1.20 lukem if (i < excludetags.count)
232 1.14 lukem return (0);
233 1.14 lukem
234 1.14 lukem for (i = 0; i < includetags.count; i++)
235 1.14 lukem if (strstr(node->tags, includetags.list[i]))
236 1.14 lukem break;
237 1.14 lukem if (i > 0 && i == includetags.count)
238 1.14 lukem return (0);
239 1.14 lukem } else if (includetags.count > 0) {
240 1.14 lukem return (0);
241 1.14 lukem }
242 1.14 lukem return (1);
243 1.18 lukem }
244 1.18 lukem
245 1.18 lukem u_int
246 1.18 lukem nodetoino(u_int type)
247 1.18 lukem {
248 1.18 lukem
249 1.18 lukem switch (type) {
250 1.18 lukem case F_BLOCK:
251 1.18 lukem return S_IFBLK;
252 1.18 lukem case F_CHAR:
253 1.18 lukem return S_IFCHR;
254 1.18 lukem case F_DIR:
255 1.18 lukem return S_IFDIR;
256 1.18 lukem case F_FIFO:
257 1.18 lukem return S_IFIFO;
258 1.18 lukem case F_FILE:
259 1.18 lukem return S_IFREG;
260 1.18 lukem case F_LINK:
261 1.18 lukem return S_IFLNK;
262 1.25 jmc #ifdef S_IFSOCK
263 1.18 lukem case F_SOCK:
264 1.18 lukem return S_IFSOCK;
265 1.25 jmc #endif
266 1.18 lukem default:
267 1.18 lukem printf("unknown type %d", type);
268 1.18 lukem abort();
269 1.18 lukem }
270 1.18 lukem /* NOTREACHED */
271 1.18 lukem }
272 1.18 lukem
273 1.18 lukem const char *
274 1.18 lukem nodetype(u_int type)
275 1.18 lukem {
276 1.18 lukem
277 1.18 lukem return (inotype(nodetoino(type)));
278 1.18 lukem }
279 1.18 lukem
280 1.18 lukem
281 1.18 lukem const char *
282 1.18 lukem inotype(u_int type)
283 1.18 lukem {
284 1.18 lukem
285 1.18 lukem switch (type & S_IFMT) {
286 1.18 lukem case S_IFBLK:
287 1.18 lukem return ("block");
288 1.18 lukem case S_IFCHR:
289 1.18 lukem return ("char");
290 1.18 lukem case S_IFDIR:
291 1.18 lukem return ("dir");
292 1.18 lukem case S_IFIFO:
293 1.18 lukem return ("fifo");
294 1.18 lukem case S_IFREG:
295 1.18 lukem return ("file");
296 1.18 lukem case S_IFLNK:
297 1.18 lukem return ("link");
298 1.25 jmc #ifdef S_IFSOCK
299 1.18 lukem case S_IFSOCK:
300 1.18 lukem return ("socket");
301 1.25 jmc #endif
302 1.28 christos #ifdef S_IFDOOR
303 1.28 christos case S_IFDOOR:
304 1.28 christos return ("door");
305 1.28 christos #endif
306 1.18 lukem default:
307 1.18 lukem return ("unknown");
308 1.18 lukem }
309 1.18 lukem /* NOTREACHED */
310 1.1 cgd }
311