1 /* $NetBSD: misc.c,v 1.34 2012/12/20 19:09:25 christos Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * @(#)misc.c 8.1 (Berkeley) 6/6/93
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
38 #include <sys/types.h>
48 enum flavor flavor = F_MTREE;
51 const char *name; /* key name */
52 u_int val; /* value */
54 #define NEEDVALUE 0x01
58 /* NB: the following tables must be sorted lexically. */
59 static KEY keylist[] = {
60 {"cksum", F_CKSUM, NEEDVALUE},
61 {"device", F_DEV, NEEDVALUE},
62 {"flags", F_FLAGS, NEEDVALUE},
63 {"gid", F_GID, NEEDVALUE},
64 {"gname", F_GNAME, NEEDVALUE},
66 {"link", F_SLINK, NEEDVALUE},
67 {"md5", F_MD5, NEEDVALUE},
68 {"md5digest", F_MD5, NEEDVALUE},
69 {"mode", F_MODE, NEEDVALUE},
70 {"nlink", F_NLINK, NEEDVALUE},
71 {"nochange", F_NOCHANGE, 0},
72 {"optional", F_OPT, 0},
73 {"ripemd160digest", F_RMD160, NEEDVALUE},
74 {"rmd160", F_RMD160, NEEDVALUE},
75 {"rmd160digest",F_RMD160, NEEDVALUE},
76 {"sha1", F_SHA1, NEEDVALUE},
77 {"sha1digest", F_SHA1, NEEDVALUE},
78 {"sha256", F_SHA256, NEEDVALUE},
79 {"sha256digest",F_SHA256, NEEDVALUE},
80 {"sha384", F_SHA384, NEEDVALUE},
81 {"sha384digest",F_SHA384, NEEDVALUE},
82 {"sha512", F_SHA512, NEEDVALUE},
83 {"sha512digest",F_SHA512, NEEDVALUE},
84 {"size", F_SIZE, NEEDVALUE},
85 {"tags", F_TAGS, NEEDVALUE},
86 {"time", F_TIME, NEEDVALUE},
87 {"type", F_TYPE, NEEDVALUE},
88 {"uid", F_UID, NEEDVALUE},
89 {"uname", F_UNAME, NEEDVALUE}
92 static KEY typelist[] = {
93 {"block", F_BLOCK, 0},
102 {"socket", F_SOCK, 0},
105 slist_t excludetags, includetags;
106 int keys = KEYDEFAULT;
109 int keycompare(const void *, const void *);
112 parsekey(const char *name, int *needvaluep)
120 for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
121 allbits |= keylist[i].val;
124 if (strcmp(name, "all") == 0)
126 k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
127 sizeof(KEY), keycompare);
129 mtree_err("unknown keyword `%s'", name);
132 *needvaluep = k->flags & NEEDVALUE ? 1 : 0;
138 parsetype(const char *name)
143 k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
144 sizeof(KEY), keycompare);
146 mtree_err("unknown file type `%s'", name);
152 keycompare(const void *a, const void *b)
155 return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
159 mtree_err(const char *fmt, ...)
167 warnx("failed at line %lu of the specification",
168 (u_long) mtree_lineno);
174 addtag(slist_t *list, char *elem)
179 if ((list->count % TAG_CHUNK) == 0) {
182 new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
185 mtree_err("memory allocation error");
188 list->list[list->count] = elem;
193 parsetags(slist_t *list, char *args)
202 while ((p = strsep(&args, ",")) != NULL) {
205 len = strlen(p) + 3; /* "," + p + ",\0" */
206 if ((e = malloc(len)) == NULL)
207 mtree_err("memory allocation error");
208 snprintf(e, len, ",%s,", p);
215 * returns 0 if there's a match from the exclude list in the node's tags,
216 * or there's an include list and no match.
217 * return 1 otherwise.
220 matchtags(NODE *node)
225 for (i = 0; i < excludetags.count; i++)
226 if (strstr(node->tags, excludetags.list[i]))
228 if (i < excludetags.count)
231 for (i = 0; i < includetags.count; i++)
232 if (strstr(node->tags, includetags.list[i]))
234 if (i > 0 && i == includetags.count)
236 } else if (includetags.count > 0) {
243 nodetoino(u_int type)
264 printf("unknown type %d", type);
274 return (inotype(nodetoino(type)));
282 switch (type & S_IFMT) {