Merge branch 'vendor/TOP'
[dragonfly.git] / usr.sbin / mtree / mtree.c
1 /*-
2  * Copyright (c) 1989, 1990, 1993
3  *      The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1989, 1990, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)mtree.c  8.1 (Berkeley) 6/6/93
31  * $FreeBSD: src/usr.sbin/mtree/mtree.c,v 1.8.2.3 2003/05/07 17:55:17 tobez Exp $
32  */
33
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <fts.h>
39 #include <stdio.h>
40 #include <unistd.h>
41 #include "mtree.h"
42 #include "extern.h"
43
44 int ftsoptions = FTS_PHYSICAL;
45 int cflag, dflag, eflag, iflag, nflag, qflag, rflag, sflag, uflag, Uflag;
46 u_int keys;
47 char fullpath[MAXPATHLEN];
48
49 static void usage(void);
50
51 int
52 main(int argc, char *argv[])
53 {
54         int ch;
55         char *dir, *p;
56         int status;
57
58         dir = NULL;
59         keys = KEYDEFAULT;
60         init_excludes();
61
62         while ((ch = getopt(argc, argv, "cdef:iK:k:LnPp:qrs:UuxX:")) != -1)
63                 switch((char)ch) {
64                 case 'c':
65                         cflag = 1;
66                         break;
67                 case 'd':
68                         dflag = 1;
69                         break;
70                 case 'e':
71                         eflag = 1;
72                         break;
73                 case 'f':
74                         if (!(freopen(optarg, "r", stdin)))
75                                 err(1, "%s", optarg);
76                         break;
77                 case 'i':
78                         iflag = 1;
79                         break;
80                 case 'K':
81                         while ((p = strsep(&optarg, " \t,")) != NULL)
82                                 if (*p != '\0')
83                                         keys |= parsekey(p, NULL);
84                         break;
85                 case 'k':
86                         keys = F_TYPE;
87                         while ((p = strsep(&optarg, " \t,")) != NULL)
88                                 if (*p != '\0')
89                                         keys |= parsekey(p, NULL);
90                         break;
91                 case 'L':
92                         ftsoptions &= ~FTS_PHYSICAL;
93                         ftsoptions |= FTS_LOGICAL;
94                         break;
95                 case 'n':
96                         nflag = 1;
97                         break;
98                 case 'P':
99                         ftsoptions &= ~FTS_LOGICAL;
100                         ftsoptions |= FTS_PHYSICAL;
101                         break;
102                 case 'p':
103                         dir = optarg;
104                         break;
105                 case 'q':
106                         qflag = 1;
107                         break;
108                 case 'r':
109                         rflag = 1;
110                         break;
111                 case 's':
112                         sflag = 1;
113                         crc_total = ~strtol(optarg, &p, 0);
114                         if (*p)
115                                 errx(1, "illegal seed value -- %s", optarg);
116                         break;
117                 case 'U':
118                         Uflag = 1;
119                         uflag = 1;
120                         break;
121                 case 'u':
122                         uflag = 1;
123                         break;
124                 case 'x':
125                         ftsoptions |= FTS_XDEV;
126                         break;
127                 case 'X':
128                         read_excludes_file(optarg);
129                         break;
130                 case '?':
131                 default:
132                         usage();
133                 }
134         argc -= optind;
135         argv += optind;
136
137         if (argc)
138                 usage();
139
140         if (dir && chdir(dir))
141                 err(1, "%s", dir);
142
143         if ((cflag || sflag) && !getcwd(fullpath, sizeof(fullpath)))
144                 errx(1, "%s", fullpath);
145
146         if (cflag) {
147                 cwalk();
148                 exit(0);
149         }
150         status = verify();
151         if (Uflag & (status == MISMATCHEXIT))
152                 status = 0;
153         exit(status);
154 }
155
156 static void
157 usage(void)
158 {
159
160         fprintf(stderr,
161 "usage: mtree [-LPUcdeinqrux] [-f spec] [-K key] [-k key] [-p path] [-s seed]\n"
162 "\t[-X excludes]\n");
163         exit(1);
164 }