mtree.c revision 114601
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.
131553Srgrimes * 3. All advertising materials mentioning features or use of this software
141553Srgrimes *    must display the following acknowledgement:
151553Srgrimes *	This product includes software developed by the University of
161553Srgrimes *	California, Berkeley and its contributors.
171553Srgrimes * 4. Neither the name of the University nor the names of its contributors
181553Srgrimes *    may be used to endorse or promote products derived from this software
191553Srgrimes *    without specific prior written permission.
201553Srgrimes *
211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311553Srgrimes * SUCH DAMAGE.
321553Srgrimes */
331553Srgrimes
34114601Sobrien#if 0
351553Srgrimes#ifndef lint
3630027Scharnierstatic const char copyright[] =
371553Srgrimes"@(#) Copyright (c) 1989, 1990, 1993\n\
381553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
391553Srgrimes#endif /* not lint */
401553Srgrimes
411553Srgrimes#ifndef lint
421553Srgrimesstatic char sccsid[] = "@(#)mtree.c	8.1 (Berkeley) 6/6/93";
43114601Sobrien#endif /* not lint */
4430027Scharnier#endif
45114601Sobrien#include <sys/cdefs.h>
46114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/mtree/mtree.c 114601 2003-05-03 21:06:42Z obrien $");
471553Srgrimes
481553Srgrimes#include <sys/param.h>
491553Srgrimes#include <sys/stat.h>
5030027Scharnier#include <err.h>
511553Srgrimes#include <errno.h>
5230027Scharnier#include <fts.h>
5330027Scharnier#include <stdio.h>
541553Srgrimes#include <unistd.h>
551553Srgrimes#include "mtree.h"
561553Srgrimes#include "extern.h"
571553Srgrimes
5865882Sacheint ftsoptions = FTS_PHYSICAL;
5963853Simpint cflag, dflag, eflag, iflag, nflag, qflag, rflag, sflag, uflag, Uflag;
6036670Speteru_int keys;
611553Srgrimeschar fullpath[MAXPATHLEN];
621553Srgrimes
6399800Salfredstatic void usage(void);
641553Srgrimes
651553Srgrimesint
6699802Salfredmain(int argc, char *argv[])
671553Srgrimes{
681553Srgrimes	int ch;
691553Srgrimes	char *dir, *p;
703468Srgrimes	int status;
711553Srgrimes
721553Srgrimes	dir = NULL;
731553Srgrimes	keys = KEYDEFAULT;
7460418Swollman	init_excludes();
7560418Swollman
7666357Sache	while ((ch = getopt(argc, argv, "cdef:iK:k:LnPp:qrs:UuxX:")) != -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':
881553Srgrimes			if (!(freopen(optarg, "r", stdin)))
8930027Scharnier				err(1, "%s", optarg);
901553Srgrimes			break;
912860Srgrimes		case 'i':
922860Srgrimes			iflag = 1;
932860Srgrimes			break;
941553Srgrimes		case 'K':
951553Srgrimes			while ((p = strsep(&optarg, " \t,")) != NULL)
961553Srgrimes				if (*p != '\0')
971553Srgrimes					keys |= parsekey(p, NULL);
981553Srgrimes			break;
991553Srgrimes		case 'k':
1001553Srgrimes			keys = F_TYPE;
1011553Srgrimes			while ((p = strsep(&optarg, " \t,")) != NULL)
1021553Srgrimes				if (*p != '\0')
1031553Srgrimes					keys |= parsekey(p, NULL);
1041553Srgrimes			break;
10565882Sache		case 'L':
10665882Sache			ftsoptions &= ~FTS_PHYSICAL;
10765882Sache			ftsoptions |= FTS_LOGICAL;
10865882Sache			break;
1092860Srgrimes		case 'n':
1102860Srgrimes			nflag = 1;
1112860Srgrimes			break;
11266357Sache		case 'P':
11366357Sache			ftsoptions &= ~FTS_LOGICAL;
11466357Sache			ftsoptions |= FTS_PHYSICAL;
11566357Sache			break;
1161553Srgrimes		case 'p':
1171553Srgrimes			dir = optarg;
1181553Srgrimes			break;
11963853Simp		case 'q':
12063853Simp			qflag = 1;
12163853Simp			break;
1221553Srgrimes		case 'r':
1231553Srgrimes			rflag = 1;
1241553Srgrimes			break;
1251553Srgrimes		case 's':
1261553Srgrimes			sflag = 1;
127112214Srobert			crc_total = ~strtoul(optarg, &p, 0);
1281553Srgrimes			if (*p)
12930027Scharnier				errx(1, "illegal seed value -- %s", optarg);
130112403Stobez			break;
1313468Srgrimes		case 'U':
1323468Srgrimes			Uflag = 1;
1333468Srgrimes			uflag = 1;
1343468Srgrimes			break;
1351553Srgrimes		case 'u':
1361553Srgrimes			uflag = 1;
1371553Srgrimes			break;
1381553Srgrimes		case 'x':
1391553Srgrimes			ftsoptions |= FTS_XDEV;
1401553Srgrimes			break;
14160418Swollman		case 'X':
14260418Swollman			read_excludes_file(optarg);
14360418Swollman			break;
1441553Srgrimes		case '?':
1451553Srgrimes		default:
1461553Srgrimes			usage();
1471553Srgrimes		}
1481553Srgrimes	argc -= optind;
1491553Srgrimes	argv += optind;
1501553Srgrimes
1511553Srgrimes	if (argc)
1521553Srgrimes		usage();
1531553Srgrimes
1541553Srgrimes	if (dir && chdir(dir))
15530027Scharnier		err(1, "%s", dir);
1561553Srgrimes
1571553Srgrimes	if ((cflag || sflag) && !getwd(fullpath))
15830027Scharnier		errx(1, "%s", fullpath);
1591553Srgrimes
1601553Srgrimes	if (cflag) {
1611553Srgrimes		cwalk();
1621553Srgrimes		exit(0);
1631553Srgrimes	}
1643468Srgrimes	status = verify();
1653468Srgrimes	if (Uflag & (status == MISMATCHEXIT))
1663468Srgrimes		status = 0;
1673468Srgrimes	exit(status);
1681553Srgrimes}
1691553Srgrimes
1701553Srgrimesstatic void
1711553Srgrimesusage()
1721553Srgrimes{
1731553Srgrimes	(void)fprintf(stderr,
17466357Sache"usage: mtree [-LPUcdeinqrux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"
17560418Swollman"\t[-X excludes]\n");
1761553Srgrimes	exit(1);
1771553Srgrimes}
178