Home | History | Annotate | Line # | Download | only in mtree
mtree.c revision 1.41
      1 /*	$NetBSD: mtree.c,v 1.41 2012/10/05 01:26:56 christos 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #if HAVE_NBTOOL_CONFIG_H
     33 #include "nbtool_config.h"
     34 #endif
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(__COPYRIGHT) && !defined(lint)
     38 __COPYRIGHT("@(#) Copyright (c) 1989, 1990, 1993\
     39  The Regents of the University of California.  All rights reserved.");
     40 #endif /* not lint */
     41 
     42 #if defined(__RCSID) && !defined(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.41 2012/10/05 01:26:56 christos 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 <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <unistd.h>
     58 
     59 #include "extern.h"
     60 
     61 int	ftsoptions = FTS_PHYSICAL;
     62 int	cflag, Cflag, dflag, Dflag, eflag, iflag, jflag, lflag, mflag,
     63     	nflag, qflag, rflag, sflag, tflag, uflag, Uflag;
     64 char	fullpath[MAXPATHLEN];
     65 
     66 __dead static	void	usage(void);
     67 
     68 int
     69 main(int argc, char **argv)
     70 {
     71 	int	ch, status;
     72 	char	*dir, *p;
     73 	FILE	*spec1, *spec2;
     74 
     75 	setprogname(argv[0]);
     76 
     77 	dir = NULL;
     78 	init_excludes();
     79 	spec1 = stdin;
     80 	spec2 = NULL;
     81 
     82 	while ((ch = getopt(argc, argv,
     83 	    "cCdDeE:f:I:ik:K:lLmMnN:p:PqrR:s:StuUWxX:"))
     84 	    != -1) {
     85 		switch((char)ch) {
     86 		case 'c':
     87 			cflag = 1;
     88 			break;
     89 		case 'C':
     90 			Cflag = 1;
     91 			break;
     92 		case 'd':
     93 			dflag = 1;
     94 			break;
     95 		case 'D':
     96 			Dflag = 1;
     97 			break;
     98 		case 'E':
     99 			parsetags(&excludetags, optarg);
    100 			break;
    101 		case 'e':
    102 			eflag = 1;
    103 			break;
    104 		case 'f':
    105 			if (spec1 == stdin) {
    106 				spec1 = fopen(optarg, "r");
    107 				if (spec1 == NULL)
    108 					mtree_err("%s: %s", optarg,
    109 					    strerror(errno));
    110 			} else if (spec2 == NULL) {
    111 				spec2 = fopen(optarg, "r");
    112 				if (spec2 == NULL)
    113 					mtree_err("%s: %s", optarg,
    114 					    strerror(errno));
    115 			} else
    116 				usage();
    117 			break;
    118 		case 'i':
    119 			iflag = 1;
    120 			break;
    121 		case 'I':
    122 			parsetags(&includetags, optarg);
    123 			break;
    124 		case 'j':
    125 			jflag = 1;
    126 			break;
    127 		case 'k':
    128 			keys = F_TYPE;
    129 			while ((p = strsep(&optarg, " \t,")) != NULL)
    130 				if (*p != '\0')
    131 					keys |= parsekey(p, NULL);
    132 			break;
    133 		case 'K':
    134 			while ((p = strsep(&optarg, " \t,")) != NULL)
    135 				if (*p != '\0')
    136 					keys |= parsekey(p, NULL);
    137 			break;
    138 		case 'l':
    139 			lflag = 1;
    140 			break;
    141 		case 'L':
    142 			ftsoptions &= ~FTS_PHYSICAL;
    143 			ftsoptions |= FTS_LOGICAL;
    144 			break;
    145 		case 'm':
    146 			mflag = 1;
    147 			break;
    148 		case 'M':
    149 			mtree_Mflag = 1;
    150 			break;
    151 		case 'n':
    152 			nflag = 1;
    153 			break;
    154 		case 'N':
    155 			if (! setup_getid(optarg))
    156 				mtree_err(
    157 			    "Unable to use user and group databases in `%s'",
    158 				    optarg);
    159 			break;
    160 		case 'p':
    161 			dir = optarg;
    162 			break;
    163 		case 'P':
    164 			ftsoptions &= ~FTS_LOGICAL;
    165 			ftsoptions |= FTS_PHYSICAL;
    166 			break;
    167 		case 'q':
    168 			qflag = 1;
    169 			break;
    170 		case 'r':
    171 			rflag = 1;
    172 			break;
    173 		case 'R':
    174 			while ((p = strsep(&optarg, " \t,")) != NULL)
    175 				if (*p != '\0')
    176 					keys &= ~parsekey(p, NULL);
    177 			break;
    178 		case 's':
    179 			sflag = 1;
    180 			crc_total = ~strtol(optarg, &p, 0);
    181 			if (*p)
    182 				mtree_err("illegal seed value -- %s", optarg);
    183 			break;
    184 		case 'S':
    185 			mtree_Sflag = 1;
    186 			break;
    187 		case 't':
    188 			tflag = 1;
    189 			break;
    190 		case 'u':
    191 			uflag = 1;
    192 			break;
    193 		case 'U':
    194 			Uflag = uflag = 1;
    195 			break;
    196 		case 'W':
    197 			mtree_Wflag = 1;
    198 			break;
    199 		case 'x':
    200 			ftsoptions |= FTS_XDEV;
    201 			break;
    202 		case 'X':
    203 			read_excludes_file(optarg);
    204 			break;
    205 		case '?':
    206 		default:
    207 			usage();
    208 		}
    209 	}
    210 	argc -= optind;
    211 	argv += optind;
    212 
    213 	if (argc)
    214 		usage();
    215 
    216 	if (spec2 && (cflag || Cflag || Dflag))
    217 		mtree_err("Double -f, -c, -C and -D flags are mutually "
    218 		    "exclusive");
    219 
    220 	if (dir && spec2)
    221 		mtree_err("Double -f and -p flags are mutually exclusive");
    222 
    223 	if (dir && chdir(dir))
    224 		mtree_err("%s: %s", dir, strerror(errno));
    225 
    226 	if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
    227 		mtree_err("%s", strerror(errno));
    228 
    229 	if ((cflag && Cflag) || (cflag && Dflag) || (Cflag && Dflag))
    230 		mtree_err("-c, -C and -D flags are mutually exclusive");
    231 
    232 	if (iflag && mflag)
    233 		mtree_err("-i and -m flags are mutually exclusive");
    234 
    235 	if (lflag && uflag)
    236 		mtree_err("-l and -u flags are mutually exclusive");
    237 
    238 	if (cflag) {
    239 		cwalk();
    240 		exit(0);
    241 	}
    242 	if (Cflag || Dflag) {
    243 		dump_nodes("", spec(spec1), Dflag);
    244 		exit(0);
    245 	}
    246 	if (spec2 != NULL)
    247 		status = mtree_specspec(spec1, spec2);
    248 	else
    249 		status = verify(spec1);
    250 	if (Uflag && (status == MISMATCHEXIT))
    251 		status = 0;
    252 	exit(status);
    253 }
    254 
    255 static void
    256 usage(void)
    257 {
    258 
    259 	fprintf(stderr,
    260 	    "usage: %s [-CcDdeLlMnPrSUuWx] [-i|-m] [-E tags]\n"
    261 	    "\t\t[-f spec] [-f spec]\n"
    262 	    "\t\t[-I tags] [-K keywords] [-k keywords] [-N dbdir] [-p path]\n"
    263 	    "\t\t[-R keywords] [-s seed] [-X exclude-file]\n",
    264 	    getprogname());
    265 	exit(1);
    266 }
    267