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