1 /* $OpenBSD: misc.c,v 1.18 2004/08/01 18:32:20 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 <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 {"size", F_SIZE, NEEDVALUE},
68 {"time", F_TIME, NEEDVALUE},
69 {"type", F_TYPE, NEEDVALUE},
70 {"uid", F_UID, NEEDVALUE},
71 {"uname", F_UNAME, NEEDVALUE},
72 };
73
74 u_int
parsekey(char * name,int * needvaluep)75 parsekey(char *name, int *needvaluep)
76 {
77 KEY *k, tmp;
78 int keycompare(const void *, const void *);
79
80 tmp.name = name;
81 k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
82 sizeof(KEY), keycompare);
83 if (k == NULL)
84 error("unknown keyword %s", name);
85
86 if (needvaluep)
87 *needvaluep = k->flags & NEEDVALUE ? 1 : 0;
88 return (k->val);
89 }
90
91 int
keycompare(const void * a,const void * b)92 keycompare(const void *a, const void *b)
93 {
94 return (strcmp(((KEY *)a)->name, ((KEY *)b)->name));
95 }
96
97 void
error(const char * fmt,...)98 error(const char *fmt, ...)
99 {
100 va_list ap;
101
102 va_start(ap, fmt);
103 (void)fflush(NULL);
104 (void)fprintf(stderr, "\nmtree: ");
105 (void)vfprintf(stderr, fmt, ap);
106 va_end(ap);
107 (void)fprintf(stderr, "\n");
108 if (lineno)
109 (void)fprintf(stderr,
110 "mtree: failed at line %d of the specification\n", lineno);
111 exit(1);
112 /* NOTREACHED */
113 }
114