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 #include <sys/types.h>
44 enum flavor flavor = F_MTREE;
47 const char *name; /* key name */
48 u_int val; /* value */
50 #define NEEDVALUE 0x01
54 /* NB: the following tables must be sorted lexically. */
55 static KEY keylist[] = {
56 {"cksum", F_CKSUM, NEEDVALUE},
57 {"device", F_DEV, NEEDVALUE},
58 {"flags", F_FLAGS, NEEDVALUE},
59 {"gid", F_GID, NEEDVALUE},
60 {"gname", F_GNAME, NEEDVALUE},
62 {"link", F_SLINK, NEEDVALUE},
63 {"md5", F_MD5, NEEDVALUE},
64 {"md5digest", F_MD5, NEEDVALUE},
65 {"mode", F_MODE, NEEDVALUE},
66 {"nlink", F_NLINK, NEEDVALUE},
67 {"nochange", F_NOCHANGE, 0},
68 {"optional", F_OPT, 0},
69 {"ripemd160digest", F_RMD160, NEEDVALUE},
70 {"rmd160", F_RMD160, NEEDVALUE},
71 {"rmd160digest",F_RMD160, NEEDVALUE},
72 {"sha1", F_SHA1, NEEDVALUE},
73 {"sha1digest", F_SHA1, NEEDVALUE},
74 {"sha256", F_SHA256, NEEDVALUE},
75 {"sha256digest",F_SHA256, NEEDVALUE},
76 {"sha384", F_SHA384, NEEDVALUE},
77 {"sha384digest",F_SHA384, NEEDVALUE},
78 {"sha512", F_SHA512, NEEDVALUE},
79 {"sha512digest",F_SHA512, NEEDVALUE},
80 {"size", F_SIZE, NEEDVALUE},
81 {"tags", F_TAGS, NEEDVALUE},
82 {"time", F_TIME, NEEDVALUE},
83 {"type", F_TYPE, NEEDVALUE},
84 {"uid", F_UID, NEEDVALUE},
85 {"uname", F_UNAME, NEEDVALUE}
88 static KEY typelist[] = {
89 {"block", F_BLOCK, 0},
98 {"socket", F_SOCK, 0},
101 slist_t excludetags, includetags;
102 int keys = KEYDEFAULT;
105 int keycompare(const void *, const void *);
108 parsekey(const char *name, int *needvaluep)
116 for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
117 allbits |= keylist[i].val;
120 if (strcmp(name, "all") == 0)
122 k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
123 sizeof(KEY), keycompare);
125 mtree_err("unknown keyword `%s'", name);
128 *needvaluep = k->flags & NEEDVALUE ? 1 : 0;
134 parsetype(const char *name)
139 k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
140 sizeof(KEY), keycompare);
142 mtree_err("unknown file type `%s'", name);
148 keycompare(const void *a, const void *b)
151 return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
155 mtree_err(const char *fmt, ...)
163 warnx("failed at line %lu of the specification",
164 (u_long) mtree_lineno);
170 addtag(slist_t *list, char *elem)
175 if ((list->count % TAG_CHUNK) == 0) {
178 new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
181 mtree_err("memory allocation error");
184 list->list[list->count] = elem;
189 parsetags(slist_t *list, char *args)
198 while ((p = strsep(&args, ",")) != NULL) {
201 len = strlen(p) + 3; /* "," + p + ",\0" */
202 if ((e = malloc(len)) == NULL)
203 mtree_err("memory allocation error");
204 snprintf(e, len, ",%s,", p);
211 * returns 0 if there's a match from the exclude list in the node's tags,
212 * or there's an include list and no match.
213 * return 1 otherwise.
216 matchtags(NODE *node)
221 for (i = 0; i < excludetags.count; i++)
222 if (strstr(node->tags, excludetags.list[i]))
224 if (i < excludetags.count)
227 for (i = 0; i < includetags.count; i++)
228 if (strstr(node->tags, includetags.list[i]))
230 if (i > 0 && i == includetags.count)
232 } else if (includetags.count > 0) {
239 nodetoino(u_int type)
260 printf("unknown type %d", type);
270 return (inotype(nodetoino(type)));
278 switch (type & S_IFMT) {