pfctl(8): Fix building with -fno-common.
[dragonfly.git] / sbin / fsck_hammer2 / fsck_hammer2.c
1 /*
2  * Copyright (c) 2019 Tomohiro Kusumi <tkusumi@netbsd.org>
3  * Copyright (c) 2019 The DragonFly Project
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@dragonflybsd.org>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  * 3. Neither the name of The DragonFly Project nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific, prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #include <unistd.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <limits.h>
42 #include <errno.h>
43
44 #include "fsck_hammer2.h"
45
46 int DebugOpt;
47 int ForceOpt;
48 int VerboseOpt;
49 int QuietOpt;
50 int CountEmpty;
51 int ScanBest;
52 int ScanPFS;
53 int NumPFSNames;
54 char **PFSNames;
55 long BlockrefCacheCount = -1;
56
57 static void
58 init_pfs_names(const char *names)
59 {
60         char *name, *h, *p;
61         int siz = 32;
62
63         PFSNames = calloc(siz, sizeof(char *));
64         p = strdup(names);
65         h = p;
66
67         while ((name = p) != NULL) {
68                 p = strchr(p, ',');
69                 if (p)
70                         *p++ = 0;
71                 if (strlen(name)) {
72                         if (NumPFSNames > siz - 1) {
73                                 siz *= 2;
74                                 PFSNames = realloc(PFSNames,
75                                     siz * sizeof(char *));
76                         }
77                         PFSNames[NumPFSNames++] = strdup(name);
78                 }
79         }
80         free(h);
81
82         if (DebugOpt) {
83                 int i;
84                 for (i = 0; i < NumPFSNames; i++)
85                         printf("PFSNames[%d]=\"%s\"\n", i, PFSNames[i]);
86         }
87 }
88
89 static void
90 cleanup_pfs_names(void)
91 {
92         int i;
93
94         for (i = 0; i < NumPFSNames; i++)
95                 free(PFSNames[i]);
96         free(PFSNames);
97 }
98
99 static void
100 usage(void)
101 {
102         fprintf(stderr, "fsck_hammer2 [-f] [-v] [-q] [-e] [-b] [-p] "
103             "[-l pfs_names] [-c cache_count] special\n");
104         exit(1);
105 }
106
107 int
108 main(int ac, char **av)
109 {
110         int ch;
111
112         while ((ch = getopt(ac, av, "dfvqebpl:c:")) != -1) {
113                 switch(ch) {
114                 case 'd':
115                         DebugOpt++;
116                         break;
117                 case 'f':
118                         ForceOpt = 1;
119                         break;
120                 case 'v':
121                         if (QuietOpt)
122                                 --QuietOpt;
123                         else
124                                 ++VerboseOpt;
125                         break;
126                 case 'q':
127                         if (VerboseOpt)
128                                 --VerboseOpt;
129                         else
130                                 ++QuietOpt;
131                         break;
132                 case 'e':
133                         CountEmpty = 1;
134                         break;
135                 case 'b':
136                         ScanBest = 1;
137                         break;
138                 case 'p':
139                         ScanPFS = 1;
140                         break;
141                 case 'l':
142                         init_pfs_names(optarg);
143                         break;
144                 case 'c':
145                         errno = 0;
146                         BlockrefCacheCount = strtol(optarg, NULL, 10);
147                         if (errno == ERANGE &&
148                             (BlockrefCacheCount == LONG_MIN ||
149                              BlockrefCacheCount == LONG_MAX)) {
150                                 perror("strtol");
151                                 exit(1);
152                         }
153                         break;
154                 default:
155                         usage();
156                         /* not reached */
157                         break;
158                 }
159         }
160
161         ac -= optind;
162         av += optind;
163         if (ac < 1) {
164                 usage();
165                 /* not reached */
166         }
167
168         if (test_hammer2(av[0]) == -1)
169                 exit(1);
170
171         cleanup_pfs_names();
172
173         return 0;
174 }