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