mtree.c revision 1.30 1 /* $NetBSD: mtree.c,v 1.30 2003/08/07 11:25:36 agc 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 #include <sys/cdefs.h>
33 #if defined(__COPYRIGHT) && !defined(lint)
34 __COPYRIGHT("@(#) Copyright (c) 1989, 1990, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #if defined(__RCSID) && !defined(lint)
39 #if 0
40 static char sccsid[] = "@(#)mtree.c 8.1 (Berkeley) 6/6/93";
41 #else
42 __RCSID("$NetBSD: mtree.c,v 1.30 2003/08/07 11:25:36 agc Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/stat.h>
48
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include "extern.h"
56
57 int ftsoptions = FTS_PHYSICAL;
58 int cflag, Cflag, dflag, Dflag, eflag, iflag, lflag, mflag,
59 rflag, sflag, tflag, uflag, Uflag;
60 char fullpath[MAXPATHLEN];
61
62 int main(int, char **);
63 static void usage(void);
64
65 int
66 main(int argc, char **argv)
67 {
68 int ch, status;
69 char *dir, *p;
70
71 setprogname(argv[0]);
72
73 dir = NULL;
74 init_excludes();
75
76 while ((ch = getopt(argc, argv, "cCdDeE:f:I:ik:K:lLmN:p:PrR:s:tuUWxX:"))
77 != -1) {
78 switch((char)ch) {
79 case 'c':
80 cflag = 1;
81 break;
82 case 'C':
83 Cflag = 1;
84 break;
85 case 'd':
86 dflag = 1;
87 break;
88 case 'D':
89 Dflag = 1;
90 break;
91 case 'E':
92 parsetags(&excludetags, optarg);
93 break;
94 case 'e':
95 eflag = 1;
96 break;
97 case 'f':
98 if (!(freopen(optarg, "r", stdin)))
99 mtree_err("%s: %s", optarg, strerror(errno));
100 break;
101 case 'i':
102 iflag = 1;
103 break;
104 case 'I':
105 parsetags(&includetags, optarg);
106 break;
107 case 'k':
108 keys = F_TYPE;
109 while ((p = strsep(&optarg, " \t,")) != NULL)
110 if (*p != '\0')
111 keys |= parsekey(p, NULL);
112 break;
113 case 'K':
114 while ((p = strsep(&optarg, " \t,")) != NULL)
115 if (*p != '\0')
116 keys |= parsekey(p, NULL);
117 break;
118 case 'l':
119 lflag = 1;
120 break;
121 case 'L':
122 ftsoptions &= ~FTS_PHYSICAL;
123 ftsoptions |= FTS_LOGICAL;
124 break;
125 case 'm':
126 mflag = 1;
127 break;
128 case 'N':
129 if (! setup_getid(optarg))
130 mtree_err(
131 "Unable to use user and group databases in `%s'",
132 optarg);
133 break;
134 case 'p':
135 dir = optarg;
136 break;
137 case 'P':
138 ftsoptions &= ~FTS_LOGICAL;
139 ftsoptions |= FTS_PHYSICAL;
140 break;
141 case 'r':
142 rflag = 1;
143 break;
144 case 'R':
145 while ((p = strsep(&optarg, " \t,")) != NULL)
146 if (*p != '\0')
147 keys &= ~parsekey(p, NULL);
148 break;
149 case 's':
150 sflag = 1;
151 crc_total = ~strtol(optarg, &p, 0);
152 if (*p)
153 mtree_err("illegal seed value -- %s", optarg);
154 break;
155 case 't':
156 tflag = 1;
157 break;
158 case 'u':
159 uflag = 1;
160 break;
161 case 'U':
162 Uflag = uflag = 1;
163 break;
164 case 'W':
165 Wflag = 1;
166 break;
167 case 'x':
168 ftsoptions |= FTS_XDEV;
169 break;
170 case 'X':
171 read_excludes_file(optarg);
172 break;
173 case '?':
174 default:
175 usage();
176 }
177 }
178 argc -= optind;
179 argv += optind;
180
181 if (argc)
182 usage();
183
184 if (dir && chdir(dir))
185 mtree_err("%s: %s", dir, strerror(errno));
186
187 if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
188 mtree_err("%s", strerror(errno));
189
190 if ((cflag && Cflag) || (cflag && Dflag) || (Cflag && Dflag))
191 mtree_err("-c, -C and -D flags are mutually exclusive");
192
193 if (iflag && mflag)
194 mtree_err("-i and -m flags are mutually exclusive");
195
196 if (lflag && uflag)
197 mtree_err("-l and -u flags are mutually exclusive");
198
199 if (cflag) {
200 cwalk();
201 exit(0);
202 }
203 if (Cflag || Dflag) {
204 dump_nodes("", spec(stdin), Dflag);
205 exit(0);
206 }
207 status = verify();
208 if (Uflag & (status == MISMATCHEXIT))
209 status = 0;
210 exit(status);
211 }
212
213 static void
214 usage(void)
215 {
216
217 fprintf(stderr,
218 "usage: %s [-cCdDelLPruUWx] [-i|-m] [-f spec] [-k key]\n"
219 "\t\t[-K addkey] [-R removekey] [-I inctags] [-E exctags]\n"
220 "\t\t[-N userdbdir] [-X exclude-file] [-p path] [-s seed]\n",
221 getprogname());
222 exit(1);
223 }
224