xref: /dragonfly/usr.sbin/mtree/mtree.c (revision 99dd49c5)
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  * @(#) Copyright (c) 1989, 1990, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)mtree.c	8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.sbin/mtree/mtree.c,v 1.8.2.3 2003/05/07 17:55:17 tobez Exp $
36  * $DragonFly: src/usr.sbin/mtree/mtree.c,v 1.5 2004/03/15 16:24:22 dillon Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fts.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 #include "mtree.h"
47 #include "extern.h"
48 
49 int ftsoptions = FTS_PHYSICAL;
50 int cflag, dflag, eflag, iflag, nflag, qflag, rflag, sflag, uflag, Uflag;
51 u_int keys;
52 char fullpath[MAXPATHLEN];
53 
54 static void usage(void);
55 
56 int
57 main(int argc, char *argv[])
58 {
59 	int ch;
60 	char *dir, *p;
61 	int status;
62 
63 	dir = NULL;
64 	keys = KEYDEFAULT;
65 	init_excludes();
66 
67 	while ((ch = getopt(argc, argv, "cdef:iK:k:LnPp:qrs:UuxX:")) != -1)
68 		switch((char)ch) {
69 		case 'c':
70 			cflag = 1;
71 			break;
72 		case 'd':
73 			dflag = 1;
74 			break;
75 		case 'e':
76 			eflag = 1;
77 			break;
78 		case 'f':
79 			if (!(freopen(optarg, "r", stdin)))
80 				err(1, "%s", optarg);
81 			break;
82 		case 'i':
83 			iflag = 1;
84 			break;
85 		case 'K':
86 			while ((p = strsep(&optarg, " \t,")) != NULL)
87 				if (*p != '\0')
88 					keys |= parsekey(p, NULL);
89 			break;
90 		case 'k':
91 			keys = F_TYPE;
92 			while ((p = strsep(&optarg, " \t,")) != NULL)
93 				if (*p != '\0')
94 					keys |= parsekey(p, NULL);
95 			break;
96 		case 'L':
97 			ftsoptions &= ~FTS_PHYSICAL;
98 			ftsoptions |= FTS_LOGICAL;
99 			break;
100 		case 'n':
101 			nflag = 1;
102 			break;
103 		case 'P':
104 			ftsoptions &= ~FTS_LOGICAL;
105 			ftsoptions |= FTS_PHYSICAL;
106 			break;
107 		case 'p':
108 			dir = optarg;
109 			break;
110 		case 'q':
111 			qflag = 1;
112 			break;
113 		case 'r':
114 			rflag = 1;
115 			break;
116 		case 's':
117 			sflag = 1;
118 			crc_total = ~strtol(optarg, &p, 0);
119 			if (*p)
120 				errx(1, "illegal seed value -- %s", optarg);
121 			break;
122 		case 'U':
123 			Uflag = 1;
124 			uflag = 1;
125 			break;
126 		case 'u':
127 			uflag = 1;
128 			break;
129 		case 'x':
130 			ftsoptions |= FTS_XDEV;
131 			break;
132 		case 'X':
133 			read_excludes_file(optarg);
134 			break;
135 		case '?':
136 		default:
137 			usage();
138 		}
139 	argc -= optind;
140 	argv += optind;
141 
142 	if (argc)
143 		usage();
144 
145 	if (dir && chdir(dir))
146 		err(1, "%s", dir);
147 
148 	if ((cflag || sflag) && !getwd(fullpath))
149 		errx(1, "%s", fullpath);
150 
151 	if (cflag) {
152 		cwalk();
153 		exit(0);
154 	}
155 	status = verify();
156 	if (Uflag & (status == MISMATCHEXIT))
157 		status = 0;
158 	exit(status);
159 }
160 
161 static void
162 usage(void)
163 {
164 
165 	fprintf(stderr,
166 "usage: mtree [-LPUcdeinqrux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"
167 "\t[-X excludes]\n");
168 	exit(1);
169 }
170