xref: /netbsd-src/usr.sbin/mtree/misc.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: misc.c,v 1.20 2001/11/07 08:01:52 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.20 2001/11/07 08:01:52 lukem Exp $");
41 #endif /* not lint */
42 
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 
46 #include <err.h>
47 #include <fts.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "mtree.h"
54 #include "extern.h"
55 
56 typedef struct _key {
57 	const char	*name;		/* key name */
58 	u_int		val;		/* value */
59 
60 #define	NEEDVALUE	0x01
61 	u_int		flags;
62 } KEY;
63 
64 /* NB: the following tables must be sorted lexically. */
65 static KEY keylist[] = {
66 	{"cksum",	F_CKSUM,	NEEDVALUE},
67 	{"device",	F_DEV,		NEEDVALUE},
68 	{"flags",	F_FLAGS,	NEEDVALUE},
69 	{"gid",		F_GID,		NEEDVALUE},
70 	{"gname",	F_GNAME,	NEEDVALUE},
71 	{"ignore",	F_IGN,		0},
72 	{"link",	F_SLINK,	NEEDVALUE},
73 	{"md5",		F_MD5,		NEEDVALUE},
74 	{"md5digest",	F_MD5,		NEEDVALUE},
75 	{"mode",	F_MODE,		NEEDVALUE},
76 	{"nlink",	F_NLINK,	NEEDVALUE},
77 	{"optional",	F_OPT,		0},
78 	{"rmd160",	F_RMD160,	NEEDVALUE},
79 	{"rmd160digest",F_RMD160,	NEEDVALUE},
80 	{"sha1",	F_SHA1,		NEEDVALUE},
81 	{"sha1digest",	F_SHA1,		NEEDVALUE},
82 	{"size",	F_SIZE,		NEEDVALUE},
83 	{"tags",	F_TAGS,		NEEDVALUE},
84 	{"time",	F_TIME,		NEEDVALUE},
85 	{"type",	F_TYPE,		NEEDVALUE},
86 	{"uid",		F_UID,		NEEDVALUE},
87 	{"uname",	F_UNAME,	NEEDVALUE}
88 };
89 
90 static KEY typelist[] = {
91 	{"block",	F_BLOCK,	},
92 	{"char",	F_CHAR,		},
93 	{"dir",		F_DIR,		},
94 	{"fifo",	F_FIFO,		},
95 	{"file",	F_FILE,		},
96 	{"link",	F_LINK,		},
97 	{"socket",	F_SOCK,		},
98 };
99 
100 slist_t	excludetags, includetags;
101 int	keys = KEYDEFAULT;
102 
103 
104 int keycompare(const void *, const void *);
105 
106 u_int
107 parsekey(const char *name, int *needvaluep)
108 {
109 	static int allbits;
110 	KEY *k, tmp;
111 
112 	if (allbits == 0) {
113 		int i;
114 
115 		for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
116 			allbits |= keylist[i].val;
117 	}
118 	tmp.name = name;
119 	if (strcmp(name, "all") == 0)
120 		return (allbits);
121 	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
122 	    sizeof(KEY), keycompare);
123 	if (k == NULL)
124 		mtree_err("unknown keyword `%s'", name);
125 
126 	if (needvaluep)
127 		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
128 
129 	return (k->val);
130 }
131 
132 u_int
133 parsetype(const char *name)
134 {
135 	KEY *k, tmp;
136 
137 	tmp.name = name;
138 	k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
139 	    sizeof(KEY), keycompare);
140 	if (k == NULL)
141 		mtree_err("unknown file type `%s'", name);
142 
143 	return (k->val);
144 }
145 
146 int
147 keycompare(const void *a, const void *b)
148 {
149 
150 	return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
151 }
152 
153 void
154 mtree_err(const char *fmt, ...)
155 {
156 	va_list ap;
157 
158 	va_start(ap, fmt);
159 	vwarnx(fmt, ap);
160 	va_end(ap);
161 	if (mtree_lineno)
162 		warnx("failed at line %lu of the specification",
163 		    (u_long) mtree_lineno);
164 	exit(1);
165 	/* NOTREACHED */
166 }
167 
168 void
169 addtag(slist_t *list, char *elem)
170 {
171 
172 #define	TAG_CHUNK 20
173 
174 	if ((list->count % TAG_CHUNK) == 0) {
175 		char **new;
176 
177 		new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
178 		    * sizeof(char *));
179 		if (new == NULL)
180 			mtree_err("memory allocation error");
181 		list->list = new;
182 	}
183 	list->list[list->count] = elem;
184 	list->count++;
185 }
186 
187 void
188 parsetags(slist_t *list, char *args)
189 {
190 	char	*p, *e;
191 	int	len;
192 
193 	if (args == NULL) {
194 		addtag(list, NULL);
195 		return;
196 	}
197 	while ((p = strsep(&args, ",")) != NULL) {
198 		if (*p == '\0')
199 			continue;
200 		len = strlen(p) + 3;	/* "," + p + ",\0" */
201 		if ((e = malloc(len)) == NULL)
202 			mtree_err("memory allocation error");
203 		snprintf(e, len, ",%s,", p);
204 		addtag(list, e);
205 	}
206 }
207 
208 /*
209  * matchtags
210  *	returns 0 if there's a match from the exclude list in the node's tags,
211  *	or there's an include list and no match.
212  *	return 1 otherwise.
213  */
214 int
215 matchtags(NODE *node)
216 {
217 	int	i;
218 
219 	if (node->tags) {
220 		for (i = 0; i < excludetags.count; i++)
221 			if (strstr(node->tags, excludetags.list[i]))
222 				break;
223 		if (i < excludetags.count)
224 			return (0);
225 
226 		for (i = 0; i < includetags.count; i++)
227 			if (strstr(node->tags, includetags.list[i]))
228 				break;
229 		if (i > 0 && i == includetags.count)
230 			return (0);
231 	} else if (includetags.count > 0) {
232 		return (0);
233 	}
234 	return (1);
235 }
236 
237 u_int
238 nodetoino(u_int type)
239 {
240 
241 	switch (type) {
242 	case F_BLOCK:
243 		return S_IFBLK;
244 	case F_CHAR:
245 		return S_IFCHR;
246 	case F_DIR:
247 		return S_IFDIR;
248 	case F_FIFO:
249 		return S_IFIFO;
250 	case F_FILE:
251 		return S_IFREG;
252 	case F_LINK:
253 		return S_IFLNK;
254 	case F_SOCK:
255 		return S_IFSOCK;
256 	default:
257 		printf("unknown type %d", type);
258 		abort();
259 	}
260 	/* NOTREACHED */
261 }
262 
263 const char *
264 nodetype(u_int type)
265 {
266 
267 	return (inotype(nodetoino(type)));
268 }
269 
270 
271 const char *
272 inotype(u_int type)
273 {
274 
275 	switch (type & S_IFMT) {
276 	case F_BLOCK:
277 	case S_IFBLK:
278 		return ("block");
279 	case F_CHAR:
280 	case S_IFCHR:
281 		return ("char");
282 	case F_DIR:
283 	case S_IFDIR:
284 		return ("dir");
285 	case F_FIFO:
286 	case S_IFIFO:
287 		return ("fifo");
288 	case F_FILE:
289 	case S_IFREG:
290 		return ("file");
291 	case F_LINK:
292 	case S_IFLNK:
293 		return ("link");
294 	case F_SOCK:
295 	case S_IFSOCK:
296 		return ("socket");
297 	default:
298 		return ("unknown");
299 	}
300 	/* NOTREACHED */
301 }
302