xref: /openbsd/usr.sbin/mtree/mtree.c (revision 8529ddd3)
1 /*	$OpenBSD: mtree.c,v 1.22 2015/01/16 06:40:18 deraadt 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 #include <sys/stat.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <stdio.h>
37 #include <limits.h>
38 #include <fts.h>
39 #include "mtree.h"
40 #include "extern.h"
41 
42 extern u_int32_t crc_total;
43 
44 int ftsoptions = FTS_PHYSICAL;
45 int cflag, dflag, eflag, iflag, lflag, nflag, qflag, rflag, sflag, tflag,
46     uflag, Uflag;
47 u_int keys;
48 char fullpath[PATH_MAX];
49 
50 static void usage(void);
51 
52 int
53 main(int argc, char *argv[])
54 {
55 	extern int optind;
56 	extern char *optarg;
57 	int ch;
58 	char *dir, *p;
59 	int status;
60 
61 	dir = NULL;
62 	keys = KEYDEFAULT;
63 	while ((ch = getopt(argc, argv, "cdef:iK:k:lnp:qrs:tUux")) != -1)
64 		switch(ch) {
65 		case 'c':
66 			cflag = 1;
67 			break;
68 		case 'd':
69 			dflag = 1;
70 			break;
71 		case 'e':
72 			eflag = 1;
73 			break;
74 		case 'f':
75 			if (!(freopen(optarg, "r", stdin)))
76 				error("%s: %s", optarg, strerror(errno));
77 			break;
78 		case 'i':
79 			iflag = 1;
80 			break;
81 		case 'K':
82 			while ((p = strsep(&optarg, " \t,")) != NULL)
83 				if (*p != '\0')
84 					keys |= parsekey(p, NULL);
85 			break;
86 		case 'k':
87 			keys = F_TYPE;
88 			while ((p = strsep(&optarg, " \t,")) != NULL)
89 				if (*p != '\0')
90 					keys |= parsekey(p, NULL);
91 			break;
92 		case 'l':
93 			lflag = 1;
94 			break;
95 		case 'n':
96 			nflag = 1;
97 			break;
98 		case 'p':
99 			dir = optarg;
100 			break;
101 		case 'q':
102 			qflag = 1;
103 			break;
104 		case 'r':
105 			rflag = 1;
106 			break;
107 		case 's':
108 			sflag = 1;
109 			crc_total = ~strtol(optarg, &p, 0);
110 			if (*p)
111 				error("illegal seed value -- %s", optarg);
112 			break;
113 		case 't':
114 			tflag = 1;
115 			break;
116 		case 'U':
117 			Uflag = 1;
118 			uflag = 1;
119 			break;
120 		case 'u':
121 			uflag = 1;
122 			break;
123 		case 'x':
124 			ftsoptions |= FTS_XDEV;
125 			break;
126 		case '?':
127 		default:
128 			usage();
129 		}
130 	argc -= optind;
131 	argv += optind;
132 
133 	if (argc)
134 		usage();
135 
136 	if (dir && chdir(dir))
137 		error("%s: %s", dir, strerror(errno));
138 
139 	if ((cflag || sflag) && !getcwd(fullpath, sizeof fullpath))
140 		error("getcwd: %s", strerror(errno));
141 
142 	if (lflag == 1 && uflag == 1)
143 		error("-l and -u flags are mutually exclusive");
144 
145 	if (cflag) {
146 		cwalk();
147 		exit(0);
148 	}
149 	status = verify();
150 	if (Uflag && (status == MISMATCHEXIT))
151 		status = 0;
152 	exit(status);
153 }
154 
155 static void
156 usage(void)
157 {
158 	(void)fprintf(stderr,
159 	    "usage: mtree [-cdeilnqrtUux] [-f spec] [-K keywords] "
160 	    "[-k keywords] [-p path]\n"
161 	    "             [-s seed]\n");
162 	exit(1);
163 }
164