mtree.c revision 124389
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: head/usr.sbin/mtree/mtree.c 124389 2004-01-11 19:38:48Z phk $");
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;
55124389Sphkint cflag, dflag, eflag, iflag, nflag, qflag, rflag, sflag, uflag, Uflag, wflag;
5636670Speteru_int keys;
571553Srgrimeschar fullpath[MAXPATHLEN];
581553Srgrimes
5999800Salfredstatic void usage(void);
601553Srgrimes
611553Srgrimesint
6299802Salfredmain(int argc, char *argv[])
631553Srgrimes{
641553Srgrimes	int ch;
651553Srgrimes	char *dir, *p;
663468Srgrimes	int status;
67122141Sphk	FILE *spec1, *spec2;
681553Srgrimes
691553Srgrimes	dir = NULL;
701553Srgrimes	keys = KEYDEFAULT;
7160418Swollman	init_excludes();
72122141Sphk	spec1 = stdin;
73122141Sphk	spec2 = NULL;
7460418Swollman
75124389Sphk	while ((ch = getopt(argc, argv, "cdef:iK:k:LnPp:qrs:UuwxX:")) != -1)
761553Srgrimes		switch((char)ch) {
771553Srgrimes		case 'c':
781553Srgrimes			cflag = 1;
791553Srgrimes			break;
801553Srgrimes		case 'd':
811553Srgrimes			dflag = 1;
821553Srgrimes			break;
831553Srgrimes		case 'e':
841553Srgrimes			eflag = 1;
851553Srgrimes			break;
861553Srgrimes		case 'f':
87122141Sphk			if (spec1 == stdin) {
88122141Sphk				spec1 = fopen(optarg, "r");
89122141Sphk				if (spec1 == NULL)
90122141Sphk					err(1, "%s", optarg);
91122141Sphk			} else if (spec2 == NULL) {
92122141Sphk				spec2 = fopen(optarg, "r");
93122141Sphk				if (spec2 == NULL)
94122141Sphk					err(1, "%s", optarg);
95122141Sphk			} else
96122141Sphk				usage();
971553Srgrimes			break;
982860Srgrimes		case 'i':
992860Srgrimes			iflag = 1;
1002860Srgrimes			break;
1011553Srgrimes		case 'K':
1021553Srgrimes			while ((p = strsep(&optarg, " \t,")) != NULL)
1031553Srgrimes				if (*p != '\0')
1041553Srgrimes					keys |= parsekey(p, NULL);
1051553Srgrimes			break;
1061553Srgrimes		case 'k':
1071553Srgrimes			keys = F_TYPE;
1081553Srgrimes			while ((p = strsep(&optarg, " \t,")) != NULL)
1091553Srgrimes				if (*p != '\0')
1101553Srgrimes					keys |= parsekey(p, NULL);
1111553Srgrimes			break;
11265882Sache		case 'L':
11365882Sache			ftsoptions &= ~FTS_PHYSICAL;
11465882Sache			ftsoptions |= FTS_LOGICAL;
11565882Sache			break;
1162860Srgrimes		case 'n':
1172860Srgrimes			nflag = 1;
1182860Srgrimes			break;
11966357Sache		case 'P':
12066357Sache			ftsoptions &= ~FTS_LOGICAL;
12166357Sache			ftsoptions |= FTS_PHYSICAL;
12266357Sache			break;
1231553Srgrimes		case 'p':
1241553Srgrimes			dir = optarg;
1251553Srgrimes			break;
12663853Simp		case 'q':
12763853Simp			qflag = 1;
12863853Simp			break;
1291553Srgrimes		case 'r':
1301553Srgrimes			rflag = 1;
1311553Srgrimes			break;
1321553Srgrimes		case 's':
1331553Srgrimes			sflag = 1;
134112214Srobert			crc_total = ~strtoul(optarg, &p, 0);
1351553Srgrimes			if (*p)
13630027Scharnier				errx(1, "illegal seed value -- %s", optarg);
137112403Stobez			break;
1383468Srgrimes		case 'U':
1393468Srgrimes			Uflag = 1;
1403468Srgrimes			uflag = 1;
1413468Srgrimes			break;
1421553Srgrimes		case 'u':
1431553Srgrimes			uflag = 1;
1441553Srgrimes			break;
145124389Sphk		case 'w':
146124389Sphk			wflag = 1;
147124389Sphk			break;
1481553Srgrimes		case 'x':
1491553Srgrimes			ftsoptions |= FTS_XDEV;
1501553Srgrimes			break;
15160418Swollman		case 'X':
15260418Swollman			read_excludes_file(optarg);
15360418Swollman			break;
1541553Srgrimes		case '?':
1551553Srgrimes		default:
1561553Srgrimes			usage();
1571553Srgrimes		}
1581553Srgrimes	argc -= optind;
1591553Srgrimes	argv += optind;
1601553Srgrimes
1611553Srgrimes	if (argc)
1621553Srgrimes		usage();
1631553Srgrimes
1641553Srgrimes	if (dir && chdir(dir))
16530027Scharnier		err(1, "%s", dir);
1661553Srgrimes
1671553Srgrimes	if ((cflag || sflag) && !getwd(fullpath))
16830027Scharnier		errx(1, "%s", fullpath);
1691553Srgrimes
1701553Srgrimes	if (cflag) {
1711553Srgrimes		cwalk();
1721553Srgrimes		exit(0);
1731553Srgrimes	}
174122141Sphk	if (spec2 != NULL)
175122141Sphk		status = mtree_specspec(spec1, spec2);
176122141Sphk	else
177122141Sphk		status = mtree_verifyspec(spec1);
1783468Srgrimes	if (Uflag & (status == MISMATCHEXIT))
1793468Srgrimes		status = 0;
1803468Srgrimes	exit(status);
1811553Srgrimes}
1821553Srgrimes
1831553Srgrimesstatic void
184121299Sphkusage(void)
1851553Srgrimes{
1861553Srgrimes	(void)fprintf(stderr,
187122141Sphk"usage: mtree [-LPUcdeinqrux] [-f spec] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"
18860418Swollman"\t[-X excludes]\n");
1891553Srgrimes	exit(1);
1901553Srgrimes}
191