xref: /original-bsd/usr.sbin/mtree/misc.c (revision ea3a8ee8)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)misc.c	5.2 (Berkeley) 04/17/92
8  */
9 
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fts.h>
13 #include <stdio.h>
14 #include "mtree.h"
15 #include "extern.h"
16 
17 extern int lineno;
18 
19 typedef struct _key {
20 	char *name;			/* key name */
21 	u_int val;			/* value */
22 
23 #define	NEEDVALUE	0x01
24 	u_int flags;
25 } KEY;
26 
27 /* NB: the following table must be sorted lexically. */
28 static KEY keylist[] = {
29 	"cksum",	F_CKSUM,	NEEDVALUE,
30 	"gid",		F_GID,		NEEDVALUE,
31 	"gname",	F_GNAME,	NEEDVALUE,
32 	"ignore",	F_IGN,		0,
33 	"link",		F_SLINK,	NEEDVALUE,
34 	"mode",		F_MODE,		NEEDVALUE,
35 	"nlink",	F_NLINK,	NEEDVALUE,
36 	"size",		F_SIZE,		NEEDVALUE,
37 	"time",		F_TIME,		NEEDVALUE,
38 	"type",		F_TYPE,		NEEDVALUE,
39 	"uid",		F_UID,		NEEDVALUE,
40 	"uname",	F_UNAME,	NEEDVALUE,
41 };
42 
43 u_int
44 parsekey(name, needvaluep)
45 	char *name;
46 	int *needvaluep;
47 {
48 	KEY *k, tmp;
49 	int keycompare __P((const void *, const void *));
50 
51 	tmp.name = name;
52 	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
53 	    sizeof(KEY), keycompare);
54 	if (k == NULL)
55 		err("unknown keyword %s", name);
56 
57 	if (needvaluep)
58 		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
59 	return (k->val);
60 }
61 
62 int
63 keycompare(a, b)
64 	const void *a, *b;
65 {
66 	return (strcmp(((KEY *)a)->name, ((KEY *)b)->name));
67 }
68 
69 #if __STDC__
70 #include <stdarg.h>
71 #else
72 #include <varargs.h>
73 #endif
74 
75 void
76 #if __STDC__
77 err(const char *fmt, ...)
78 #else
79 err(fmt, va_alist)
80 	char *fmt;
81         va_dcl
82 #endif
83 {
84 	va_list ap;
85 #if __STDC__
86 	va_start(ap, fmt);
87 #else
88 	va_start(ap);
89 #endif
90 	(void)fprintf(stderr, "mtree: ");
91 	(void)vfprintf(stderr, fmt, ap);
92 	va_end(ap);
93 	(void)fprintf(stderr, "\n");
94 	if (lineno)
95 		(void)fprintf(stderr,
96 		    "mtree: failed at line %d of the specification\n", lineno);
97 	exit(1);
98 	/* NOTREACHED */
99 }
100