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