mtree(8): Replace our mtree(8) with NetBSD's version.
[dragonfly.git] / usr.sbin / mtree / mtree.c
blobced0a5749227780bb79b8511ed14574062cea396
1 /* @(#)mtree.c 8.1 (Berkeley) 6/6/93 */
2 /* $NetBSD: mtree.c,v 1.50 2015/01/23 02:27:01 christos Exp $ */
4 /*-
5 * Copyright (c) 1989, 1990, 1993
6 * The Regents of the University of California. All rights reserved.
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.
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.
33 #if HAVE_NBTOOL_CONFIG_H
34 #include "nbtool_config.h"
35 #endif
37 #include <sys/param.h>
38 #include <sys/stat.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
46 #include "extern.h"
48 int ftsoptions = FTS_PHYSICAL;
49 int bflag, dflag, eflag, iflag, jflag, lflag, mflag, nflag, qflag, rflag,
50 sflag, tflag, uflag;
51 char fullpath[MAXPATHLEN];
53 static struct {
54 enum flavor flavor;
55 const char name[9];
56 } flavors[] = {
57 {F_MTREE, "mtree"},
58 {F_FREEBSD9, "freebsd9"},
59 {F_NETBSD6, "netbsd6"},
62 __dead2 static void usage(void);
64 int
65 main(int argc, char **argv)
67 int ch, status;
68 unsigned int i;
69 int cflag, Cflag, Dflag, Uflag, wflag;
70 char *dir, *p;
71 FILE *spec1, *spec2;
73 setprogname(argv[0]);
75 cflag = Cflag = Dflag = Uflag = wflag = 0;
76 dir = NULL;
77 init_excludes();
78 spec1 = stdin;
79 spec2 = NULL;
81 while ((ch = getopt(argc, argv,
82 "bcCdDeE:f:F:I:ijk:K:lLmMnN:O:p:PqrR:s:StuUwWxX:"))
83 != -1) {
84 switch((char)ch) {
85 case 'b':
86 bflag = 1;
87 break;
88 case 'c':
89 cflag = 1;
90 break;
91 case 'C':
92 Cflag = 1;
93 break;
94 case 'd':
95 dflag = 1;
96 break;
97 case 'D':
98 Dflag = 1;
99 break;
100 case 'E':
101 parsetags(&excludetags, optarg);
102 break;
103 case 'e':
104 eflag = 1;
105 break;
106 case 'f':
107 if (spec1 == stdin) {
108 spec1 = fopen(optarg, "r");
109 if (spec1 == NULL)
110 mtree_err("%s: %s", optarg,
111 strerror(errno));
112 } else if (spec2 == NULL) {
113 spec2 = fopen(optarg, "r");
114 if (spec2 == NULL)
115 mtree_err("%s: %s", optarg,
116 strerror(errno));
117 } else
118 usage();
119 break;
120 case 'F':
121 for (i = 0; i < NELEM(flavors); i++)
122 if (strcmp(optarg, flavors[i].name) == 0) {
123 flavor = flavors[i].flavor;
124 break;
126 if (i == NELEM(flavors))
127 usage();
128 break;
129 case 'i':
130 iflag = 1;
131 break;
132 case 'I':
133 parsetags(&includetags, optarg);
134 break;
135 case 'j':
136 jflag = 1;
137 break;
138 case 'k':
139 keys = F_TYPE;
140 while ((p = strsep(&optarg, " \t,")) != NULL)
141 if (*p != '\0')
142 keys |= parsekey(p, NULL);
143 break;
144 case 'K':
145 while ((p = strsep(&optarg, " \t,")) != NULL)
146 if (*p != '\0')
147 keys |= parsekey(p, NULL);
148 break;
149 case 'l':
150 lflag = 1;
151 break;
152 case 'L':
153 ftsoptions &= ~FTS_PHYSICAL;
154 ftsoptions |= FTS_LOGICAL;
155 break;
156 case 'm':
157 mflag = 1;
158 break;
159 case 'M':
160 mtree_Mflag = 1;
161 break;
162 case 'n':
163 nflag = 1;
164 break;
165 case 'N':
166 if (! setup_getid(optarg))
167 mtree_err(
168 "Unable to use user and group databases in `%s'",
169 optarg);
170 break;
171 case 'O':
172 load_only(optarg);
173 break;
174 case 'p':
175 dir = optarg;
176 break;
177 case 'P':
178 ftsoptions &= ~FTS_LOGICAL;
179 ftsoptions |= FTS_PHYSICAL;
180 break;
181 case 'q':
182 qflag = 1;
183 break;
184 case 'r':
185 rflag++;
186 break;
187 case 'R':
188 while ((p = strsep(&optarg, " \t,")) != NULL)
189 if (*p != '\0')
190 keys &= ~parsekey(p, NULL);
191 break;
192 case 's':
193 sflag = 1;
194 crc_total = ~strtol(optarg, &p, 0);
195 if (*p)
196 mtree_err("illegal seed value -- %s", optarg);
197 break;
198 case 'S':
199 mtree_Sflag = 1;
200 break;
201 case 't':
202 tflag = 1;
203 break;
204 case 'u':
205 uflag = 1;
206 break;
207 case 'U':
208 Uflag = uflag = 1;
209 break;
210 case 'w':
211 wflag = 1;
212 break;
213 case 'W':
214 mtree_Wflag = 1;
215 break;
216 case 'x':
217 ftsoptions |= FTS_XDEV;
218 break;
219 case 'X':
220 read_excludes_file(optarg);
221 break;
222 case '?':
223 default:
224 usage();
227 argc -= optind;
228 argv += optind;
230 if (argc)
231 usage();
233 switch (flavor) {
234 case F_FREEBSD9:
235 if (cflag && iflag) {
236 warnx("-c and -i passed, replacing -i with -j for "
237 "FreeBSD compatibility");
238 iflag = 0;
239 jflag = 1;
241 if (dflag && !bflag) {
242 warnx("Adding -b to -d for FreeBSD compatibility");
243 bflag = 1;
245 if (uflag && !iflag) {
246 warnx("Adding -i to -%c for FreeBSD compatibility",
247 Uflag ? 'U' : 'u');
248 iflag = 1;
250 if (uflag && !tflag) {
251 warnx("Adding -t to -%c for FreeBSD compatibility",
252 Uflag ? 'U' : 'u');
253 tflag = 1;
255 if (wflag)
256 warnx("The -w flag is a no-op");
257 break;
258 default:
259 if (wflag)
260 usage();
263 if (spec2 && (cflag || Cflag || Dflag))
264 mtree_err("Double -f, -c, -C and -D flags are mutually "
265 "exclusive");
267 if (dir && spec2)
268 mtree_err("Double -f and -p flags are mutually exclusive");
270 if (dir && chdir(dir))
271 mtree_err("%s: %s", dir, strerror(errno));
273 if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
274 mtree_err("%s", strerror(errno));
276 if ((cflag && Cflag) || (cflag && Dflag) || (Cflag && Dflag))
277 mtree_err("-c, -C and -D flags are mutually exclusive");
279 if (iflag && mflag)
280 mtree_err("-i and -m flags are mutually exclusive");
282 if (lflag && uflag)
283 mtree_err("-l and -u flags are mutually exclusive");
285 if (cflag) {
286 cwalk(stdout);
287 exit(0);
289 if (Cflag || Dflag) {
290 dump_nodes(stdout, "", spec(spec1), Dflag);
291 exit(0);
293 if (spec2 != NULL)
294 status = mtree_specspec(spec1, spec2);
295 else
296 status = verify(spec1);
297 if (Uflag && (status == MISMATCHEXIT))
298 status = 0;
299 exit(status);
302 static void
303 usage(void)
305 unsigned int i;
307 fprintf(stderr,
308 "usage: %s [-bCcDdejLlMnPqrStUuWx] [-i|-m] [-E tags]\n"
309 "\t\t[-f spec] [-f spec]\n"
310 "\t\t[-I tags] [-K keywords] [-k keywords] [-N dbdir] [-p path]\n"
311 "\t\t[-R keywords] [-s seed] [-X exclude-file]\n"
312 "\t\t[-F flavor]\n",
313 getprogname());
314 fprintf(stderr, "\nflavors:");
315 for (i = 0; i < NELEM(flavors); i++)
316 fprintf(stderr, " %s", flavors[i].name);
317 fprintf(stderr, "\n");
318 exit(1);