11553Srgrimes/*-
21553Srgrimes * Copyright (c) 1989, 1990, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
13121300Sphk * 3. Neither the name of the University nor the names of its contributors
141553Srgrimes *    may be used to endorse or promote products derived from this software
151553Srgrimes *    without specific prior written permission.
161553Srgrimes *
171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271553Srgrimes * SUCH DAMAGE.
281553Srgrimes */
291553Srgrimes
30114601Sobrien#if 0
311553Srgrimes#ifndef lint
3230027Scharnierstatic const char copyright[] =
331553Srgrimes"@(#) Copyright (c) 1989, 1990, 1993\n\
341553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
351553Srgrimes#endif /* not lint */
361553Srgrimes
371553Srgrimes#ifndef lint
381553Srgrimesstatic char sccsid[] = "@(#)mtree.c	8.1 (Berkeley) 6/6/93";
39114601Sobrien#endif /* not lint */
4030027Scharnier#endif
41114601Sobrien#include <sys/cdefs.h>
42114601Sobrien__FBSDID("$FreeBSD$");
431553Srgrimes
441553Srgrimes#include <sys/param.h>
451553Srgrimes#include <sys/stat.h>
4630027Scharnier#include <err.h>
471553Srgrimes#include <errno.h>
4830027Scharnier#include <fts.h>
4930027Scharnier#include <stdio.h>
501553Srgrimes#include <unistd.h>
511553Srgrimes#include "mtree.h"
521553Srgrimes#include "extern.h"
531553Srgrimes
5465882Sacheint ftsoptions = FTS_PHYSICAL;
55241777Sedint dflag, eflag, iflag, nflag, qflag, rflag, sflag, uflag, wflag;
56241777Sedstatic int cflag, Uflag;
5736670Speteru_int keys;
581553Srgrimeschar fullpath[MAXPATHLEN];
591553Srgrimes
6099800Salfredstatic void usage(void);
611553Srgrimes
621553Srgrimesint
6399802Salfredmain(int argc, char *argv[])
641553Srgrimes{
651553Srgrimes	int ch;
661553Srgrimes	char *dir, *p;
673468Srgrimes	int status;
68122141Sphk	FILE *spec1, *spec2;
691553Srgrimes
701553Srgrimes	dir = NULL;
711553Srgrimes	keys = KEYDEFAULT;
7260418Swollman	init_excludes();
73122141Sphk	spec1 = stdin;
74122141Sphk	spec2 = NULL;
7560418Swollman
76124389Sphk	while ((ch = getopt(argc, argv, "cdef:iK:k:LnPp:qrs:UuwxX:")) != -1)
771553Srgrimes		switch((char)ch) {
781553Srgrimes		case 'c':
791553Srgrimes			cflag = 1;
801553Srgrimes			break;
811553Srgrimes		case 'd':
821553Srgrimes			dflag = 1;
831553Srgrimes			break;
841553Srgrimes		case 'e':
851553Srgrimes			eflag = 1;
861553Srgrimes			break;
871553Srgrimes		case 'f':
88122141Sphk			if (spec1 == stdin) {
89122141Sphk				spec1 = fopen(optarg, "r");
90122141Sphk				if (spec1 == NULL)
91122141Sphk					err(1, "%s", optarg);
92122141Sphk			} else if (spec2 == NULL) {
93122141Sphk				spec2 = fopen(optarg, "r");
94122141Sphk				if (spec2 == NULL)
95122141Sphk					err(1, "%s", optarg);
96122141Sphk			} else
97122141Sphk				usage();
981553Srgrimes			break;
992860Srgrimes		case 'i':
1002860Srgrimes			iflag = 1;
1012860Srgrimes			break;
1021553Srgrimes		case 'K':
1031553Srgrimes			while ((p = strsep(&optarg, " \t,")) != NULL)
1041553Srgrimes				if (*p != '\0')
1051553Srgrimes					keys |= parsekey(p, NULL);
1061553Srgrimes			break;
1071553Srgrimes		case 'k':
1081553Srgrimes			keys = F_TYPE;
1091553Srgrimes			while ((p = strsep(&optarg, " \t,")) != NULL)
1101553Srgrimes				if (*p != '\0')
1111553Srgrimes					keys |= parsekey(p, NULL);
1121553Srgrimes			break;
11365882Sache		case 'L':
11465882Sache			ftsoptions &= ~FTS_PHYSICAL;
11565882Sache			ftsoptions |= FTS_LOGICAL;
11665882Sache			break;
1172860Srgrimes		case 'n':
1182860Srgrimes			nflag = 1;
1192860Srgrimes			break;
12066357Sache		case 'P':
12166357Sache			ftsoptions &= ~FTS_LOGICAL;
12266357Sache			ftsoptions |= FTS_PHYSICAL;
12366357Sache			break;
1241553Srgrimes		case 'p':
1251553Srgrimes			dir = optarg;
1261553Srgrimes			break;
12763853Simp		case 'q':
12863853Simp			qflag = 1;
12963853Simp			break;
1301553Srgrimes		case 'r':
1311553Srgrimes			rflag = 1;
1321553Srgrimes			break;
1331553Srgrimes		case 's':
1341553Srgrimes			sflag = 1;
135112214Srobert			crc_total = ~strtoul(optarg, &p, 0);
1361553Srgrimes			if (*p)
13730027Scharnier				errx(1, "illegal seed value -- %s", optarg);
138112403Stobez			break;
1393468Srgrimes		case 'U':
1403468Srgrimes			Uflag = 1;
1413468Srgrimes			uflag = 1;
1423468Srgrimes			break;
1431553Srgrimes		case 'u':
1441553Srgrimes			uflag = 1;
1451553Srgrimes			break;
146124389Sphk		case 'w':
147124389Sphk			wflag = 1;
148124389Sphk			break;
1491553Srgrimes		case 'x':
1501553Srgrimes			ftsoptions |= FTS_XDEV;
1511553Srgrimes			break;
15260418Swollman		case 'X':
15360418Swollman			read_excludes_file(optarg);
15460418Swollman			break;
1551553Srgrimes		case '?':
1561553Srgrimes		default:
1571553Srgrimes			usage();
1581553Srgrimes		}
1591553Srgrimes	argc -= optind;
1601553Srgrimes	argv += optind;
1611553Srgrimes
1621553Srgrimes	if (argc)
1631553Srgrimes		usage();
1641553Srgrimes
1651553Srgrimes	if (dir && chdir(dir))
16630027Scharnier		err(1, "%s", dir);
1671553Srgrimes
168173282Skeramida	if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
16930027Scharnier		errx(1, "%s", fullpath);
1701553Srgrimes
1711553Srgrimes	if (cflag) {
1721553Srgrimes		cwalk();
1731553Srgrimes		exit(0);
1741553Srgrimes	}
175122141Sphk	if (spec2 != NULL)
176122141Sphk		status = mtree_specspec(spec1, spec2);
177122141Sphk	else
178122141Sphk		status = mtree_verifyspec(spec1);
1793468Srgrimes	if (Uflag & (status == MISMATCHEXIT))
1803468Srgrimes		status = 0;
1813468Srgrimes	exit(status);
1821553Srgrimes}
1831553Srgrimes
1841553Srgrimesstatic void
185121299Sphkusage(void)
1861553Srgrimes{
1871553Srgrimes	(void)fprintf(stderr,
188130094Sru"usage: mtree [-LPUcdeinqruxw] [-f spec] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"
18960418Swollman"\t[-X excludes]\n");
1901553Srgrimes	exit(1);
1911553Srgrimes}
192