xref: /openbsd/usr.sbin/mtree/misc.c (revision fc61954a)
1 /*	$OpenBSD: misc.c,v 1.19 2012/07/08 21:19:42 naddy 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 <stdio.h>
38 #include <stdarg.h>
39 #include "mtree.h"
40 #include "extern.h"
41 
42 extern int lineno;
43 
44 typedef struct _key {
45 	char *name;			/* key name */
46 	u_int val;			/* value */
47 
48 #define	NEEDVALUE	0x01
49 	u_int flags;
50 } KEY;
51 
52 /* NB: the following table must be sorted lexically. */
53 static KEY keylist[] = {
54 	{"cksum",	F_CKSUM,	NEEDVALUE},
55 	{"flags",	F_FLAGS,	NEEDVALUE},
56 	{"gid",		F_GID,		NEEDVALUE},
57 	{"gname",	F_GNAME,	NEEDVALUE},
58 	{"ignore",	F_IGN,		0},
59 	{"link",	F_SLINK,	NEEDVALUE},
60 	{"md5digest",	F_MD5,		NEEDVALUE},
61 	{"mode",	F_MODE,		NEEDVALUE},
62 	{"nlink",	F_NLINK,	NEEDVALUE},
63 	{"nochange",	F_NOCHANGE,	0},
64 	{"optional",	F_OPT,		0},
65 	{"rmd160digest",F_RMD160,	NEEDVALUE},
66 	{"sha1digest",	F_SHA1,		NEEDVALUE},
67 	{"sha256digest",F_SHA256,	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(char *name, int *needvaluep)
77 {
78 	KEY *k, tmp;
79 	int keycompare(const void *, const void *);
80 
81 	tmp.name = name;
82 	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
83 	    sizeof(KEY), keycompare);
84 	if (k == NULL)
85 		error("unknown keyword %s", name);
86 
87 	if (needvaluep)
88 		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
89 	return (k->val);
90 }
91 
92 int
93 keycompare(const void *a, const void *b)
94 {
95 	return (strcmp(((KEY *)a)->name, ((KEY *)b)->name));
96 }
97 
98 void
99 error(const char *fmt, ...)
100 {
101 	va_list ap;
102 
103 	va_start(ap, fmt);
104 	(void)fflush(NULL);
105 	(void)fprintf(stderr, "\nmtree: ");
106 	(void)vfprintf(stderr, fmt, ap);
107 	va_end(ap);
108 	(void)fprintf(stderr, "\n");
109 	if (lineno)
110 		(void)fprintf(stderr,
111 		    "mtree: failed at line %d of the specification\n", lineno);
112 	exit(1);
113 	/* NOTREACHED */
114 }
115