xref: /netbsd/usr.sbin/mtree/mtree.c (revision 6550d01e)
1 /*	$NetBSD: mtree.c,v 1.36 2009/04/08 19:43:05 wiz 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.36 2009/04/08 19:43:05 wiz 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     	rflag, sflag, tflag, uflag, Uflag;
64 char	fullpath[MAXPATHLEN];
65 
66 	int	main(int, char **);
67 static	void	usage(void);
68 
69 int
70 main(int argc, char **argv)
71 {
72 	int	ch, status;
73 	char	*dir, *p;
74 
75 	setprogname(argv[0]);
76 
77 	dir = NULL;
78 	init_excludes();
79 
80 	while ((ch = getopt(argc, argv,
81 	    "cCdDeE:f:I:ik:K:lLmMN:p:PrR:s:StuUWxX:"))
82 	    != -1) {
83 		switch((char)ch) {
84 		case 'c':
85 			cflag = 1;
86 			break;
87 		case 'C':
88 			Cflag = 1;
89 			break;
90 		case 'd':
91 			dflag = 1;
92 			break;
93 		case 'D':
94 			Dflag = 1;
95 			break;
96 		case 'E':
97 			parsetags(&excludetags, optarg);
98 			break;
99 		case 'e':
100 			eflag = 1;
101 			break;
102 		case 'f':
103 			if (!(freopen(optarg, "r", stdin)))
104 				mtree_err("%s: %s", optarg, strerror(errno));
105 			break;
106 		case 'i':
107 			iflag = 1;
108 			break;
109 		case 'I':
110 			parsetags(&includetags, optarg);
111 			break;
112 		case 'k':
113 			keys = F_TYPE;
114 			while ((p = strsep(&optarg, " \t,")) != NULL)
115 				if (*p != '\0')
116 					keys |= parsekey(p, NULL);
117 			break;
118 		case 'K':
119 			while ((p = strsep(&optarg, " \t,")) != NULL)
120 				if (*p != '\0')
121 					keys |= parsekey(p, NULL);
122 			break;
123 		case 'l':
124 			lflag = 1;
125 			break;
126 		case 'L':
127 			ftsoptions &= ~FTS_PHYSICAL;
128 			ftsoptions |= FTS_LOGICAL;
129 			break;
130 		case 'm':
131 			mflag = 1;
132 			break;
133 		case 'M':
134 			mtree_Mflag = 1;
135 			break;
136 		case 'N':
137 			if (! setup_getid(optarg))
138 				mtree_err(
139 			    "Unable to use user and group databases in `%s'",
140 				    optarg);
141 			break;
142 		case 'p':
143 			dir = optarg;
144 			break;
145 		case 'P':
146 			ftsoptions &= ~FTS_LOGICAL;
147 			ftsoptions |= FTS_PHYSICAL;
148 			break;
149 		case 'r':
150 			rflag = 1;
151 			break;
152 		case 'R':
153 			while ((p = strsep(&optarg, " \t,")) != NULL)
154 				if (*p != '\0')
155 					keys &= ~parsekey(p, NULL);
156 			break;
157 		case 's':
158 			sflag = 1;
159 			crc_total = ~strtol(optarg, &p, 0);
160 			if (*p)
161 				mtree_err("illegal seed value -- %s", optarg);
162 			break;
163 		case 'S':
164 			mtree_Sflag = 1;
165 			break;
166 		case 't':
167 			tflag = 1;
168 			break;
169 		case 'u':
170 			uflag = 1;
171 			break;
172 		case 'U':
173 			Uflag = uflag = 1;
174 			break;
175 		case 'W':
176 			mtree_Wflag = 1;
177 			break;
178 		case 'x':
179 			ftsoptions |= FTS_XDEV;
180 			break;
181 		case 'X':
182 			read_excludes_file(optarg);
183 			break;
184 		case '?':
185 		default:
186 			usage();
187 		}
188 	}
189 	argc -= optind;
190 	argv += optind;
191 
192 	if (argc)
193 		usage();
194 
195 	if (dir && chdir(dir))
196 		mtree_err("%s: %s", dir, strerror(errno));
197 
198 	if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
199 		mtree_err("%s", strerror(errno));
200 
201 	if ((cflag && Cflag) || (cflag && Dflag) || (Cflag && Dflag))
202 		mtree_err("-c, -C and -D flags are mutually exclusive");
203 
204 	if (iflag && mflag)
205 		mtree_err("-i and -m flags are mutually exclusive");
206 
207 	if (lflag && uflag)
208 		mtree_err("-l and -u flags are mutually exclusive");
209 
210 	if (cflag) {
211 		cwalk();
212 		exit(0);
213 	}
214 	if (Cflag || Dflag) {
215 		dump_nodes("", spec(stdin), Dflag);
216 		exit(0);
217 	}
218 	status = verify();
219 	if (Uflag && (status == MISMATCHEXIT))
220 		status = 0;
221 	exit(status);
222 }
223 
224 static void
225 usage(void)
226 {
227 
228 	fprintf(stderr,
229 	    "usage: %s [-CcDdeLlMPrSUuWx] [-i|-m] [-E tags] [-f spec]\n"
230 	    "\t\t[-I tags] [-K keywords] [-k keywords] [-N dbdir] [-p path]\n"
231 	    "\t\t[-R keywords] [-s seed] [-X exclude-file]\n",
232 	    getprogname());
233 	exit(1);
234 }
235