1147039Ssam/*-
2147039Ssam * Copyright (c) 1989, 1990, 1993
3265420Simp *	The Regents of the University of California.  All rights reserved.
4319190Sngie *
5172319Ssam * Redistribution and use in source and binary forms, with or without
6234786Sbschmidt * modification, are permitted provided that the following conditions
7346981Scy * are met:
8346981Scy * 1. Redistributions of source code must retain the above copyright
9346981Scy *    notice, this list of conditions and the following disclaimer.
10346981Scy * 2. Redistributions in binary form must reproduce the above copyright
11346981Scy *    notice, this list of conditions and the following disclaimer in the
12147039Ssam *    documentation and/or other materials provided with the distribution.
13147039Ssam * 3. Neither the name of the University nor the names of its contributors
14346981Scy *    may be used to endorse or promote products derived from this software
15346981Scy *    without specific prior written permission.
16346981Scy *
17346981Scy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18346981Scy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19346981Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20346981Scy * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21346981Scy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22346981Scy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23346981Scy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24346981Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25346981Scy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26281806Srpaulo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27346981Scy * SUCH DAMAGE.
28346981Scy */
29346981Scy
30346981Scy#if 0
31346981Scy#ifndef lint
32346981Scystatic const char copyright[] =
33346981Scy"@(#) Copyright (c) 1989, 1990, 1993\n\
34346981Scy	The Regents of the University of California.  All rights reserved.\n";
35346981Scy#endif /* not lint */
36346981Scy
37346981Scy#ifndef lint
38346981Scystatic char sccsid[] = "@(#)mtree.c	8.1 (Berkeley) 6/6/93";
39346981Scy#endif /* not lint */
40346981Scy#endif
41346981Scy#include <sys/cdefs.h>
42346981Scy__FBSDID("$FreeBSD$");
43346981Scy
44346981Scy#include <sys/param.h>
45346981Scy#include <sys/stat.h>
46346981Scy#include <err.h>
47363441Scy#include <errno.h>
48363441Scy#include <fts.h>
49346981Scy#include <stdio.h>
50363441Scy#include <unistd.h>
51346981Scy#include "mtree.h"
52337817Scy#include "extern.h"
53346981Scy
54346981Scyint ftsoptions = FTS_PHYSICAL;
55363441Scyint dflag, eflag, iflag, nflag, qflag, rflag, sflag, uflag, wflag;
56346981Scystatic int cflag, Uflag;
57346981Scyu_int keys;
58346981Scychar fullpath[MAXPATHLEN];
59346981Scy
60346981Scystatic void usage(void);
61346981Scy
62346981Scyint
63346981Scymain(int argc, char *argv[])
64346981Scy{
65346981Scy	int ch;
66346981Scy	char *dir, *p;
67346981Scy	int status;
68346981Scy	FILE *spec1, *spec2;
69346981Scy
70346981Scy	dir = NULL;
71346981Scy	keys = KEYDEFAULT;
72346981Scy	init_excludes();
73346981Scy	spec1 = stdin;
74346981Scy	spec2 = NULL;
75346981Scy
76346981Scy	while ((ch = getopt(argc, argv, "cdef:iK:k:LnPp:qrs:UuwxX:")) != -1)
77346981Scy		switch((char)ch) {
78346981Scy		case 'c':
79346981Scy			cflag = 1;
80346981Scy			break;
81346981Scy		case 'd':
82346981Scy			dflag = 1;
83346981Scy			break;
84346981Scy		case 'e':
85346981Scy			eflag = 1;
86346981Scy			break;
87346981Scy		case 'f':
88346981Scy			if (spec1 == stdin) {
89346981Scy				spec1 = fopen(optarg, "r");
90346981Scy				if (spec1 == NULL)
91346981Scy					err(1, "%s", optarg);
92346981Scy			} else if (spec2 == NULL) {
93346981Scy				spec2 = fopen(optarg, "r");
94346981Scy				if (spec2 == NULL)
95147039Ssam					err(1, "%s", optarg);
96147453Ssam			} else
97147453Ssam				usage();
98173530Ssam			break;
99173530Ssam		case 'i':
100189263Ssam			iflag = 1;
101173530Ssam			break;
102173530Ssam		case 'K':
103173530Ssam			while ((p = strsep(&optarg, " \t,")) != NULL)
104346981Scy				if (*p != '\0')
105346981Scy					keys |= parsekey(p, NULL);
106346981Scy			break;
107234711Sbschmidt		case 'k':
108287577Sjkim			keys = F_TYPE;
109363441Scy			while ((p = strsep(&optarg, " \t,")) != NULL)
110363441Scy				if (*p != '\0')
111363441Scy					keys |= parsekey(p, NULL);
112363441Scy			break;
113363441Scy		case 'L':
114287577Sjkim			ftsoptions &= ~FTS_PHYSICAL;
115287577Sjkim			ftsoptions |= FTS_LOGICAL;
116234711Sbschmidt			break;
117252726Srpaulo		case 'n':
118252726Srpaulo			nflag = 1;
119252726Srpaulo			break;
120287577Sjkim		case 'P':
121172319Ssam			ftsoptions &= ~FTS_LOGICAL;
122172319Ssam			ftsoptions |= FTS_PHYSICAL;
123172319Ssam			break;
124214735Srpaulo		case 'p':
125275054Sbapt			dir = optarg;
126172319Ssam			break;
127172319Ssam		case 'q':
128189263Ssam			qflag = 1;
129189263Ssam			break;
130189263Ssam		case 'r':
131189263Ssam			rflag = 1;
132172319Ssam			break;
133234711Sbschmidt		case 's':
134234711Sbschmidt			sflag = 1;
135234711Sbschmidt			crc_total = ~strtoul(optarg, &p, 0);
136234711Sbschmidt			if (*p)
137234711Sbschmidt				errx(1, "illegal seed value -- %s", optarg);
138234711Sbschmidt			break;
139234711Sbschmidt		case 'U':
140234711Sbschmidt			Uflag = 1;
141234711Sbschmidt			uflag = 1;
142287577Sjkim			break;
143287577Sjkim		case 'u':
144281806Srpaulo			uflag = 1;
145281806Srpaulo			break;
146346981Scy		case 'w':
147346981Scy			wflag = 1;
148346981Scy			break;
149346981Scy		case 'x':
150234711Sbschmidt			ftsoptions |= FTS_XDEV;
151234711Sbschmidt			break;
152346981Scy		case 'X':
153234711Sbschmidt			read_excludes_file(optarg);
154234711Sbschmidt			break;
155234711Sbschmidt		case '?':
156234711Sbschmidt		default:
157252726Srpaulo			usage();
158252726Srpaulo		}
159346981Scy	argc -= optind;
160346981Scy	argv += optind;
161346981Scy
162346981Scy	if (argc)
163234759Sbschmidt		usage();
164172319Ssam
165252726Srpaulo	if (dir && chdir(dir))
166252726Srpaulo		err(1, "%s", dir);
167252726Srpaulo
168252726Srpaulo	if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
169234758Sbschmidt		errx(1, "%s", fullpath);
170234711Sbschmidt
171234759Sbschmidt	if (cflag) {
172172319Ssam		cwalk();
173172319Ssam		exit(0);
174234758Sbschmidt	}
175234711Sbschmidt	if (spec2 != NULL)
176234759Sbschmidt		status = mtree_specspec(spec1, spec2);
177172319Ssam	else
178172319Ssam		status = mtree_verifyspec(spec1);
179172319Ssam	if (Uflag & (status == MISMATCHEXIT))
180234711Sbschmidt		status = 0;
181234711Sbschmidt	exit(status);
182234759Sbschmidt}
183172319Ssam
184172319Ssamstatic void
185234758Sbschmidtusage(void)
186172319Ssam{
187234711Sbschmidt	(void)fprintf(stderr,
188234711Sbschmidt"usage: mtree [-LPUcdeinqruxw] [-f spec] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"
189234759Sbschmidt"\t[-X excludes]\n");
190172319Ssam	exit(1);
191172319Ssam}
192234758Sbschmidt