xref: /netbsd/usr.sbin/mtree/mtree.c (revision bf9ec67e)
1 /*	$NetBSD: mtree.c,v 1.28 2002/01/31 19:37:16 tv 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.28 2002/01/31 19:37:16 tv 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, 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, "cdDeE: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 'd':
87 			dflag = 1;
88 			break;
89 		case 'D':
90 			Dflag = 1;
91 			break;
92 		case 'E':
93 			parsetags(&excludetags, optarg);
94 			break;
95 		case 'e':
96 			eflag = 1;
97 			break;
98 		case 'f':
99 			if (!(freopen(optarg, "r", stdin)))
100 				mtree_err("%s: %s", optarg, strerror(errno));
101 			break;
102 		case 'i':
103 			iflag = 1;
104 			break;
105 		case 'I':
106 			parsetags(&includetags, optarg);
107 			break;
108 		case 'k':
109 			keys = F_TYPE;
110 			while ((p = strsep(&optarg, " \t,")) != NULL)
111 				if (*p != '\0')
112 					keys |= parsekey(p, NULL);
113 			break;
114 		case 'K':
115 			while ((p = strsep(&optarg, " \t,")) != NULL)
116 				if (*p != '\0')
117 					keys |= parsekey(p, NULL);
118 			break;
119 		case 'l':
120 			lflag = 1;
121 			break;
122 		case 'L':
123 			ftsoptions &= ~FTS_PHYSICAL;
124 			ftsoptions |= FTS_LOGICAL;
125 			break;
126 		case 'm':
127 			mflag = 1;
128 			break;
129 		case 'N':
130 			if (! setup_getid(optarg))
131 				mtree_err(
132 			    "Unable to use user and group databases in `%s'",
133 				    optarg);
134 			break;
135 		case 'p':
136 			dir = optarg;
137 			break;
138 		case 'P':
139 			ftsoptions &= ~FTS_LOGICAL;
140 			ftsoptions |= FTS_PHYSICAL;
141 			break;
142 		case 'r':
143 			rflag = 1;
144 			break;
145 		case 'R':
146 			while ((p = strsep(&optarg, " \t,")) != NULL)
147 				if (*p != '\0')
148 					keys &= ~parsekey(p, NULL);
149 			break;
150 		case 's':
151 			sflag = 1;
152 			crc_total = ~strtol(optarg, &p, 0);
153 			if (*p)
154 				mtree_err("illegal seed value -- %s", optarg);
155 			break;
156 		case 't':
157 			tflag = 1;
158 			break;
159 		case 'u':
160 			uflag = 1;
161 			break;
162 		case 'U':
163 			Uflag = uflag = 1;
164 			break;
165 		case 'W':
166 			Wflag = 1;
167 			break;
168 		case 'x':
169 			ftsoptions |= FTS_XDEV;
170 			break;
171 		case 'X':
172 			read_excludes_file(optarg);
173 			break;
174 		case '?':
175 		default:
176 			usage();
177 		}
178 	}
179 	argc -= optind;
180 	argv += optind;
181 
182 	if (argc)
183 		usage();
184 
185 	if (dir && chdir(dir))
186 		mtree_err("%s: %s", dir, strerror(errno));
187 
188 	if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
189 		mtree_err("%s", strerror(errno));
190 
191 	if (cflag == 1 && Dflag == 1)
192 		mtree_err("-c and -D flags are mutually exclusive");
193 
194 	if (iflag == 1 && mflag == 1)
195 		mtree_err("-i and -m flags are mutually exclusive");
196 
197 	if (lflag == 1 && uflag == 1)
198 		mtree_err("-l and -u flags are mutually exclusive");
199 
200 	if (cflag) {
201 		cwalk();
202 		exit(0);
203 	}
204 	if (Dflag) {
205 		dump_nodes("", spec(stdin));
206 		exit(0);
207 	}
208 	status = verify();
209 	if (Uflag & (status == MISMATCHEXIT))
210 		status = 0;
211 	exit(status);
212 }
213 
214 static void
215 usage(void)
216 {
217 
218 	fprintf(stderr,
219 	    "usage: %s [-cdDelLPruUWx] [-i|-m] [-f spec] [-k key]\n"
220 	    "\t\t[-K addkey] [-R removekey] [-I inctags] [-E exctags]\n"
221 	    "\t\t[-N userdbdir] [-X exclude-file] [-p path] [-s seed]\n",
222 	    getprogname());
223 	exit(1);
224 }
225