Home | History | Annotate | Line # | Download | only in mtree
mtree.c revision 1.20
      1 /*	$NetBSD: mtree.c,v 1.20 2001/10/05 01:03:24 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.20 2001/10/05 01:03:24 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 slist_t	excludetags, includetags;
     68 
     69 	int	main(int, char **);
     70 static	void	usage(void);
     71 
     72 int
     73 main(int argc, char **argv)
     74 {
     75 	int	ch, status;
     76 	char	*dir, *p;
     77 
     78 	setprogname(argv[0]);
     79 
     80 	dir = NULL;
     81 	keys = KEYDEFAULT;
     82 	while ((ch = getopt(argc, argv, "cdDeE:f:I:iK:k:lmp:rR:s:tUux")) != -1)
     83 		switch((char)ch) {
     84 		case 'c':
     85 			cflag = 1;
     86 			break;
     87 		case 'd':
     88 			dflag = 1;
     89 			break;
     90 		case 'D':
     91 			Dflag = 1;
     92 			break;
     93 		case 'E':
     94 			parsetags(&excludetags, optarg);
     95 			break;
     96 		case 'e':
     97 			eflag = 1;
     98 			break;
     99 		case 'f':
    100 			if (!(freopen(optarg, "r", stdin)))
    101 				mtree_err("%s: %s", optarg, strerror(errno));
    102 			break;
    103 		case 'I':
    104 			parsetags(&includetags, optarg);
    105 			break;
    106 		case 'i':
    107 			iflag = 1;
    108 			break;
    109 		case 'K':
    110 			while ((p = strsep(&optarg, " \t,")) != NULL)
    111 				if (*p != '\0')
    112 					keys |= parsekey(p, NULL);
    113 			break;
    114 		case 'k':
    115 			keys = F_TYPE;
    116 			while ((p = strsep(&optarg, " \t,")) != NULL)
    117 				if (*p != '\0')
    118 					keys |= parsekey(p, NULL);
    119 			break;
    120 		case 'l':
    121 			lflag = 1;
    122 			break;
    123 		case 'm':
    124 			mflag = 1;
    125 			break;
    126 		case 'p':
    127 			dir = optarg;
    128 			break;
    129 		case 'r':
    130 			rflag = 1;
    131 			break;
    132 		case 'R':
    133 			while ((p = strsep(&optarg, " \t,")) != NULL)
    134 				if (*p != '\0')
    135 					keys &= ~parsekey(p, NULL);
    136 			break;
    137 		case 's':
    138 			sflag = 1;
    139 			crc_total = ~strtol(optarg, &p, 0);
    140 			if (*p)
    141 				mtree_err("illegal seed value -- %s", optarg);
    142 			break;
    143 		case 't':
    144 			tflag = 1;
    145 			break;
    146 		case 'U':
    147 			Uflag = uflag = 1;
    148 			break;
    149 		case 'u':
    150 			uflag = 1;
    151 			break;
    152 		case 'x':
    153 			ftsoptions |= FTS_XDEV;
    154 			break;
    155 		case '?':
    156 		default:
    157 			usage();
    158 		}
    159 	argc -= optind;
    160 	argv += optind;
    161 
    162 	if (argc)
    163 		usage();
    164 
    165 	if (dir && chdir(dir))
    166 		mtree_err("%s: %s", dir, strerror(errno));
    167 
    168 	if ((cflag || sflag) && !getcwd(fullpath, MAXPATHLEN))
    169 		mtree_err("%s", strerror(errno));
    170 
    171 	if (cflag == 1 && Dflag == 1)
    172 		mtree_err("-c and -D flags are mutually exclusive");
    173 
    174 	if (iflag == 1 && mflag == 1)
    175 		mtree_err("-i and -m flags are mutually exclusive");
    176 
    177 	if (lflag == 1 && uflag == 1)
    178 		mtree_err("-l and -u flags are mutually exclusive");
    179 
    180 	if (cflag) {
    181 		cwalk();
    182 		exit(0);
    183 	}
    184 	if (Dflag) {
    185 		dump_nodes("", spec());
    186 		exit(0);
    187 	}
    188 	status = verify();
    189 	if (Uflag & (status == MISMATCHEXIT))
    190 		status = 0;
    191 	exit(status);
    192 }
    193 
    194 static void
    195 usage(void)
    196 {
    197 
    198 	(void)fprintf(stderr, "usage: mtree [-cdDelrUux] [-i|-m] [-f spec]"
    199 	    " [-k key] [-K addkey] [-R removekey]\n"
    200 	    "\t\t[-I inctags] [-E exctags] [-p path] [-s seed]\n");
    201 	exit(1);
    202 }
    203