Home | History | Annotate | Line # | Download | only in mtree
mtree.c revision 1.17
      1 /*	$NetBSD: mtree.c,v 1.17 2001/09/22 03:56:29 perry 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.17 2001/09/22 03:56:29 perry 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 <unistd.h>
     57 
     58 #include "mtree.h"
     59 #include "extern.h"
     60 
     61 extern int crc_total;
     62 
     63 int ftsoptions = FTS_PHYSICAL;
     64 int cflag, dflag, eflag, iflag, lflag, mflag,
     65     rflag, sflag, tflag, uflag, Uflag;
     66 int keys;
     67 char fullpath[MAXPATHLEN];
     68 
     69 	int	main(int, char **);
     70 static	void	usage(void);
     71 
     72 int
     73 main(int argc, char **argv)
     74 {
     75 	int ch;
     76 	char *dir, *p;
     77 	int status;
     78 
     79 	setprogname(argv[0]);
     80 
     81 	dir = NULL;
     82 	keys = KEYDEFAULT;
     83 	while ((ch = getopt(argc, argv, "cdef:iK:k:lmp:rs:tUux")) != -1)
     84 		switch((char)ch) {
     85 		case 'c':
     86 			cflag = 1;
     87 			break;
     88 		case 'd':
     89 			dflag = 1;
     90 			break;
     91 		case 'e':
     92 			eflag = 1;
     93 			break;
     94 		case 'f':
     95 			if (!(freopen(optarg, "r", stdin)))
     96 				mtree_err("%s: %s", optarg, strerror(errno));
     97 			break;
     98 		case 'i':
     99 			iflag = 1;
    100 			break;
    101 		case 'K':
    102 			while ((p = strsep(&optarg, " \t,")) != NULL)
    103 				if (*p != '\0')
    104 					keys |= parsekey(p, NULL);
    105 			break;
    106 		case 'k':
    107 			keys = F_TYPE;
    108 			while ((p = strsep(&optarg, " \t,")) != NULL)
    109 				if (*p != '\0')
    110 					keys |= parsekey(p, NULL);
    111 			break;
    112 		case 'l':
    113 			lflag = 1;
    114 			break;
    115 		case 'm':
    116 			mflag = 1;
    117 			break;
    118 		case 'p':
    119 			dir = optarg;
    120 			break;
    121 		case 'r':
    122 			rflag = 1;
    123 			break;
    124 		case 's':
    125 			sflag = 1;
    126 			crc_total = ~strtol(optarg, &p, 0);
    127 			if (*p)
    128 				mtree_err("illegal seed value -- %s", optarg);
    129 			break;
    130 		case 't':
    131 			tflag = 1;
    132 			break;
    133 		case 'U':
    134 			Uflag = uflag = 1;
    135 			break;
    136 		case 'u':
    137 			uflag = 1;
    138 			break;
    139 		case 'x':
    140 			ftsoptions |= FTS_XDEV;
    141 			break;
    142 		case '?':
    143 		default:
    144 			usage();
    145 		}
    146 	argc -= optind;
    147 	argv += optind;
    148 
    149 	if (argc)
    150 		usage();
    151 
    152 	if (dir && chdir(dir))
    153 		mtree_err("%s: %s", dir, strerror(errno));
    154 
    155 	if ((cflag || sflag) && !getcwd(fullpath, MAXPATHLEN))
    156 		mtree_err("%s", strerror(errno));
    157 
    158 	if (iflag == 1 && mflag == 1)
    159 		mtree_err("-i and -m flags are mutually exclusive");
    160 
    161 	if (lflag == 1 && uflag == 1)
    162 		mtree_err("-l and -u flags are mutually exclusive");
    163 
    164 	if (cflag) {
    165 		cwalk();
    166 		exit(0);
    167 	}
    168 	status = verify();
    169 	if (Uflag & (status == MISMATCHEXIT))
    170 		status = 0;
    171 	exit(status);
    172 }
    173 
    174 static void
    175 usage(void)
    176 {
    177 
    178 	(void)fprintf(stderr, "usage: mtree [-cdelrUux] [-i|-m] [-f spec]"
    179 	    " [-K key] [-k key] [-p path] [-s seed]\n");
    180 	exit(1);
    181 }
    182