xref: /openbsd-src/usr.sbin/mtree/misc.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: misc.c,v 1.9 2001/08/10 02:33:46 millert Exp $	*/
2 /*	$NetBSD: misc.c,v 1.4 1995/03/07 21:26:23 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)misc.c	8.1 (Berkeley) 6/6/93
37  */
38 
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fts.h>
42 #include <stdio.h>
43 #include "mtree.h"
44 #include "extern.h"
45 
46 extern int lineno;
47 
48 typedef struct _key {
49 	char *name;			/* key name */
50 	u_int val;			/* value */
51 
52 #define	NEEDVALUE	0x01
53 	u_int flags;
54 } KEY;
55 
56 /* NB: the following table must be sorted lexically. */
57 static KEY keylist[] = {
58 	{"cksum",	F_CKSUM,	NEEDVALUE},
59 	{"flags",	F_FLAGS,	NEEDVALUE},
60 	{"gid",		F_GID,		NEEDVALUE},
61 	{"gname",	F_GNAME,	NEEDVALUE},
62 	{"ignore",	F_IGN,		0},
63 	{"link",	F_SLINK,	NEEDVALUE},
64 	{"md5digest",	F_MD5,		NEEDVALUE},
65 	{"mode",	F_MODE,		NEEDVALUE},
66 	{"nlink",	F_NLINK,	NEEDVALUE},
67 	{"optional",	F_OPT,		0},
68 	{"rmd160digest",F_RMD160,	NEEDVALUE},
69 	{"sha1digest",	F_SHA1,		NEEDVALUE},
70 	{"size",	F_SIZE,		NEEDVALUE},
71 	{"time",	F_TIME,		NEEDVALUE},
72 	{"type",	F_TYPE,		NEEDVALUE},
73 	{"uid",		F_UID,		NEEDVALUE},
74 	{"uname",	F_UNAME,	NEEDVALUE},
75 };
76 
77 u_int
78 parsekey(name, needvaluep)
79 	char *name;
80 	int *needvaluep;
81 {
82 	KEY *k, tmp;
83 	int keycompare __P((const void *, const void *));
84 
85 	tmp.name = name;
86 	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
87 	    sizeof(KEY), keycompare);
88 	if (k == NULL)
89 		error("unknown keyword %s", name);
90 
91 	if (needvaluep)
92 		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
93 	return (k->val);
94 }
95 
96 int
97 keycompare(a, b)
98 	const void *a, *b;
99 {
100 	return (strcmp(((KEY *)a)->name, ((KEY *)b)->name));
101 }
102 
103 #ifdef __STDC__
104 #include <stdarg.h>
105 #else
106 #include <varargs.h>
107 #endif
108 
109 void
110 #ifdef __STDC__
111 error(const char *fmt, ...)
112 #else
113 error(fmt, va_alist)
114 	char *fmt;
115         va_dcl
116 #endif
117 {
118 	va_list ap;
119 #ifdef __STDC__
120 	va_start(ap, fmt);
121 #else
122 	va_start(ap);
123 #endif
124 	(void)fflush(NULL);
125 	(void)fprintf(stderr, "\nmtree: ");
126 	(void)vfprintf(stderr, fmt, ap);
127 	va_end(ap);
128 	(void)fprintf(stderr, "\n");
129 	if (lineno)
130 		(void)fprintf(stderr,
131 		    "mtree: failed at line %d of the specification\n", lineno);
132 	exit(1);
133 	/* NOTREACHED */
134 }
135