mtree.c revision 1.39 1 /* $NetBSD: mtree.c,v 1.39 2012/10/05 01:05:14 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if defined(__COPYRIGHT) && !defined(lint)
38 __COPYRIGHT("@(#) Copyright (c) 1989, 1990, 1993\
39 The Regents of the University of California. All rights reserved.");
40 #endif /* not lint */
41
42 #if defined(__RCSID) && !defined(lint)
43 #if 0
44 static char sccsid[] = "@(#)mtree.c 8.1 (Berkeley) 6/6/93";
45 #else
46 __RCSID("$NetBSD: mtree.c,v 1.39 2012/10/05 01:05:14 christos Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/stat.h>
52
53 #include <errno.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #include "extern.h"
60
61 int ftsoptions = FTS_PHYSICAL;
62 int cflag, Cflag, dflag, Dflag, eflag, iflag, lflag, mflag,
63 nflag, qflag, rflag, sflag, tflag, uflag, Uflag;
64 char fullpath[MAXPATHLEN];
65
66 __dead static void usage(void);
67
68 int
69 main(int argc, char **argv)
70 {
71 int ch, status;
72 char *dir, *p;
73
74 setprogname(argv[0]);
75
76 dir = NULL;
77 init_excludes();
78
79 while ((ch = getopt(argc, argv,
80 "cCdDeE:f:I:ik:K:lLmMnN:p:PqrR:s:StuUWxX:"))
81 != -1) {
82 switch((char)ch) {
83 case 'c':
84 cflag = 1;
85 break;
86 case 'C':
87 Cflag = 1;
88 break;
89 case 'd':
90 dflag = 1;
91 break;
92 case 'D':
93 Dflag = 1;
94 break;
95 case 'E':
96 parsetags(&excludetags, optarg);
97 break;
98 case 'e':
99 eflag = 1;
100 break;
101 case 'f':
102 if (!(freopen(optarg, "r", stdin)))
103 mtree_err("%s: %s", optarg, strerror(errno));
104 break;
105 case 'i':
106 iflag = 1;
107 break;
108 case 'I':
109 parsetags(&includetags, optarg);
110 break;
111 case 'k':
112 keys = F_TYPE;
113 while ((p = strsep(&optarg, " \t,")) != NULL)
114 if (*p != '\0')
115 keys |= parsekey(p, NULL);
116 break;
117 case 'K':
118 while ((p = strsep(&optarg, " \t,")) != NULL)
119 if (*p != '\0')
120 keys |= parsekey(p, NULL);
121 break;
122 case 'l':
123 lflag = 1;
124 break;
125 case 'L':
126 ftsoptions &= ~FTS_PHYSICAL;
127 ftsoptions |= FTS_LOGICAL;
128 break;
129 case 'm':
130 mflag = 1;
131 break;
132 case 'M':
133 mtree_Mflag = 1;
134 break;
135 case 'n':
136 nflag = 1;
137 break;
138 case 'N':
139 if (! setup_getid(optarg))
140 mtree_err(
141 "Unable to use user and group databases in `%s'",
142 optarg);
143 break;
144 case 'p':
145 dir = optarg;
146 break;
147 case 'P':
148 ftsoptions &= ~FTS_LOGICAL;
149 ftsoptions |= FTS_PHYSICAL;
150 break;
151 case 'q':
152 qflag = 1;
153 break;
154 case 'r':
155 rflag = 1;
156 break;
157 case 'R':
158 while ((p = strsep(&optarg, " \t,")) != NULL)
159 if (*p != '\0')
160 keys &= ~parsekey(p, NULL);
161 break;
162 case 's':
163 sflag = 1;
164 crc_total = ~strtol(optarg, &p, 0);
165 if (*p)
166 mtree_err("illegal seed value -- %s", optarg);
167 break;
168 case 'S':
169 mtree_Sflag = 1;
170 break;
171 case 't':
172 tflag = 1;
173 break;
174 case 'u':
175 uflag = 1;
176 break;
177 case 'U':
178 Uflag = uflag = 1;
179 break;
180 case 'W':
181 mtree_Wflag = 1;
182 break;
183 case 'x':
184 ftsoptions |= FTS_XDEV;
185 break;
186 case 'X':
187 read_excludes_file(optarg);
188 break;
189 case '?':
190 default:
191 usage();
192 }
193 }
194 argc -= optind;
195 argv += optind;
196
197 if (argc)
198 usage();
199
200 if (dir && chdir(dir))
201 mtree_err("%s: %s", dir, strerror(errno));
202
203 if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
204 mtree_err("%s", strerror(errno));
205
206 if ((cflag && Cflag) || (cflag && Dflag) || (Cflag && Dflag))
207 mtree_err("-c, -C and -D flags are mutually exclusive");
208
209 if (iflag && mflag)
210 mtree_err("-i and -m flags are mutually exclusive");
211
212 if (lflag && uflag)
213 mtree_err("-l and -u flags are mutually exclusive");
214
215 if (cflag) {
216 cwalk();
217 exit(0);
218 }
219 if (Cflag || Dflag) {
220 dump_nodes("", spec(stdin), Dflag);
221 exit(0);
222 }
223 status = verify();
224 if (Uflag && (status == MISMATCHEXIT))
225 status = 0;
226 exit(status);
227 }
228
229 static void
230 usage(void)
231 {
232
233 fprintf(stderr,
234 "usage: %s [-CcDdeLlMnPrSUuWx] [-i|-m] [-E tags] [-f spec]\n"
235 "\t\t[-I tags] [-K keywords] [-k keywords] [-N dbdir] [-p path]\n"
236 "\t\t[-R keywords] [-s seed] [-X exclude-file]\n",
237 getprogname());
238 exit(1);
239 }
240