xref: /original-bsd/usr.sbin/mtree/mtree.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1989, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1989, 1990, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)mtree.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #include <sys/stat.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <fts.h>
24 #include "mtree.h"
25 #include "extern.h"
26 
27 extern int crc_total;
28 
29 int ftsoptions = FTS_PHYSICAL;
30 int cflag, dflag, eflag, rflag, sflag, uflag;
31 u_short keys;
32 char fullpath[MAXPATHLEN];
33 
34 static void usage __P((void));
35 
36 int
37 main(argc, argv)
38 	int argc;
39 	char *argv[];
40 {
41 	extern int optind;
42 	extern char *optarg;
43 	int ch;
44 	char *dir, *p;
45 
46 	dir = NULL;
47 	keys = KEYDEFAULT;
48 	while ((ch = getopt(argc, argv, "cdef:K:k:p:rs:ux")) != EOF)
49 		switch((char)ch) {
50 		case 'c':
51 			cflag = 1;
52 			break;
53 		case 'd':
54 			dflag = 1;
55 			break;
56 		case 'e':
57 			eflag = 1;
58 			break;
59 		case 'f':
60 			if (!(freopen(optarg, "r", stdin)))
61 				err("%s: %s", optarg, strerror(errno));
62 			break;
63 		case 'K':
64 			while ((p = strsep(&optarg, " \t,")) != NULL)
65 				if (*p != '\0')
66 					keys |= parsekey(p, NULL);
67 			break;
68 		case 'k':
69 			keys = F_TYPE;
70 			while ((p = strsep(&optarg, " \t,")) != NULL)
71 				if (*p != '\0')
72 					keys |= parsekey(p, NULL);
73 			break;
74 		case 'p':
75 			dir = optarg;
76 			break;
77 		case 'r':
78 			rflag = 1;
79 			break;
80 		case 's':
81 			sflag = 1;
82 			crc_total = ~strtol(optarg, &p, 0);
83 			if (*p)
84 				err("illegal seed value -- %s", optarg);
85 		case 'u':
86 			uflag = 1;
87 			break;
88 		case 'x':
89 			ftsoptions |= FTS_XDEV;
90 			break;
91 		case '?':
92 		default:
93 			usage();
94 		}
95 	argc -= optind;
96 	argv += optind;
97 
98 	if (argc)
99 		usage();
100 
101 	if (dir && chdir(dir))
102 		err("%s: %s", dir, strerror(errno));
103 
104 	if ((cflag || sflag) && !getwd(fullpath))
105 		err("%s", fullpath);
106 
107 	if (cflag) {
108 		cwalk();
109 		exit(0);
110 	}
111 	exit(verify());
112 }
113 
114 static void
115 usage()
116 {
117 	(void)fprintf(stderr,
118 "usage: mtree [-cderux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n");
119 	exit(1);
120 }
121