xref: /dragonfly/usr.sbin/mtree/mtree.c (revision 2cd2d2b5)
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 extern long int crc_total;
50 
51 int ftsoptions = FTS_PHYSICAL;
52 int cflag, dflag, eflag, iflag, nflag, qflag, rflag, sflag, uflag, Uflag;
53 u_int keys;
54 char fullpath[MAXPATHLEN];
55 
56 static void usage(void);
57 
58 int
59 main(int argc, char *argv[])
60 {
61 	int ch;
62 	char *dir, *p;
63 	int status;
64 
65 	dir = NULL;
66 	keys = KEYDEFAULT;
67 	init_excludes();
68 
69 	while ((ch = getopt(argc, argv, "cdef:iK:k:LnPp:qrs:UuxX:")) != -1)
70 		switch((char)ch) {
71 		case 'c':
72 			cflag = 1;
73 			break;
74 		case 'd':
75 			dflag = 1;
76 			break;
77 		case 'e':
78 			eflag = 1;
79 			break;
80 		case 'f':
81 			if (!(freopen(optarg, "r", stdin)))
82 				err(1, "%s", optarg);
83 			break;
84 		case 'i':
85 			iflag = 1;
86 			break;
87 		case 'K':
88 			while ((p = strsep(&optarg, " \t,")) != NULL)
89 				if (*p != '\0')
90 					keys |= parsekey(p, NULL);
91 			break;
92 		case 'k':
93 			keys = F_TYPE;
94 			while ((p = strsep(&optarg, " \t,")) != NULL)
95 				if (*p != '\0')
96 					keys |= parsekey(p, NULL);
97 			break;
98 		case 'L':
99 			ftsoptions &= ~FTS_PHYSICAL;
100 			ftsoptions |= FTS_LOGICAL;
101 			break;
102 		case 'n':
103 			nflag = 1;
104 			break;
105 		case 'P':
106 			ftsoptions &= ~FTS_LOGICAL;
107 			ftsoptions |= FTS_PHYSICAL;
108 			break;
109 		case 'p':
110 			dir = optarg;
111 			break;
112 		case 'q':
113 			qflag = 1;
114 			break;
115 		case 'r':
116 			rflag = 1;
117 			break;
118 		case 's':
119 			sflag = 1;
120 			crc_total = ~strtol(optarg, &p, 0);
121 			if (*p)
122 				errx(1, "illegal seed value -- %s", optarg);
123 			break;
124 		case 'U':
125 			Uflag = 1;
126 			uflag = 1;
127 			break;
128 		case 'u':
129 			uflag = 1;
130 			break;
131 		case 'x':
132 			ftsoptions |= FTS_XDEV;
133 			break;
134 		case 'X':
135 			read_excludes_file(optarg);
136 			break;
137 		case '?':
138 		default:
139 			usage();
140 		}
141 	argc -= optind;
142 	argv += optind;
143 
144 	if (argc)
145 		usage();
146 
147 	if (dir && chdir(dir))
148 		err(1, "%s", dir);
149 
150 	if ((cflag || sflag) && !getwd(fullpath))
151 		errx(1, "%s", fullpath);
152 
153 	if (cflag) {
154 		cwalk();
155 		exit(0);
156 	}
157 	status = verify();
158 	if (Uflag & (status == MISMATCHEXIT))
159 		status = 0;
160 	exit(status);
161 }
162 
163 static void
164 usage(void)
165 {
166 
167 	fprintf(stderr,
168 "usage: mtree [-LPUcdeinqrux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"
169 "\t[-X excludes]\n");
170 	exit(1);
171 }
172