xref: /netbsd-src/usr.sbin/mtree/misc.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: misc.c,v 1.10 2001/07/18 04:51:54 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.10 2001/07/18 04:51:54 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 extern int lineno;
54 
55 typedef struct _key {
56 	const 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 tables 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 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 	KEY *k, tmp;
98 
99 	tmp.name = name;
100 	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
101 	    sizeof(KEY), keycompare);
102 	if (k == NULL)
103 		mtree_err("unknown keyword %s", name);
104 
105 	if (needvaluep)
106 		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
107 
108 	return (k->val);
109 }
110 
111 u_int
112 parsetype(const char *name)
113 {
114 	KEY *k, tmp;
115 
116 	tmp.name = name;
117 	k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
118 	    sizeof(KEY), keycompare);
119 	if (k == NULL)
120 		mtree_err("unknown file type %s", name);
121 
122 	return (k->val);
123 }
124 
125 int
126 keycompare(const void *a, const void *b)
127 {
128 
129 	return (strcmp(((KEY *)a)->name, ((KEY *)b)->name));
130 }
131 
132 void
133 mtree_err(const char *fmt, ...)
134 {
135 	va_list ap;
136 
137 	va_start(ap, fmt);
138 	(void)fprintf(stderr, "mtree: ");
139 	(void)vfprintf(stderr, fmt, ap);
140 	va_end(ap);
141 	(void)fprintf(stderr, "\n");
142 	if (lineno)
143 		(void)fprintf(stderr,
144 		    "mtree: failed at line %d of the specification\n", lineno);
145 	exit(1);
146 	/* NOTREACHED */
147 }
148