Home | History | Annotate | Line # | Download | only in mtree
misc.c revision 1.14
      1 /*	$NetBSD: misc.c,v 1.14 2001/10/05 01:03:24 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.14 2001/10/05 01:03:24 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 
    149 void
    150 addtag(slist_t *list, char *elem)
    151 {
    152 
    153 #define	TAG_CHUNK 20
    154 
    155 	if ((list->count % TAG_CHUNK) == 0) {
    156 		char **new;
    157 
    158 		new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
    159 		    * sizeof(char *));
    160 		if (new == NULL)
    161 			mtree_err("memory allocation error");
    162 		list->list = new;
    163 	}
    164 	list->list[list->count] = elem;
    165 	list->count++;
    166 }
    167 
    168 void
    169 parsetags(slist_t *list, char *args)
    170 {
    171 	char	*p, *e;
    172 	int	len;
    173 
    174 	if (args == NULL) {
    175 		addtag(list, NULL);
    176 		return;
    177 	}
    178 	while ((p = strsep(&args, ",")) != NULL) {
    179 		if (*p == '\0')
    180 			continue;
    181 		len = strlen(p) + 3;	/* "," + p + ",\0" */
    182 		if ((e = malloc(len)) == NULL)
    183 			mtree_err("memory allocation error");
    184 		snprintf(e, len, ",%s,", p);
    185 		addtag(list, e);
    186 	}
    187 }
    188 
    189 /*
    190  * matchtags
    191  *	returns 0 if there's a match from the exclude list in the node's tags,
    192  *	or there's an include list and no match.
    193  *	return 1 otherwise.
    194  */
    195 int
    196 matchtags(NODE *node)
    197 {
    198 	int	i;
    199 
    200 	if (node->tags) {
    201 		for (i = 0; i < excludetags.count; i++)
    202 			if (strstr(node->tags, excludetags.list[i]))
    203 				break;
    204 		if (i < excludetags.count)
    205 			return (0);
    206 
    207 		for (i = 0; i < includetags.count; i++)
    208 			if (strstr(node->tags, includetags.list[i]))
    209 				break;
    210 		if (i > 0 && i == includetags.count)
    211 			return (0);
    212 	} else if (includetags.count > 0) {
    213 		return (0);
    214 	}
    215 	return (1);
    216 }
    217