xref: /netbsd-src/usr.sbin/mtree/mtree.c (revision 5f7096188587a2c7c95fa3c69b78e1ec9c7923d0)
1 /*-
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /* from: static char sccsid[] = "@(#)mtree.c	5.10 (Berkeley) 4/17/92"; */
42 static char *rcsid = "$Id: mtree.c,v 1.3 1993/11/02 07:51:12 cgd Exp $";
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <errno.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <fts.h>
51 #include "mtree.h"
52 #include "extern.h"
53 
54 extern int crc_total;
55 
56 int ftsoptions = FTS_PHYSICAL;
57 int cflag, dflag, eflag, rflag, sflag, uflag;
58 u_short keys;
59 char fullpath[MAXPATHLEN];
60 
61 static void usage __P((void));
62 
63 int
64 main(argc, argv)
65 	int argc;
66 	char *argv[];
67 {
68 	extern int optind;
69 	extern char *optarg;
70 	int ch;
71 	char *dir, *p;
72 
73 	dir = NULL;
74 	keys = KEYDEFAULT;
75 	while ((ch = getopt(argc, argv, "cdef:K:k:p:rs:ux")) != EOF)
76 		switch((char)ch) {
77 		case 'c':
78 			cflag = 1;
79 			break;
80 		case 'd':
81 			dflag = 1;
82 			break;
83 		case 'e':
84 			eflag = 1;
85 			break;
86 		case 'f':
87 			if (!(freopen(optarg, "r", stdin)))
88 				err("%s: %s", optarg, strerror(errno));
89 			break;
90 		case 'K':
91 			while ((p = strsep(&optarg, " \t,")) != NULL)
92 				if (*p != '\0')
93 					keys |= parsekey(p, NULL);
94 			break;
95 		case 'k':
96 			keys = F_TYPE;
97 			while ((p = strsep(&optarg, " \t,")) != NULL)
98 				if (*p != '\0')
99 					keys |= parsekey(p, NULL);
100 			break;
101 		case 'p':
102 			dir = optarg;
103 			break;
104 		case 'r':
105 			rflag = 1;
106 			break;
107 		case 's':
108 			sflag = 1;
109 			crc_total = ~strtol(optarg, &p, 0);
110 			if (*p)
111 				err("illegal seed value -- %s", optarg);
112 		case 'u':
113 			uflag = 1;
114 			break;
115 		case 'x':
116 			ftsoptions |= FTS_XDEV;
117 			break;
118 		case '?':
119 		default:
120 			usage();
121 		}
122 	argc -= optind;
123 	argv += optind;
124 
125 	if (argc)
126 		usage();
127 
128 	if (dir && chdir(dir))
129 		err("%s: %s", dir, strerror(errno));
130 
131 	if ((cflag || sflag) && !getwd(fullpath))
132 		err("%s", fullpath);
133 
134 	if (cflag) {
135 		cwalk();
136 		exit(0);
137 	}
138 	exit(verify());
139 }
140 
141 static void
142 usage()
143 {
144 	(void)fprintf(stderr,
145 "usage: mtree [-cderux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n");
146 	exit(1);
147 }
148