misc.c revision 1.17 1 /* $NetBSD: misc.c,v 1.17 2001/10/18 04:37:56 lukem 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)misc.c 8.1 (Berkeley) 6/6/93
36 */
37
38 #include <sys/cdefs.h>
39 #ifndef lint
40 __RCSID("$NetBSD: misc.c,v 1.17 2001/10/18 04:37:56 lukem Exp $");
41 #endif /* not lint */
42
43 #include <sys/types.h>
44 #include <sys/stat.h>
45
46 #include <fts.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49
50 #include "mtree.h"
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 {"mode", F_MODE, NEEDVALUE},
72 {"nlink", F_NLINK, NEEDVALUE},
73 {"optional", F_OPT, 0},
74 {"size", F_SIZE, NEEDVALUE},
75 {"tags", F_TAGS, NEEDVALUE},
76 {"time", F_TIME, NEEDVALUE},
77 {"type", F_TYPE, NEEDVALUE},
78 {"uid", F_UID, NEEDVALUE},
79 {"uname", F_UNAME, NEEDVALUE}
80 };
81
82 static KEY typelist[] = {
83 {"block", F_BLOCK, },
84 {"char", F_CHAR, },
85 {"dir", F_DIR, },
86 {"fifo", F_FIFO, },
87 {"file", F_FILE, },
88 {"link", F_LINK, },
89 {"socket", F_SOCK, },
90 };
91
92 int keycompare(const void *, const void *);
93
94 u_int
95 parsekey(const char *name, int *needvaluep)
96 {
97 static int allbits;
98 KEY *k, tmp;
99
100 if (allbits == 0) {
101 int i;
102
103 for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
104 allbits |= keylist[i].val;
105 }
106 tmp.name = name;
107 if (strcmp(name, "all") == 0)
108 return (allbits);
109 k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
110 sizeof(KEY), keycompare);
111 if (k == NULL)
112 mtree_err("unknown keyword `%s'", name);
113
114 if (needvaluep)
115 *needvaluep = k->flags & NEEDVALUE ? 1 : 0;
116
117 return (k->val);
118 }
119
120 u_int
121 parsetype(const char *name)
122 {
123 KEY *k, tmp;
124
125 tmp.name = name;
126 k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
127 sizeof(KEY), keycompare);
128 if (k == NULL)
129 mtree_err("unknown file type `%s'", name);
130
131 return (k->val);
132 }
133
134 int
135 keycompare(const void *a, const void *b)
136 {
137
138 return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
139 }
140
141 void
142 mtree_err(const char *fmt, ...)
143 {
144 va_list ap;
145
146 va_start(ap, fmt);
147 (void)fprintf(stderr, "mtree: ");
148 (void)vfprintf(stderr, fmt, ap);
149 va_end(ap);
150 (void)fprintf(stderr, "\n");
151 if (lineno)
152 (void)fprintf(stderr,
153 "mtree: failed at line %lu of the specification\n",
154 (u_long) lineno);
155 exit(1);
156 /* NOTREACHED */
157 }
158
159 void
160 addtag(slist_t *list, char *elem)
161 {
162
163 #define TAG_CHUNK 20
164
165 if ((list->count % TAG_CHUNK) == 0) {
166 char **new;
167
168 new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
169 * sizeof(char *));
170 if (new == NULL)
171 mtree_err("memory allocation error");
172 list->list = new;
173 }
174 list->list[list->count] = elem;
175 list->count++;
176 }
177
178 void
179 parsetags(slist_t *list, char *args)
180 {
181 char *p, *e;
182 int len;
183
184 if (args == NULL) {
185 addtag(list, NULL);
186 return;
187 }
188 while ((p = strsep(&args, ",")) != NULL) {
189 if (*p == '\0')
190 continue;
191 len = strlen(p) + 3; /* "," + p + ",\0" */
192 if ((e = malloc(len)) == NULL)
193 mtree_err("memory allocation error");
194 snprintf(e, len, ",%s,", p);
195 addtag(list, e);
196 }
197 }
198
199 /*
200 * matchtags
201 * returns 0 if there's a match from the exclude list in the node's tags,
202 * or there's an include list and no match.
203 * return 1 otherwise.
204 */
205 int
206 matchtags(NODE *node)
207 {
208 int i;
209
210 if (node->tags) {
211 for (i = 0; i < excludetags.count; i++)
212 if (strstr(node->tags, excludetags.list[i]))
213 break;
214 if (i < excludetags.count)
215 return (0);
216
217 for (i = 0; i < includetags.count; i++)
218 if (strstr(node->tags, includetags.list[i]))
219 break;
220 if (i > 0 && i == includetags.count)
221 return (0);
222 } else if (includetags.count > 0) {
223 return (0);
224 }
225 return (1);
226 }
227