xref: /netbsd-src/usr.sbin/mtree/mtree.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: mtree.c,v 1.29 2002/12/23 04:40:19 lukem 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #if defined(__COPYRIGHT) && !defined(lint)
38 __COPYRIGHT("@(#) Copyright (c) 1989, 1990, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n");
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.29 2002/12/23 04:40:19 lukem 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, "cCdDeE:f:I:ik:K:lLmN:p:PrR:s:tuUWxX:"))
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 'N':
133 			if (! setup_getid(optarg))
134 				mtree_err(
135 			    "Unable to use user and group databases in `%s'",
136 				    optarg);
137 			break;
138 		case 'p':
139 			dir = optarg;
140 			break;
141 		case 'P':
142 			ftsoptions &= ~FTS_LOGICAL;
143 			ftsoptions |= FTS_PHYSICAL;
144 			break;
145 		case 'r':
146 			rflag = 1;
147 			break;
148 		case 'R':
149 			while ((p = strsep(&optarg, " \t,")) != NULL)
150 				if (*p != '\0')
151 					keys &= ~parsekey(p, NULL);
152 			break;
153 		case 's':
154 			sflag = 1;
155 			crc_total = ~strtol(optarg, &p, 0);
156 			if (*p)
157 				mtree_err("illegal seed value -- %s", optarg);
158 			break;
159 		case 't':
160 			tflag = 1;
161 			break;
162 		case 'u':
163 			uflag = 1;
164 			break;
165 		case 'U':
166 			Uflag = uflag = 1;
167 			break;
168 		case 'W':
169 			Wflag = 1;
170 			break;
171 		case 'x':
172 			ftsoptions |= FTS_XDEV;
173 			break;
174 		case 'X':
175 			read_excludes_file(optarg);
176 			break;
177 		case '?':
178 		default:
179 			usage();
180 		}
181 	}
182 	argc -= optind;
183 	argv += optind;
184 
185 	if (argc)
186 		usage();
187 
188 	if (dir && chdir(dir))
189 		mtree_err("%s: %s", dir, strerror(errno));
190 
191 	if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
192 		mtree_err("%s", strerror(errno));
193 
194 	if ((cflag && Cflag) || (cflag && Dflag) || (Cflag && Dflag))
195 		mtree_err("-c, -C and -D flags are mutually exclusive");
196 
197 	if (iflag && mflag)
198 		mtree_err("-i and -m flags are mutually exclusive");
199 
200 	if (lflag && uflag)
201 		mtree_err("-l and -u flags are mutually exclusive");
202 
203 	if (cflag) {
204 		cwalk();
205 		exit(0);
206 	}
207 	if (Cflag || Dflag) {
208 		dump_nodes("", spec(stdin), Dflag);
209 		exit(0);
210 	}
211 	status = verify();
212 	if (Uflag & (status == MISMATCHEXIT))
213 		status = 0;
214 	exit(status);
215 }
216 
217 static void
218 usage(void)
219 {
220 
221 	fprintf(stderr,
222 	    "usage: %s [-cCdDelLPruUWx] [-i|-m] [-f spec] [-k key]\n"
223 	    "\t\t[-K addkey] [-R removekey] [-I inctags] [-E exctags]\n"
224 	    "\t\t[-N userdbdir] [-X exclude-file] [-p path] [-s seed]\n",
225 	    getprogname());
226 	exit(1);
227 }
228