mtree.c revision 121299
1/*-
2 * Copyright (c) 1989, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if 0
35#ifndef lint
36static const char copyright[] =
37"@(#) Copyright (c) 1989, 1990, 1993\n\
38	The Regents of the University of California.  All rights reserved.\n";
39#endif /* not lint */
40
41#ifndef lint
42static char sccsid[] = "@(#)mtree.c	8.1 (Berkeley) 6/6/93";
43#endif /* not lint */
44#endif
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: head/usr.sbin/mtree/mtree.c 121299 2003-10-21 07:58:52Z phk $");
47
48#include <sys/param.h>
49#include <sys/stat.h>
50#include <err.h>
51#include <errno.h>
52#include <fts.h>
53#include <stdio.h>
54#include <unistd.h>
55#include "mtree.h"
56#include "extern.h"
57
58int ftsoptions = FTS_PHYSICAL;
59int cflag, dflag, eflag, iflag, nflag, qflag, rflag, sflag, uflag, Uflag;
60u_int keys;
61char fullpath[MAXPATHLEN];
62
63static void usage(void);
64
65int
66main(int argc, char *argv[])
67{
68	int ch;
69	char *dir, *p;
70	int status;
71
72	dir = NULL;
73	keys = KEYDEFAULT;
74	init_excludes();
75
76	while ((ch = getopt(argc, argv, "cdef:iK:k:LnPp:qrs:UuxX:")) != -1)
77		switch((char)ch) {
78		case 'c':
79			cflag = 1;
80			break;
81		case 'd':
82			dflag = 1;
83			break;
84		case 'e':
85			eflag = 1;
86			break;
87		case 'f':
88			if (!(freopen(optarg, "r", stdin)))
89				err(1, "%s", optarg);
90			break;
91		case 'i':
92			iflag = 1;
93			break;
94		case 'K':
95			while ((p = strsep(&optarg, " \t,")) != NULL)
96				if (*p != '\0')
97					keys |= parsekey(p, NULL);
98			break;
99		case 'k':
100			keys = F_TYPE;
101			while ((p = strsep(&optarg, " \t,")) != NULL)
102				if (*p != '\0')
103					keys |= parsekey(p, NULL);
104			break;
105		case 'L':
106			ftsoptions &= ~FTS_PHYSICAL;
107			ftsoptions |= FTS_LOGICAL;
108			break;
109		case 'n':
110			nflag = 1;
111			break;
112		case 'P':
113			ftsoptions &= ~FTS_LOGICAL;
114			ftsoptions |= FTS_PHYSICAL;
115			break;
116		case 'p':
117			dir = optarg;
118			break;
119		case 'q':
120			qflag = 1;
121			break;
122		case 'r':
123			rflag = 1;
124			break;
125		case 's':
126			sflag = 1;
127			crc_total = ~strtoul(optarg, &p, 0);
128			if (*p)
129				errx(1, "illegal seed value -- %s", optarg);
130			break;
131		case 'U':
132			Uflag = 1;
133			uflag = 1;
134			break;
135		case 'u':
136			uflag = 1;
137			break;
138		case 'x':
139			ftsoptions |= FTS_XDEV;
140			break;
141		case 'X':
142			read_excludes_file(optarg);
143			break;
144		case '?':
145		default:
146			usage();
147		}
148	argc -= optind;
149	argv += optind;
150
151	if (argc)
152		usage();
153
154	if (dir && chdir(dir))
155		err(1, "%s", dir);
156
157	if ((cflag || sflag) && !getwd(fullpath))
158		errx(1, "%s", fullpath);
159
160	if (cflag) {
161		cwalk();
162		exit(0);
163	}
164	status = verify();
165	if (Uflag & (status == MISMATCHEXIT))
166		status = 0;
167	exit(status);
168}
169
170static void
171usage(void)
172{
173	(void)fprintf(stderr,
174"usage: mtree [-LPUcdeinqrux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"
175"\t[-X excludes]\n");
176	exit(1);
177}
178