xref: /netbsd-src/usr.sbin/mtree/misc.c (revision 17dd36da8292193180754d5047c0926dbb56818c)
1 /*	$NetBSD: misc.c,v 1.9 2001/03/09 03:09:45 simonb 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.9 2001/03/09 03:09:45 simonb 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 extern int lineno;
54 
55 typedef struct _key {
56 	char *name;			/* key name */
57 	u_int val;			/* value */
58 
59 #define	NEEDVALUE	0x01
60 	u_int flags;
61 } KEY;
62 
63 /* NB: the following table must be sorted lexically. */
64 static KEY keylist[] = {
65 	{"cksum",	F_CKSUM,	NEEDVALUE},
66 	{"flags",	F_FLAGS,	NEEDVALUE},
67 	{"gid",		F_GID,		NEEDVALUE},
68 	{"gname",	F_GNAME,	NEEDVALUE},
69 	{"ignore",	F_IGN,		0},
70 	{"link",	F_SLINK,	NEEDVALUE},
71 	{"md5",		F_MD5,		NEEDVALUE},
72 	{"mode",	F_MODE,		NEEDVALUE},
73 	{"nlink",	F_NLINK,	NEEDVALUE},
74 	{"optional",	F_OPT,		0},
75 	{"size",	F_SIZE,		NEEDVALUE},
76 	{"time",	F_TIME,		NEEDVALUE},
77 	{"type",	F_TYPE,		NEEDVALUE},
78 	{"uid",		F_UID,		NEEDVALUE},
79 	{"uname",	F_UNAME,	NEEDVALUE}
80 };
81 
82 int keycompare(const void *, const void *);
83 
84 u_int
85 parsekey(char *name, int *needvaluep)
86 {
87 	KEY *k, tmp;
88 
89 	tmp.name = name;
90 	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
91 	    sizeof(KEY), keycompare);
92 	if (k == NULL)
93 		mtree_err("unknown keyword %s", name);
94 
95 	if (needvaluep)
96 		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
97 
98 	return (k->val);
99 }
100 
101 int
102 keycompare(const void *a, const void *b)
103 {
104 
105 	return (strcmp(((KEY *)a)->name, ((KEY *)b)->name));
106 }
107 
108 void
109 mtree_err(const char *fmt, ...)
110 {
111 	va_list ap;
112 
113 	va_start(ap, fmt);
114 	(void)fprintf(stderr, "mtree: ");
115 	(void)vfprintf(stderr, fmt, ap);
116 	va_end(ap);
117 	(void)fprintf(stderr, "\n");
118 	if (lineno)
119 		(void)fprintf(stderr,
120 		    "mtree: failed at line %d of the specification\n", lineno);
121 	exit(1);
122 	/* NOTREACHED */
123 }
124