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