mtree.c revision 63853
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
341553Srgrimes#ifndef lint
3530027Scharnierstatic const char copyright[] =
361553Srgrimes"@(#) Copyright (c) 1989, 1990, 1993\n\
371553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381553Srgrimes#endif /* not lint */
391553Srgrimes
401553Srgrimes#ifndef lint
4130027Scharnier#if 0
421553Srgrimesstatic char sccsid[] = "@(#)mtree.c	8.1 (Berkeley) 6/6/93";
4330027Scharnier#endif
4430027Scharnierstatic const char rcsid[] =
4550479Speter  "$FreeBSD: head/usr.sbin/mtree/mtree.c 63853 2000-07-25 19:05:09Z imp $";
461553Srgrimes#endif /* not lint */
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
582860Srgrimesextern long int crc_total;
591553Srgrimes
6063786Smarcelint ftsoptions = FTS_LOGICAL;
6163853Simpint cflag, dflag, eflag, iflag, nflag, qflag, rflag, sflag, uflag, Uflag;
6236670Speteru_int keys;
631553Srgrimeschar fullpath[MAXPATHLEN];
641553Srgrimes
651553Srgrimesstatic void usage __P((void));
661553Srgrimes
671553Srgrimesint
681553Srgrimesmain(argc, argv)
691553Srgrimes	int argc;
701553Srgrimes	char *argv[];
711553Srgrimes{
721553Srgrimes	int ch;
731553Srgrimes	char *dir, *p;
743468Srgrimes	int status;
751553Srgrimes
761553Srgrimes	dir = NULL;
771553Srgrimes	keys = KEYDEFAULT;
7860418Swollman	init_excludes();
7960418Swollman
8063786Smarcel	while ((ch = getopt(argc, argv, "cdef:iK:k:np:Prs:UuxX:")) != -1)
811553Srgrimes		switch((char)ch) {
821553Srgrimes		case 'c':
831553Srgrimes			cflag = 1;
841553Srgrimes			break;
851553Srgrimes		case 'd':
861553Srgrimes			dflag = 1;
871553Srgrimes			break;
881553Srgrimes		case 'e':
891553Srgrimes			eflag = 1;
901553Srgrimes			break;
911553Srgrimes		case 'f':
921553Srgrimes			if (!(freopen(optarg, "r", stdin)))
9330027Scharnier				err(1, "%s", optarg);
941553Srgrimes			break;
952860Srgrimes		case 'i':
962860Srgrimes			iflag = 1;
972860Srgrimes			break;
981553Srgrimes		case 'K':
991553Srgrimes			while ((p = strsep(&optarg, " \t,")) != NULL)
1001553Srgrimes				if (*p != '\0')
1011553Srgrimes					keys |= parsekey(p, NULL);
1021553Srgrimes			break;
1031553Srgrimes		case 'k':
1041553Srgrimes			keys = F_TYPE;
1051553Srgrimes			while ((p = strsep(&optarg, " \t,")) != NULL)
1061553Srgrimes				if (*p != '\0')
1071553Srgrimes					keys |= parsekey(p, NULL);
1081553Srgrimes			break;
1092860Srgrimes		case 'n':
1102860Srgrimes			nflag = 1;
1112860Srgrimes			break;
1121553Srgrimes		case 'p':
1131553Srgrimes			dir = optarg;
1141553Srgrimes			break;
11563853Simp		case 'q':
11663853Simp			qflag = 1;
11763853Simp			break;
11863786Smarcel		case 'P':
11963786Smarcel			ftsoptions ^= FTS_LOGICAL;
12063786Smarcel			ftsoptions |= FTS_PHYSICAL;
12163786Smarcel			break;
1221553Srgrimes		case 'r':
1231553Srgrimes			rflag = 1;
1241553Srgrimes			break;
1251553Srgrimes		case 's':
1261553Srgrimes			sflag = 1;
1271553Srgrimes			crc_total = ~strtol(optarg, &p, 0);
1281553Srgrimes			if (*p)
12930027Scharnier				errx(1, "illegal seed value -- %s", optarg);
1303468Srgrimes		case 'U':
1313468Srgrimes			Uflag = 1;
1323468Srgrimes			uflag = 1;
1333468Srgrimes			break;
1341553Srgrimes		case 'u':
1351553Srgrimes			uflag = 1;
1361553Srgrimes			break;
1371553Srgrimes		case 'x':
1381553Srgrimes			ftsoptions |= FTS_XDEV;
1391553Srgrimes			break;
14060418Swollman		case 'X':
14160418Swollman			read_excludes_file(optarg);
14260418Swollman			break;
1431553Srgrimes		case '?':
1441553Srgrimes		default:
1451553Srgrimes			usage();
1461553Srgrimes		}
1471553Srgrimes	argc -= optind;
1481553Srgrimes	argv += optind;
1491553Srgrimes
1501553Srgrimes	if (argc)
1511553Srgrimes		usage();
1521553Srgrimes
1531553Srgrimes	if (dir && chdir(dir))
15430027Scharnier		err(1, "%s", dir);
1551553Srgrimes
1561553Srgrimes	if ((cflag || sflag) && !getwd(fullpath))
15730027Scharnier		errx(1, "%s", fullpath);
1581553Srgrimes
1591553Srgrimes	if (cflag) {
1601553Srgrimes		cwalk();
1611553Srgrimes		exit(0);
1621553Srgrimes	}
1633468Srgrimes	status = verify();
1643468Srgrimes	if (Uflag & (status == MISMATCHEXIT))
1653468Srgrimes		status = 0;
1663468Srgrimes	exit(status);
1671553Srgrimes}
1681553Srgrimes
1691553Srgrimesstatic void
1701553Srgrimesusage()
1711553Srgrimes{
1721553Srgrimes	(void)fprintf(stderr,
17363853Simp"usage: mtree [-PUcdeinqrux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"
17460418Swollman"\t[-X excludes]\n");
1751553Srgrimes	exit(1);
1761553Srgrimes}
177