xref: /openbsd-src/usr.sbin/mtree/misc.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: misc.c,v 1.16 2003/06/26 19:47:09 deraadt 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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)misc.c	8.1 (Berkeley) 6/6/93
33  */
34 
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fts.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include "mtree.h"
41 #include "extern.h"
42 
43 extern int lineno;
44 
45 typedef struct _key {
46 	char *name;			/* key name */
47 	u_int val;			/* value */
48 
49 #define	NEEDVALUE	0x01
50 	u_int flags;
51 } KEY;
52 
53 /* NB: the following table must be sorted lexically. */
54 static KEY keylist[] = {
55 	{"cksum",	F_CKSUM,	NEEDVALUE},
56 	{"flags",	F_FLAGS,	NEEDVALUE},
57 	{"gid",		F_GID,		NEEDVALUE},
58 	{"gname",	F_GNAME,	NEEDVALUE},
59 	{"ignore",	F_IGN,		0},
60 	{"link",	F_SLINK,	NEEDVALUE},
61 	{"md5digest",	F_MD5,		NEEDVALUE},
62 	{"mode",	F_MODE,		NEEDVALUE},
63 	{"nlink",	F_NLINK,	NEEDVALUE},
64 	{"nochange",	F_NOCHANGE,	0},
65 	{"optional",	F_OPT,		0},
66 	{"rmd160digest",F_RMD160,	NEEDVALUE},
67 	{"sha1digest",	F_SHA1,		NEEDVALUE},
68 	{"size",	F_SIZE,		NEEDVALUE},
69 	{"time",	F_TIME,		NEEDVALUE},
70 	{"type",	F_TYPE,		NEEDVALUE},
71 	{"uid",		F_UID,		NEEDVALUE},
72 	{"uname",	F_UNAME,	NEEDVALUE},
73 };
74 
75 u_int
76 parsekey(name, needvaluep)
77 	char *name;
78 	int *needvaluep;
79 {
80 	KEY *k, tmp;
81 	int keycompare(const void *, const void *);
82 
83 	tmp.name = name;
84 	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
85 	    sizeof(KEY), keycompare);
86 	if (k == NULL)
87 		error("unknown keyword %s", name);
88 
89 	if (needvaluep)
90 		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
91 	return (k->val);
92 }
93 
94 int
95 keycompare(const void *a, const void *b)
96 {
97 	return (strcmp(((KEY *)a)->name, ((KEY *)b)->name));
98 }
99 
100 void
101 error(const char *fmt, ...)
102 {
103 	va_list ap;
104 
105 	va_start(ap, fmt);
106 	(void)fflush(NULL);
107 	(void)fprintf(stderr, "\nmtree: ");
108 	(void)vfprintf(stderr, fmt, ap);
109 	va_end(ap);
110 	(void)fprintf(stderr, "\n");
111 	if (lineno)
112 		(void)fprintf(stderr,
113 		    "mtree: failed at line %d of the specification\n", lineno);
114 	exit(1);
115 	/* NOTREACHED */
116 }
117