1*0a6a1f1dSLionel Sambuc /* $NetBSD: mtree.c,v 1.50 2015/01/23 02:27:01 christos Exp $ */
2d433a562SThomas Veerman
3d433a562SThomas Veerman /*-
4d433a562SThomas Veerman * Copyright (c) 1989, 1990, 1993
5d433a562SThomas Veerman * The Regents of the University of California. All rights reserved.
6d433a562SThomas Veerman *
7d433a562SThomas Veerman * Redistribution and use in source and binary forms, with or without
8d433a562SThomas Veerman * modification, are permitted provided that the following conditions
9d433a562SThomas Veerman * are met:
10d433a562SThomas Veerman * 1. Redistributions of source code must retain the above copyright
11d433a562SThomas Veerman * notice, this list of conditions and the following disclaimer.
12d433a562SThomas Veerman * 2. Redistributions in binary form must reproduce the above copyright
13d433a562SThomas Veerman * notice, this list of conditions and the following disclaimer in the
14d433a562SThomas Veerman * documentation and/or other materials provided with the distribution.
15d433a562SThomas Veerman * 3. Neither the name of the University nor the names of its contributors
16d433a562SThomas Veerman * may be used to endorse or promote products derived from this software
17d433a562SThomas Veerman * without specific prior written permission.
18d433a562SThomas Veerman *
19d433a562SThomas Veerman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20d433a562SThomas Veerman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21d433a562SThomas Veerman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22d433a562SThomas Veerman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23d433a562SThomas Veerman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24d433a562SThomas Veerman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25d433a562SThomas Veerman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26d433a562SThomas Veerman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27d433a562SThomas Veerman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28d433a562SThomas Veerman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29d433a562SThomas Veerman * SUCH DAMAGE.
30d433a562SThomas Veerman */
31d433a562SThomas Veerman
32d433a562SThomas Veerman #if HAVE_NBTOOL_CONFIG_H
33d433a562SThomas Veerman #include "nbtool_config.h"
34d433a562SThomas Veerman #endif
35d433a562SThomas Veerman
36d433a562SThomas Veerman #include <sys/cdefs.h>
37d433a562SThomas Veerman #if defined(__COPYRIGHT) && !defined(lint)
38d433a562SThomas Veerman __COPYRIGHT("@(#) Copyright (c) 1989, 1990, 1993\
39d433a562SThomas Veerman The Regents of the University of California. All rights reserved.");
40d433a562SThomas Veerman #endif /* not lint */
41d433a562SThomas Veerman
42d433a562SThomas Veerman #if defined(__RCSID) && !defined(lint)
43d433a562SThomas Veerman #if 0
44d433a562SThomas Veerman static char sccsid[] = "@(#)mtree.c 8.1 (Berkeley) 6/6/93";
45d433a562SThomas Veerman #else
46*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: mtree.c,v 1.50 2015/01/23 02:27:01 christos Exp $");
47d433a562SThomas Veerman #endif
48d433a562SThomas Veerman #endif /* not lint */
49d433a562SThomas Veerman
50d433a562SThomas Veerman #include <sys/param.h>
51d433a562SThomas Veerman #include <sys/stat.h>
52d433a562SThomas Veerman
53d433a562SThomas Veerman #include <errno.h>
54d433a562SThomas Veerman #include <stdio.h>
55d433a562SThomas Veerman #include <stdlib.h>
56d433a562SThomas Veerman #include <string.h>
57d433a562SThomas Veerman #include <unistd.h>
58d433a562SThomas Veerman
59d433a562SThomas Veerman #include "extern.h"
60d433a562SThomas Veerman
61d433a562SThomas Veerman int ftsoptions = FTS_PHYSICAL;
6284d9c625SLionel Sambuc int bflag, dflag, eflag, iflag, jflag, lflag, mflag, nflag, qflag, rflag,
6384d9c625SLionel Sambuc sflag, tflag, uflag;
64d433a562SThomas Veerman char fullpath[MAXPATHLEN];
65d433a562SThomas Veerman
6684d9c625SLionel Sambuc static struct {
6784d9c625SLionel Sambuc enum flavor flavor;
6884d9c625SLionel Sambuc const char name[9];
6984d9c625SLionel Sambuc } flavors[] = {
7084d9c625SLionel Sambuc {F_MTREE, "mtree"},
7184d9c625SLionel Sambuc {F_FREEBSD9, "freebsd9"},
7284d9c625SLionel Sambuc {F_NETBSD6, "netbsd6"},
7384d9c625SLionel Sambuc };
7484d9c625SLionel Sambuc
75d433a562SThomas Veerman __dead static void usage(void);
76d433a562SThomas Veerman
77d433a562SThomas Veerman int
main(int argc,char ** argv)78d433a562SThomas Veerman main(int argc, char **argv)
79d433a562SThomas Veerman {
80d433a562SThomas Veerman int ch, status;
8184d9c625SLionel Sambuc unsigned int i;
8284d9c625SLionel Sambuc int cflag, Cflag, Dflag, Uflag, wflag;
83d433a562SThomas Veerman char *dir, *p;
84a8ef0910SBen Gras FILE *spec1, *spec2;
85d433a562SThomas Veerman
86d433a562SThomas Veerman setprogname(argv[0]);
87d433a562SThomas Veerman
8884d9c625SLionel Sambuc cflag = Cflag = Dflag = Uflag = wflag = 0;
89d433a562SThomas Veerman dir = NULL;
90d433a562SThomas Veerman init_excludes();
91a8ef0910SBen Gras spec1 = stdin;
92a8ef0910SBen Gras spec2 = NULL;
93d433a562SThomas Veerman
94d433a562SThomas Veerman while ((ch = getopt(argc, argv,
9584d9c625SLionel Sambuc "bcCdDeE:f:F:I:ijk:K:lLmMnN:O:p:PqrR:s:StuUwWxX:"))
96d433a562SThomas Veerman != -1) {
97d433a562SThomas Veerman switch((char)ch) {
9884d9c625SLionel Sambuc case 'b':
9984d9c625SLionel Sambuc bflag = 1;
10084d9c625SLionel Sambuc break;
101d433a562SThomas Veerman case 'c':
102d433a562SThomas Veerman cflag = 1;
103d433a562SThomas Veerman break;
104d433a562SThomas Veerman case 'C':
105d433a562SThomas Veerman Cflag = 1;
106d433a562SThomas Veerman break;
107d433a562SThomas Veerman case 'd':
108d433a562SThomas Veerman dflag = 1;
109d433a562SThomas Veerman break;
110d433a562SThomas Veerman case 'D':
111d433a562SThomas Veerman Dflag = 1;
112d433a562SThomas Veerman break;
113d433a562SThomas Veerman case 'E':
114d433a562SThomas Veerman parsetags(&excludetags, optarg);
115d433a562SThomas Veerman break;
116d433a562SThomas Veerman case 'e':
117d433a562SThomas Veerman eflag = 1;
118d433a562SThomas Veerman break;
119d433a562SThomas Veerman case 'f':
120a8ef0910SBen Gras if (spec1 == stdin) {
121a8ef0910SBen Gras spec1 = fopen(optarg, "r");
122a8ef0910SBen Gras if (spec1 == NULL)
123a8ef0910SBen Gras mtree_err("%s: %s", optarg,
124a8ef0910SBen Gras strerror(errno));
125a8ef0910SBen Gras } else if (spec2 == NULL) {
126a8ef0910SBen Gras spec2 = fopen(optarg, "r");
127a8ef0910SBen Gras if (spec2 == NULL)
128a8ef0910SBen Gras mtree_err("%s: %s", optarg,
129a8ef0910SBen Gras strerror(errno));
130a8ef0910SBen Gras } else
131a8ef0910SBen Gras usage();
132d433a562SThomas Veerman break;
13384d9c625SLionel Sambuc case 'F':
13484d9c625SLionel Sambuc for (i = 0; i < __arraycount(flavors); i++)
13584d9c625SLionel Sambuc if (strcmp(optarg, flavors[i].name) == 0) {
13684d9c625SLionel Sambuc flavor = flavors[i].flavor;
13784d9c625SLionel Sambuc break;
13884d9c625SLionel Sambuc }
13984d9c625SLionel Sambuc if (i == __arraycount(flavors))
14084d9c625SLionel Sambuc usage();
14184d9c625SLionel Sambuc break;
142d433a562SThomas Veerman case 'i':
143d433a562SThomas Veerman iflag = 1;
144d433a562SThomas Veerman break;
145d433a562SThomas Veerman case 'I':
146d433a562SThomas Veerman parsetags(&includetags, optarg);
147d433a562SThomas Veerman break;
148a8ef0910SBen Gras case 'j':
149a8ef0910SBen Gras jflag = 1;
150a8ef0910SBen Gras break;
151d433a562SThomas Veerman case 'k':
152d433a562SThomas Veerman keys = F_TYPE;
153d433a562SThomas Veerman while ((p = strsep(&optarg, " \t,")) != NULL)
154d433a562SThomas Veerman if (*p != '\0')
155d433a562SThomas Veerman keys |= parsekey(p, NULL);
156d433a562SThomas Veerman break;
157d433a562SThomas Veerman case 'K':
158d433a562SThomas Veerman while ((p = strsep(&optarg, " \t,")) != NULL)
159d433a562SThomas Veerman if (*p != '\0')
160d433a562SThomas Veerman keys |= parsekey(p, NULL);
161d433a562SThomas Veerman break;
162d433a562SThomas Veerman case 'l':
163d433a562SThomas Veerman lflag = 1;
164d433a562SThomas Veerman break;
165d433a562SThomas Veerman case 'L':
166d433a562SThomas Veerman ftsoptions &= ~FTS_PHYSICAL;
167d433a562SThomas Veerman ftsoptions |= FTS_LOGICAL;
168d433a562SThomas Veerman break;
169d433a562SThomas Veerman case 'm':
170d433a562SThomas Veerman mflag = 1;
171d433a562SThomas Veerman break;
172d433a562SThomas Veerman case 'M':
173d433a562SThomas Veerman mtree_Mflag = 1;
174d433a562SThomas Veerman break;
175a8ef0910SBen Gras case 'n':
176a8ef0910SBen Gras nflag = 1;
177a8ef0910SBen Gras break;
178d433a562SThomas Veerman case 'N':
179d433a562SThomas Veerman if (! setup_getid(optarg))
180d433a562SThomas Veerman mtree_err(
181d433a562SThomas Veerman "Unable to use user and group databases in `%s'",
182d433a562SThomas Veerman optarg);
183d433a562SThomas Veerman break;
18484d9c625SLionel Sambuc case 'O':
18584d9c625SLionel Sambuc load_only(optarg);
18684d9c625SLionel Sambuc break;
187d433a562SThomas Veerman case 'p':
188d433a562SThomas Veerman dir = optarg;
189d433a562SThomas Veerman break;
190d433a562SThomas Veerman case 'P':
191d433a562SThomas Veerman ftsoptions &= ~FTS_LOGICAL;
192d433a562SThomas Veerman ftsoptions |= FTS_PHYSICAL;
193d433a562SThomas Veerman break;
194a8ef0910SBen Gras case 'q':
195a8ef0910SBen Gras qflag = 1;
196a8ef0910SBen Gras break;
197d433a562SThomas Veerman case 'r':
198*0a6a1f1dSLionel Sambuc rflag++;
199d433a562SThomas Veerman break;
200d433a562SThomas Veerman case 'R':
201d433a562SThomas Veerman while ((p = strsep(&optarg, " \t,")) != NULL)
202d433a562SThomas Veerman if (*p != '\0')
203d433a562SThomas Veerman keys &= ~parsekey(p, NULL);
204d433a562SThomas Veerman break;
205d433a562SThomas Veerman case 's':
206d433a562SThomas Veerman sflag = 1;
207d433a562SThomas Veerman crc_total = ~strtol(optarg, &p, 0);
208d433a562SThomas Veerman if (*p)
209d433a562SThomas Veerman mtree_err("illegal seed value -- %s", optarg);
210d433a562SThomas Veerman break;
211d433a562SThomas Veerman case 'S':
212d433a562SThomas Veerman mtree_Sflag = 1;
213d433a562SThomas Veerman break;
214d433a562SThomas Veerman case 't':
215d433a562SThomas Veerman tflag = 1;
216d433a562SThomas Veerman break;
217d433a562SThomas Veerman case 'u':
218d433a562SThomas Veerman uflag = 1;
219d433a562SThomas Veerman break;
220d433a562SThomas Veerman case 'U':
221d433a562SThomas Veerman Uflag = uflag = 1;
222d433a562SThomas Veerman break;
22384d9c625SLionel Sambuc case 'w':
22484d9c625SLionel Sambuc wflag = 1;
22584d9c625SLionel Sambuc break;
226d433a562SThomas Veerman case 'W':
227d433a562SThomas Veerman mtree_Wflag = 1;
228d433a562SThomas Veerman break;
229d433a562SThomas Veerman case 'x':
230d433a562SThomas Veerman ftsoptions |= FTS_XDEV;
231d433a562SThomas Veerman break;
232d433a562SThomas Veerman case 'X':
233d433a562SThomas Veerman read_excludes_file(optarg);
234d433a562SThomas Veerman break;
235d433a562SThomas Veerman case '?':
236d433a562SThomas Veerman default:
237d433a562SThomas Veerman usage();
238d433a562SThomas Veerman }
239d433a562SThomas Veerman }
240d433a562SThomas Veerman argc -= optind;
241d433a562SThomas Veerman argv += optind;
242d433a562SThomas Veerman
243d433a562SThomas Veerman if (argc)
244d433a562SThomas Veerman usage();
245d433a562SThomas Veerman
24684d9c625SLionel Sambuc switch (flavor) {
24784d9c625SLionel Sambuc case F_FREEBSD9:
24884d9c625SLionel Sambuc if (cflag && iflag) {
24984d9c625SLionel Sambuc warnx("-c and -i passed, replacing -i with -j for "
25084d9c625SLionel Sambuc "FreeBSD compatibility");
25184d9c625SLionel Sambuc iflag = 0;
25284d9c625SLionel Sambuc jflag = 1;
25384d9c625SLionel Sambuc }
25484d9c625SLionel Sambuc if (dflag && !bflag) {
25584d9c625SLionel Sambuc warnx("Adding -b to -d for FreeBSD compatibility");
25684d9c625SLionel Sambuc bflag = 1;
25784d9c625SLionel Sambuc }
25884d9c625SLionel Sambuc if (uflag && !iflag) {
25984d9c625SLionel Sambuc warnx("Adding -i to -%c for FreeBSD compatibility",
26084d9c625SLionel Sambuc Uflag ? 'U' : 'u');
26184d9c625SLionel Sambuc iflag = 1;
26284d9c625SLionel Sambuc }
26384d9c625SLionel Sambuc if (uflag && !tflag) {
26484d9c625SLionel Sambuc warnx("Adding -t to -%c for FreeBSD compatibility",
26584d9c625SLionel Sambuc Uflag ? 'U' : 'u');
26684d9c625SLionel Sambuc tflag = 1;
26784d9c625SLionel Sambuc }
26884d9c625SLionel Sambuc if (wflag)
26984d9c625SLionel Sambuc warnx("The -w flag is a no-op");
27084d9c625SLionel Sambuc break;
27184d9c625SLionel Sambuc default:
27284d9c625SLionel Sambuc if (wflag)
27384d9c625SLionel Sambuc usage();
27484d9c625SLionel Sambuc }
27584d9c625SLionel Sambuc
276a8ef0910SBen Gras if (spec2 && (cflag || Cflag || Dflag))
277a8ef0910SBen Gras mtree_err("Double -f, -c, -C and -D flags are mutually "
278a8ef0910SBen Gras "exclusive");
279a8ef0910SBen Gras
280a8ef0910SBen Gras if (dir && spec2)
281a8ef0910SBen Gras mtree_err("Double -f and -p flags are mutually exclusive");
282a8ef0910SBen Gras
283d433a562SThomas Veerman if (dir && chdir(dir))
284d433a562SThomas Veerman mtree_err("%s: %s", dir, strerror(errno));
285d433a562SThomas Veerman
286d433a562SThomas Veerman if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
287d433a562SThomas Veerman mtree_err("%s", strerror(errno));
288d433a562SThomas Veerman
289d433a562SThomas Veerman if ((cflag && Cflag) || (cflag && Dflag) || (Cflag && Dflag))
290d433a562SThomas Veerman mtree_err("-c, -C and -D flags are mutually exclusive");
291d433a562SThomas Veerman
292d433a562SThomas Veerman if (iflag && mflag)
293d433a562SThomas Veerman mtree_err("-i and -m flags are mutually exclusive");
294d433a562SThomas Veerman
295d433a562SThomas Veerman if (lflag && uflag)
296d433a562SThomas Veerman mtree_err("-l and -u flags are mutually exclusive");
297d433a562SThomas Veerman
298d433a562SThomas Veerman if (cflag) {
299*0a6a1f1dSLionel Sambuc cwalk(stdout);
300d433a562SThomas Veerman exit(0);
301d433a562SThomas Veerman }
302d433a562SThomas Veerman if (Cflag || Dflag) {
303*0a6a1f1dSLionel Sambuc dump_nodes(stdout, "", spec(spec1), Dflag);
304d433a562SThomas Veerman exit(0);
305d433a562SThomas Veerman }
306a8ef0910SBen Gras if (spec2 != NULL)
307a8ef0910SBen Gras status = mtree_specspec(spec1, spec2);
308a8ef0910SBen Gras else
309a8ef0910SBen Gras status = verify(spec1);
310d433a562SThomas Veerman if (Uflag && (status == MISMATCHEXIT))
311d433a562SThomas Veerman status = 0;
312d433a562SThomas Veerman exit(status);
313d433a562SThomas Veerman }
314d433a562SThomas Veerman
315d433a562SThomas Veerman static void
usage(void)316d433a562SThomas Veerman usage(void)
317d433a562SThomas Veerman {
31884d9c625SLionel Sambuc unsigned int i;
319d433a562SThomas Veerman
320d433a562SThomas Veerman fprintf(stderr,
32184d9c625SLionel Sambuc "usage: %s [-bCcDdejLlMnPqrStUuWx] [-i|-m] [-E tags]\n"
322a8ef0910SBen Gras "\t\t[-f spec] [-f spec]\n"
323d433a562SThomas Veerman "\t\t[-I tags] [-K keywords] [-k keywords] [-N dbdir] [-p path]\n"
32484d9c625SLionel Sambuc "\t\t[-R keywords] [-s seed] [-X exclude-file]\n"
32584d9c625SLionel Sambuc "\t\t[-F flavor]\n",
326d433a562SThomas Veerman getprogname());
32784d9c625SLionel Sambuc fprintf(stderr, "\nflavors:");
32884d9c625SLionel Sambuc for (i = 0; i < __arraycount(flavors); i++)
32984d9c625SLionel Sambuc fprintf(stderr, " %s", flavors[i].name);
33084d9c625SLionel Sambuc fprintf(stderr, "\n");
331d433a562SThomas Veerman exit(1);
332d433a562SThomas Veerman }
333