Home | History | Annotate | Line # | Download | only in mtree
mtree.c revision 1.19
      1 /*	$NetBSD: mtree.c,v 1.19 2001/10/04 04:51:27 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1989, 1990, 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 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1989, 1990, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)mtree.c	8.1 (Berkeley) 6/6/93";
     45 #else
     46 __RCSID("$NetBSD: mtree.c,v 1.19 2001/10/04 04:51:27 lukem Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/stat.h>
     52 
     53 #include <errno.h>
     54 #include <fts.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <unistd.h>
     58 
     59 #include "mtree.h"
     60 #include "extern.h"
     61 
     62 int	ftsoptions = FTS_PHYSICAL;
     63 int	cflag, dflag, Dflag, eflag, iflag, lflag, mflag,
     64     	rflag, sflag, tflag, uflag, Uflag;
     65 int	keys;
     66 char	fullpath[MAXPATHLEN];
     67 char	**excludetags, **includetags;
     68 
     69 static	void	addtag(char ***, int *, char *);
     70 	int	main(int, char **);
     71 static	void	parsetags(char ***, int *, char *);
     72 static	void	usage(void);
     73 
     74 int
     75 main(int argc, char **argv)
     76 {
     77 	int	ch, status, etagc, itagc;
     78 	char	*dir, *p;
     79 
     80 	setprogname(argv[0]);
     81 
     82 	etagc = itagc = 0;
     83 	dir = NULL;
     84 	keys = KEYDEFAULT;
     85 	while ((ch = getopt(argc, argv, "cdDeE:f:I:iK:k:lmp:rR:s:tUux")) != -1)
     86 		switch((char)ch) {
     87 		case 'c':
     88 			cflag = 1;
     89 			break;
     90 		case 'd':
     91 			dflag = 1;
     92 			break;
     93 		case 'D':
     94 			Dflag = 1;
     95 			break;
     96 		case 'E':
     97 			parsetags(&excludetags, &etagc, optarg);
     98 			break;
     99 		case 'e':
    100 			eflag = 1;
    101 			break;
    102 		case 'f':
    103 			if (!(freopen(optarg, "r", stdin)))
    104 				mtree_err("%s: %s", optarg, strerror(errno));
    105 			break;
    106 		case 'I':
    107 			parsetags(&includetags, &itagc, optarg);
    108 			break;
    109 		case 'i':
    110 			iflag = 1;
    111 			break;
    112 		case 'K':
    113 			while ((p = strsep(&optarg, " \t,")) != NULL)
    114 				if (*p != '\0')
    115 					keys |= parsekey(p, NULL);
    116 			break;
    117 		case 'k':
    118 			keys = F_TYPE;
    119 			while ((p = strsep(&optarg, " \t,")) != NULL)
    120 				if (*p != '\0')
    121 					keys |= parsekey(p, NULL);
    122 			break;
    123 		case 'l':
    124 			lflag = 1;
    125 			break;
    126 		case 'm':
    127 			mflag = 1;
    128 			break;
    129 		case 'p':
    130 			dir = optarg;
    131 			break;
    132 		case 'r':
    133 			rflag = 1;
    134 			break;
    135 		case 'R':
    136 			while ((p = strsep(&optarg, " \t,")) != NULL)
    137 				if (*p != '\0')
    138 					keys &= ~parsekey(p, NULL);
    139 			break;
    140 		case 's':
    141 			sflag = 1;
    142 			crc_total = ~strtol(optarg, &p, 0);
    143 			if (*p)
    144 				mtree_err("illegal seed value -- %s", optarg);
    145 			break;
    146 		case 't':
    147 			tflag = 1;
    148 			break;
    149 		case 'U':
    150 			Uflag = uflag = 1;
    151 			break;
    152 		case 'u':
    153 			uflag = 1;
    154 			break;
    155 		case 'x':
    156 			ftsoptions |= FTS_XDEV;
    157 			break;
    158 		case '?':
    159 		default:
    160 			usage();
    161 		}
    162 	argc -= optind;
    163 	argv += optind;
    164 
    165 	if (argc)
    166 		usage();
    167 
    168 	addtag(&excludetags, &etagc, NULL);
    169 	addtag(&includetags, &itagc, NULL);
    170 
    171 	if (dir && chdir(dir))
    172 		mtree_err("%s: %s", dir, strerror(errno));
    173 
    174 	if ((cflag || sflag) && !getcwd(fullpath, MAXPATHLEN))
    175 		mtree_err("%s", strerror(errno));
    176 
    177 	if (cflag == 1 && Dflag == 1)
    178 		mtree_err("-c and -D flags are mutually exclusive");
    179 
    180 	if (iflag == 1 && mflag == 1)
    181 		mtree_err("-i and -m flags are mutually exclusive");
    182 
    183 	if (lflag == 1 && uflag == 1)
    184 		mtree_err("-l and -u flags are mutually exclusive");
    185 
    186 	if (cflag) {
    187 		cwalk();
    188 		exit(0);
    189 	}
    190 	if (Dflag) {
    191 		dump_nodes("", spec());
    192 		exit(0);
    193 	}
    194 	status = verify();
    195 	if (Uflag & (status == MISMATCHEXIT))
    196 		status = 0;
    197 	exit(status);
    198 }
    199 
    200 
    201 static void
    202 addtag(char ***list, int *count, char *elem)
    203 {
    204 
    205 #define	TAG_CHUNK 20
    206 
    207 	if ((*count % TAG_CHUNK) == 0) {
    208 		char **new;
    209 
    210 		new = (char **)realloc(*list, (*count + TAG_CHUNK)
    211 		    * sizeof(char *));
    212 		if (new == NULL)
    213 			mtree_err("memory allocation error");
    214 		*list = new;
    215 	}
    216 	(*list)[*count] = elem;
    217 	(*count)++;
    218 }
    219 
    220 static void
    221 parsetags(char ***list, int *count, char *args)
    222 {
    223 	char	*p, *e;
    224 	int	len;
    225 
    226 	if (args == NULL) {
    227 		addtag(list, count, NULL);
    228 		return;
    229 	}
    230 	while ((p = strsep(&args, ",")) != NULL) {
    231 		if (*p == '\0')
    232 			continue;
    233 		len = strlen(p) + 3;	/* "," + p + ",\0" */
    234 		if ((e = malloc(len)) == NULL)
    235 			mtree_err("memory allocation error");
    236 		snprintf(e, len, ",%s,", p);
    237 		addtag(list, count, e);
    238 	}
    239 }
    240 
    241 static void
    242 usage(void)
    243 {
    244 
    245 	(void)fprintf(stderr, "usage: mtree [-cdDelrUux] [-i|-m] [-f spec]"
    246 	    " [-k key] [-K addkey] [-R removekey]\n"
    247 	    "\t\t[-I inctags] [-E exctags] [-p path] [-s seed]\n");
    248 	exit(1);
    249 }
    250