Home | History | Annotate | Line # | Download | only in mtree
misc.c revision 1.15
      1  1.15     lukem /*	$NetBSD: misc.c,v 1.15 2001/10/05 13:14:56 lukem Exp $	*/
      2   1.3       cgd 
      3   1.1       cgd /*-
      4   1.2       cgd  * Copyright (c) 1991, 1993
      5   1.2       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  *
     35   1.2       cgd  *	@(#)misc.c	8.1 (Berkeley) 6/6/93
     36   1.1       cgd  */
     37   1.1       cgd 
     38   1.5     lukem #include <sys/cdefs.h>
     39   1.5     lukem #ifndef lint
     40  1.15     lukem __RCSID("$NetBSD: misc.c,v 1.15 2001/10/05 13:14:56 lukem Exp $");
     41   1.5     lukem #endif /* not lint */
     42   1.5     lukem 
     43   1.1       cgd #include <sys/types.h>
     44   1.1       cgd #include <sys/stat.h>
     45   1.9    simonb 
     46   1.1       cgd #include <fts.h>
     47   1.9    simonb #include <stdarg.h>
     48   1.1       cgd #include <stdio.h>
     49   1.9    simonb 
     50   1.1       cgd #include "mtree.h"
     51   1.1       cgd #include "extern.h"
     52   1.1       cgd 
     53   1.1       cgd typedef struct _key {
     54  1.10     lukem 	const char	*name;		/* key name */
     55  1.10     lukem 	u_int		val;		/* value */
     56   1.1       cgd 
     57   1.1       cgd #define	NEEDVALUE	0x01
     58  1.10     lukem 	u_int		flags;
     59   1.1       cgd } KEY;
     60   1.1       cgd 
     61  1.10     lukem /* NB: the following tables must be sorted lexically. */
     62   1.1       cgd static KEY keylist[] = {
     63   1.5     lukem 	{"cksum",	F_CKSUM,	NEEDVALUE},
     64   1.7       mrg 	{"flags",	F_FLAGS,	NEEDVALUE},
     65   1.5     lukem 	{"gid",		F_GID,		NEEDVALUE},
     66   1.5     lukem 	{"gname",	F_GNAME,	NEEDVALUE},
     67   1.5     lukem 	{"ignore",	F_IGN,		0},
     68   1.5     lukem 	{"link",	F_SLINK,	NEEDVALUE},
     69   1.8     jwise 	{"md5",		F_MD5,		NEEDVALUE},
     70   1.5     lukem 	{"mode",	F_MODE,		NEEDVALUE},
     71   1.5     lukem 	{"nlink",	F_NLINK,	NEEDVALUE},
     72   1.5     lukem 	{"optional",	F_OPT,		0},
     73   1.5     lukem 	{"size",	F_SIZE,		NEEDVALUE},
     74  1.13     lukem 	{"tags",	F_TAGS,		NEEDVALUE},
     75   1.5     lukem 	{"time",	F_TIME,		NEEDVALUE},
     76   1.5     lukem 	{"type",	F_TYPE,		NEEDVALUE},
     77   1.5     lukem 	{"uid",		F_UID,		NEEDVALUE},
     78   1.5     lukem 	{"uname",	F_UNAME,	NEEDVALUE}
     79   1.1       cgd };
     80   1.1       cgd 
     81  1.10     lukem static KEY typelist[] = {
     82  1.10     lukem 	{"block",	F_BLOCK,	},
     83  1.10     lukem 	{"char",	F_CHAR,		},
     84  1.10     lukem 	{"dir",		F_DIR,		},
     85  1.10     lukem 	{"fifo",	F_FIFO,		},
     86  1.10     lukem 	{"file",	F_FILE,		},
     87  1.10     lukem 	{"link",	F_LINK,		},
     88  1.10     lukem 	{"socket",	F_SOCK,		},
     89  1.10     lukem };
     90  1.10     lukem 
     91   1.9    simonb int keycompare(const void *, const void *);
     92   1.5     lukem 
     93   1.1       cgd u_int
     94  1.10     lukem parsekey(const char *name, int *needvaluep)
     95   1.1       cgd {
     96  1.15     lukem 	static int allbits;
     97   1.1       cgd 	KEY *k, tmp;
     98   1.1       cgd 
     99  1.15     lukem 	if (allbits == 0) {
    100  1.15     lukem 		int i;
    101  1.15     lukem 
    102  1.15     lukem 		for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
    103  1.15     lukem 			allbits |= keylist[i].val;
    104  1.15     lukem 	}
    105   1.1       cgd 	tmp.name = name;
    106  1.15     lukem 	if (strcmp(name, "all") == 0)
    107  1.15     lukem 		return (allbits);
    108   1.1       cgd 	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
    109   1.1       cgd 	    sizeof(KEY), keycompare);
    110   1.1       cgd 	if (k == NULL)
    111  1.13     lukem 		mtree_err("unknown keyword `%s'", name);
    112   1.1       cgd 
    113   1.1       cgd 	if (needvaluep)
    114   1.1       cgd 		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
    115  1.10     lukem 
    116  1.10     lukem 	return (k->val);
    117  1.10     lukem }
    118  1.10     lukem 
    119  1.10     lukem u_int
    120  1.10     lukem parsetype(const char *name)
    121  1.10     lukem {
    122  1.10     lukem 	KEY *k, tmp;
    123  1.10     lukem 
    124  1.10     lukem 	tmp.name = name;
    125  1.10     lukem 	k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
    126  1.10     lukem 	    sizeof(KEY), keycompare);
    127  1.10     lukem 	if (k == NULL)
    128  1.13     lukem 		mtree_err("unknown file type `%s'", name);
    129   1.8     jwise 
    130   1.1       cgd 	return (k->val);
    131   1.1       cgd }
    132   1.1       cgd 
    133   1.1       cgd int
    134   1.9    simonb keycompare(const void *a, const void *b)
    135   1.1       cgd {
    136   1.9    simonb 
    137   1.1       cgd 	return (strcmp(((KEY *)a)->name, ((KEY *)b)->name));
    138   1.1       cgd }
    139   1.1       cgd 
    140   1.1       cgd void
    141   1.6  wsanchez mtree_err(const char *fmt, ...)
    142   1.1       cgd {
    143   1.1       cgd 	va_list ap;
    144   1.9    simonb 
    145   1.1       cgd 	va_start(ap, fmt);
    146   1.1       cgd 	(void)fprintf(stderr, "mtree: ");
    147   1.1       cgd 	(void)vfprintf(stderr, fmt, ap);
    148   1.1       cgd 	va_end(ap);
    149   1.1       cgd 	(void)fprintf(stderr, "\n");
    150   1.1       cgd 	if (lineno)
    151   1.1       cgd 		(void)fprintf(stderr,
    152  1.12   thorpej 		    "mtree: failed at line %lu of the specification\n",
    153  1.12   thorpej 		    (u_long) lineno);
    154   1.1       cgd 	exit(1);
    155   1.1       cgd 	/* NOTREACHED */
    156  1.14     lukem }
    157  1.14     lukem 
    158  1.14     lukem void
    159  1.14     lukem addtag(slist_t *list, char *elem)
    160  1.14     lukem {
    161  1.14     lukem 
    162  1.14     lukem #define	TAG_CHUNK 20
    163  1.14     lukem 
    164  1.14     lukem 	if ((list->count % TAG_CHUNK) == 0) {
    165  1.14     lukem 		char **new;
    166  1.14     lukem 
    167  1.14     lukem 		new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
    168  1.14     lukem 		    * sizeof(char *));
    169  1.14     lukem 		if (new == NULL)
    170  1.14     lukem 			mtree_err("memory allocation error");
    171  1.14     lukem 		list->list = new;
    172  1.14     lukem 	}
    173  1.14     lukem 	list->list[list->count] = elem;
    174  1.14     lukem 	list->count++;
    175  1.14     lukem }
    176  1.14     lukem 
    177  1.14     lukem void
    178  1.14     lukem parsetags(slist_t *list, char *args)
    179  1.14     lukem {
    180  1.14     lukem 	char	*p, *e;
    181  1.14     lukem 	int	len;
    182  1.14     lukem 
    183  1.14     lukem 	if (args == NULL) {
    184  1.14     lukem 		addtag(list, NULL);
    185  1.14     lukem 		return;
    186  1.14     lukem 	}
    187  1.14     lukem 	while ((p = strsep(&args, ",")) != NULL) {
    188  1.14     lukem 		if (*p == '\0')
    189  1.14     lukem 			continue;
    190  1.14     lukem 		len = strlen(p) + 3;	/* "," + p + ",\0" */
    191  1.14     lukem 		if ((e = malloc(len)) == NULL)
    192  1.14     lukem 			mtree_err("memory allocation error");
    193  1.14     lukem 		snprintf(e, len, ",%s,", p);
    194  1.14     lukem 		addtag(list, e);
    195  1.14     lukem 	}
    196  1.14     lukem }
    197  1.14     lukem 
    198  1.14     lukem /*
    199  1.14     lukem  * matchtags
    200  1.14     lukem  *	returns 0 if there's a match from the exclude list in the node's tags,
    201  1.14     lukem  *	or there's an include list and no match.
    202  1.14     lukem  *	return 1 otherwise.
    203  1.14     lukem  */
    204  1.14     lukem int
    205  1.14     lukem matchtags(NODE *node)
    206  1.14     lukem {
    207  1.14     lukem 	int	i;
    208  1.14     lukem 
    209  1.14     lukem 	if (node->tags) {
    210  1.14     lukem 		for (i = 0; i < excludetags.count; i++)
    211  1.14     lukem 			if (strstr(node->tags, excludetags.list[i]))
    212  1.14     lukem 				break;
    213  1.14     lukem 		if (i < excludetags.count)
    214  1.14     lukem 			return (0);
    215  1.14     lukem 
    216  1.14     lukem 		for (i = 0; i < includetags.count; i++)
    217  1.14     lukem 			if (strstr(node->tags, includetags.list[i]))
    218  1.14     lukem 				break;
    219  1.14     lukem 		if (i > 0 && i == includetags.count)
    220  1.14     lukem 			return (0);
    221  1.14     lukem 	} else if (includetags.count > 0) {
    222  1.14     lukem 		return (0);
    223  1.14     lukem 	}
    224  1.14     lukem 	return (1);
    225   1.1       cgd }
    226