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 
42 #include "fsck_hammer2.h"
43 
44 int DebugOpt;
45 int ForceOpt;
46 int VerboseOpt;
47 int QuietOpt;
48 int CountEmpty;
49 int ScanBest;
50 int ScanPFS;
51 int NumPFSNames;
52 char **PFSNames;
53 
54 static void
55 init_pfs_names(const char *names)
56 {
57 	char *name, *h, *p;
58 	int siz = 32;
59 
60 	PFSNames = calloc(siz, sizeof(char *));
61 	p = strdup(names);
62 	h = p;
63 
64 	while ((name = p) != NULL) {
65 		p = strchr(p, ',');
66 		if (p)
67 			*p++ = 0;
68 		if (strlen(name)) {
69 			if (NumPFSNames > siz - 1) {
70 				siz *= 2;
71 				PFSNames = realloc(PFSNames,
72 				    siz * sizeof(char *));
73 			}
74 			PFSNames[NumPFSNames++] = strdup(name);
75 		}
76 	}
77 	free(h);
78 
79 	if (DebugOpt) {
80 		int i;
81 		for (i = 0; i < NumPFSNames; i++)
82 			printf("PFSNames[%d]=\"%s\"\n", i, PFSNames[i]);
83 	}
84 }
85 
86 static void
87 cleanup_pfs_names(void)
88 {
89 	int i;
90 
91 	for (i = 0; i < NumPFSNames; i++)
92 		free(PFSNames[i]);
93 	free(PFSNames);
94 }
95 
96 static void
97 usage(void)
98 {
99 	fprintf(stderr, "fsck_hammer2 [-f] [-v] [-q] [-e] [-b] [-p] "
100 	    "[-l pfs_names] special\n");
101 	exit(1);
102 }
103 
104 int
105 main(int ac, char **av)
106 {
107 	int ch;
108 
109 	while ((ch = getopt(ac, av, "dfvqebpl:")) != -1) {
110 		switch(ch) {
111 		case 'd':
112 			DebugOpt = 1;
113 			break;
114 		case 'f':
115 			ForceOpt = 1;
116 			break;
117 		case 'v':
118 			if (QuietOpt)
119 				--QuietOpt;
120 			else
121 				++VerboseOpt;
122 			break;
123 		case 'q':
124 			if (VerboseOpt)
125 				--VerboseOpt;
126 			else
127 				++QuietOpt;
128 			break;
129 		case 'e':
130 			CountEmpty = 1;
131 			break;
132 		case 'b':
133 			ScanBest = 1;
134 			break;
135 		case 'p':
136 			ScanPFS = 1;
137 			break;
138 		case 'l':
139 			init_pfs_names(optarg);
140 			break;
141 		default:
142 			usage();
143 			/* not reached */
144 			break;
145 		}
146 	}
147 
148 	ac -= optind;
149 	av += optind;
150 	if (ac < 1) {
151 		usage();
152 		/* not reached */
153 	}
154 
155 	if (test_hammer2(av[0]) == -1)
156 		exit(1);
157 
158 	cleanup_pfs_names();
159 
160 	return 0;
161 }
162