Home | History | Annotate | Line # | Download | only in mtree
misc.c revision 1.25
      1  1.25       jmc /*	$NetBSD: misc.c,v 1.25 2004/06/20 22:20:18 jmc 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.24       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  *
     31   1.2       cgd  *	@(#)misc.c	8.1 (Berkeley) 6/6/93
     32   1.1       cgd  */
     33   1.1       cgd 
     34  1.25       jmc #if HAVE_NBTOOL_CONFIG_H
     35  1.25       jmc #include "nbtool_config.h"
     36  1.25       jmc #endif
     37  1.25       jmc 
     38   1.5     lukem #include <sys/cdefs.h>
     39  1.22        tv #if defined(__RCSID) && !defined(lint)
     40  1.25       jmc __RCSID("$NetBSD: misc.c,v 1.25 2004/06/20 22:20:18 jmc 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.9    simonb #include <stdarg.h>
     47   1.1       cgd #include <stdio.h>
     48  1.18     lukem #include <stdlib.h>
     49  1.18     lukem #include <string.h>
     50   1.9    simonb 
     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.16     lukem 	{"device",	F_DEV,		NEEDVALUE},
     65   1.7       mrg 	{"flags",	F_FLAGS,	NEEDVALUE},
     66   1.5     lukem 	{"gid",		F_GID,		NEEDVALUE},
     67   1.5     lukem 	{"gname",	F_GNAME,	NEEDVALUE},
     68   1.5     lukem 	{"ignore",	F_IGN,		0},
     69   1.5     lukem 	{"link",	F_SLINK,	NEEDVALUE},
     70   1.8     jwise 	{"md5",		F_MD5,		NEEDVALUE},
     71  1.20     lukem 	{"md5digest",	F_MD5,		NEEDVALUE},
     72   1.5     lukem 	{"mode",	F_MODE,		NEEDVALUE},
     73   1.5     lukem 	{"nlink",	F_NLINK,	NEEDVALUE},
     74   1.5     lukem 	{"optional",	F_OPT,		0},
     75  1.20     lukem 	{"rmd160",	F_RMD160,	NEEDVALUE},
     76  1.20     lukem 	{"rmd160digest",F_RMD160,	NEEDVALUE},
     77  1.20     lukem 	{"sha1",	F_SHA1,		NEEDVALUE},
     78  1.20     lukem 	{"sha1digest",	F_SHA1,		NEEDVALUE},
     79   1.5     lukem 	{"size",	F_SIZE,		NEEDVALUE},
     80  1.13     lukem 	{"tags",	F_TAGS,		NEEDVALUE},
     81   1.5     lukem 	{"time",	F_TIME,		NEEDVALUE},
     82   1.5     lukem 	{"type",	F_TYPE,		NEEDVALUE},
     83   1.5     lukem 	{"uid",		F_UID,		NEEDVALUE},
     84   1.5     lukem 	{"uname",	F_UNAME,	NEEDVALUE}
     85   1.1       cgd };
     86   1.1       cgd 
     87  1.10     lukem static KEY typelist[] = {
     88  1.10     lukem 	{"block",	F_BLOCK,	},
     89  1.10     lukem 	{"char",	F_CHAR,		},
     90  1.10     lukem 	{"dir",		F_DIR,		},
     91  1.10     lukem 	{"fifo",	F_FIFO,		},
     92  1.10     lukem 	{"file",	F_FILE,		},
     93  1.10     lukem 	{"link",	F_LINK,		},
     94  1.10     lukem 	{"socket",	F_SOCK,		},
     95  1.10     lukem };
     96  1.10     lukem 
     97  1.18     lukem slist_t	excludetags, includetags;
     98  1.18     lukem int	keys = KEYDEFAULT;
     99  1.18     lukem 
    100  1.18     lukem 
    101   1.9    simonb int keycompare(const void *, const void *);
    102   1.5     lukem 
    103   1.1       cgd u_int
    104  1.10     lukem parsekey(const char *name, int *needvaluep)
    105   1.1       cgd {
    106  1.15     lukem 	static int allbits;
    107   1.1       cgd 	KEY *k, tmp;
    108   1.1       cgd 
    109  1.15     lukem 	if (allbits == 0) {
    110  1.15     lukem 		int i;
    111  1.15     lukem 
    112  1.15     lukem 		for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++)
    113  1.15     lukem 			allbits |= keylist[i].val;
    114  1.15     lukem 	}
    115   1.1       cgd 	tmp.name = name;
    116  1.15     lukem 	if (strcmp(name, "all") == 0)
    117  1.15     lukem 		return (allbits);
    118   1.1       cgd 	k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY),
    119   1.1       cgd 	    sizeof(KEY), keycompare);
    120   1.1       cgd 	if (k == NULL)
    121  1.13     lukem 		mtree_err("unknown keyword `%s'", name);
    122   1.1       cgd 
    123   1.1       cgd 	if (needvaluep)
    124   1.1       cgd 		*needvaluep = k->flags & NEEDVALUE ? 1 : 0;
    125  1.10     lukem 
    126  1.10     lukem 	return (k->val);
    127  1.10     lukem }
    128  1.10     lukem 
    129  1.10     lukem u_int
    130  1.10     lukem parsetype(const char *name)
    131  1.10     lukem {
    132  1.10     lukem 	KEY *k, tmp;
    133  1.10     lukem 
    134  1.10     lukem 	tmp.name = name;
    135  1.10     lukem 	k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY),
    136  1.10     lukem 	    sizeof(KEY), keycompare);
    137  1.10     lukem 	if (k == NULL)
    138  1.13     lukem 		mtree_err("unknown file type `%s'", name);
    139   1.8     jwise 
    140   1.1       cgd 	return (k->val);
    141   1.1       cgd }
    142   1.1       cgd 
    143   1.1       cgd int
    144   1.9    simonb keycompare(const void *a, const void *b)
    145   1.1       cgd {
    146   1.9    simonb 
    147  1.17     lukem 	return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name));
    148   1.1       cgd }
    149   1.1       cgd 
    150   1.1       cgd void
    151   1.6  wsanchez mtree_err(const char *fmt, ...)
    152   1.1       cgd {
    153   1.1       cgd 	va_list ap;
    154   1.9    simonb 
    155   1.1       cgd 	va_start(ap, fmt);
    156  1.18     lukem 	vwarnx(fmt, ap);
    157   1.1       cgd 	va_end(ap);
    158  1.19     lukem 	if (mtree_lineno)
    159  1.18     lukem 		warnx("failed at line %lu of the specification",
    160  1.19     lukem 		    (u_long) mtree_lineno);
    161   1.1       cgd 	exit(1);
    162   1.1       cgd 	/* NOTREACHED */
    163  1.14     lukem }
    164  1.14     lukem 
    165  1.14     lukem void
    166  1.14     lukem addtag(slist_t *list, char *elem)
    167  1.14     lukem {
    168  1.14     lukem 
    169  1.14     lukem #define	TAG_CHUNK 20
    170  1.14     lukem 
    171  1.14     lukem 	if ((list->count % TAG_CHUNK) == 0) {
    172  1.14     lukem 		char **new;
    173  1.14     lukem 
    174  1.14     lukem 		new = (char **)realloc(list->list, (list->count + TAG_CHUNK)
    175  1.14     lukem 		    * sizeof(char *));
    176  1.14     lukem 		if (new == NULL)
    177  1.14     lukem 			mtree_err("memory allocation error");
    178  1.14     lukem 		list->list = new;
    179  1.14     lukem 	}
    180  1.14     lukem 	list->list[list->count] = elem;
    181  1.14     lukem 	list->count++;
    182  1.14     lukem }
    183  1.14     lukem 
    184  1.14     lukem void
    185  1.14     lukem parsetags(slist_t *list, char *args)
    186  1.14     lukem {
    187  1.14     lukem 	char	*p, *e;
    188  1.14     lukem 	int	len;
    189  1.14     lukem 
    190  1.14     lukem 	if (args == NULL) {
    191  1.14     lukem 		addtag(list, NULL);
    192  1.14     lukem 		return;
    193  1.14     lukem 	}
    194  1.14     lukem 	while ((p = strsep(&args, ",")) != NULL) {
    195  1.14     lukem 		if (*p == '\0')
    196  1.14     lukem 			continue;
    197  1.14     lukem 		len = strlen(p) + 3;	/* "," + p + ",\0" */
    198  1.14     lukem 		if ((e = malloc(len)) == NULL)
    199  1.14     lukem 			mtree_err("memory allocation error");
    200  1.14     lukem 		snprintf(e, len, ",%s,", p);
    201  1.14     lukem 		addtag(list, e);
    202  1.14     lukem 	}
    203  1.14     lukem }
    204  1.14     lukem 
    205  1.14     lukem /*
    206  1.14     lukem  * matchtags
    207  1.14     lukem  *	returns 0 if there's a match from the exclude list in the node's tags,
    208  1.14     lukem  *	or there's an include list and no match.
    209  1.14     lukem  *	return 1 otherwise.
    210  1.14     lukem  */
    211  1.14     lukem int
    212  1.14     lukem matchtags(NODE *node)
    213  1.14     lukem {
    214  1.14     lukem 	int	i;
    215  1.14     lukem 
    216  1.14     lukem 	if (node->tags) {
    217  1.14     lukem 		for (i = 0; i < excludetags.count; i++)
    218  1.14     lukem 			if (strstr(node->tags, excludetags.list[i]))
    219  1.14     lukem 				break;
    220  1.20     lukem 		if (i < excludetags.count)
    221  1.14     lukem 			return (0);
    222  1.14     lukem 
    223  1.14     lukem 		for (i = 0; i < includetags.count; i++)
    224  1.14     lukem 			if (strstr(node->tags, includetags.list[i]))
    225  1.14     lukem 				break;
    226  1.14     lukem 		if (i > 0 && i == includetags.count)
    227  1.14     lukem 			return (0);
    228  1.14     lukem 	} else if (includetags.count > 0) {
    229  1.14     lukem 		return (0);
    230  1.14     lukem 	}
    231  1.14     lukem 	return (1);
    232  1.18     lukem }
    233  1.18     lukem 
    234  1.18     lukem u_int
    235  1.18     lukem nodetoino(u_int type)
    236  1.18     lukem {
    237  1.18     lukem 
    238  1.18     lukem 	switch (type) {
    239  1.18     lukem 	case F_BLOCK:
    240  1.18     lukem 		return S_IFBLK;
    241  1.18     lukem 	case F_CHAR:
    242  1.18     lukem 		return S_IFCHR;
    243  1.18     lukem 	case F_DIR:
    244  1.18     lukem 		return S_IFDIR;
    245  1.18     lukem 	case F_FIFO:
    246  1.18     lukem 		return S_IFIFO;
    247  1.18     lukem 	case F_FILE:
    248  1.18     lukem 		return S_IFREG;
    249  1.18     lukem 	case F_LINK:
    250  1.18     lukem 		return S_IFLNK;
    251  1.25       jmc #ifdef S_IFSOCK
    252  1.18     lukem 	case F_SOCK:
    253  1.18     lukem 		return S_IFSOCK;
    254  1.25       jmc #endif
    255  1.18     lukem 	default:
    256  1.18     lukem 		printf("unknown type %d", type);
    257  1.18     lukem 		abort();
    258  1.18     lukem 	}
    259  1.18     lukem 	/* NOTREACHED */
    260  1.18     lukem }
    261  1.18     lukem 
    262  1.18     lukem const char *
    263  1.18     lukem nodetype(u_int type)
    264  1.18     lukem {
    265  1.18     lukem 
    266  1.18     lukem 	return (inotype(nodetoino(type)));
    267  1.18     lukem }
    268  1.18     lukem 
    269  1.18     lukem 
    270  1.18     lukem const char *
    271  1.18     lukem inotype(u_int type)
    272  1.18     lukem {
    273  1.18     lukem 
    274  1.18     lukem 	switch (type & S_IFMT) {
    275  1.18     lukem 	case S_IFBLK:
    276  1.18     lukem 		return ("block");
    277  1.18     lukem 	case S_IFCHR:
    278  1.18     lukem 		return ("char");
    279  1.18     lukem 	case S_IFDIR:
    280  1.18     lukem 		return ("dir");
    281  1.18     lukem 	case S_IFIFO:
    282  1.18     lukem 		return ("fifo");
    283  1.18     lukem 	case S_IFREG:
    284  1.18     lukem 		return ("file");
    285  1.18     lukem 	case S_IFLNK:
    286  1.18     lukem 		return ("link");
    287  1.25       jmc #ifdef S_IFSOCK
    288  1.18     lukem 	case S_IFSOCK:
    289  1.18     lukem 		return ("socket");
    290  1.25       jmc #endif
    291  1.18     lukem 	default:
    292  1.18     lukem 		return ("unknown");
    293  1.18     lukem 	}
    294  1.18     lukem 	/* NOTREACHED */
    295   1.1       cgd }
    296