xref: /netbsd-src/sbin/fsck/fsutil.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: fsutil.c,v 1.7 1998/07/30 17:41:03 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __RCSID("$NetBSD: fsutil.c,v 1.7 1998/07/30 17:41:03 thorpej Exp $");
39 #endif /* not lint */
40 
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #if __STDC__
45 #include <stdarg.h>
46 #else
47 #include <varargs.h>
48 #endif
49 #include <errno.h>
50 #include <fstab.h>
51 #include <err.h>
52 
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 
56 #include "fsutil.h"
57 
58 static const char *dev = NULL;
59 static int hot = 0;
60 static int preen = 0;
61 
62 extern char *__progname;
63 
64 static void vmsg __P((int, const char *, va_list));
65 
66 void
67 setcdevname(cd, pr)
68 	const char *cd;
69 	int pr;
70 {
71 	dev = cd;
72 	preen = pr;
73 }
74 
75 const char *
76 cdevname()
77 {
78 	return dev;
79 }
80 
81 int
82 hotroot()
83 {
84 	return hot;
85 }
86 
87 /*VARARGS*/
88 void
89 #if __STDC__
90 errexit(const char *fmt, ...)
91 #else
92 errexit(va_alist)
93 	va_dcl
94 #endif
95 {
96 	va_list ap;
97 
98 #if __STDC__
99 	va_start(ap, fmt);
100 #else
101 	const char *fmt;
102 
103 	va_start(ap);
104 	fmt = va_arg(ap, const char *);
105 #endif
106 	(void) vfprintf(stderr, fmt, ap);
107 	va_end(ap);
108 	exit(8);
109 }
110 
111 static void
112 vmsg(fatal, fmt, ap)
113 	int fatal;
114 	const char *fmt;
115 	va_list ap;
116 {
117 	if (!fatal && preen)
118 		(void) printf("%s: ", dev);
119 
120 	(void) vprintf(fmt, ap);
121 
122 	if (fatal && preen)
123 		(void) printf("\n");
124 
125 	if (fatal && preen) {
126 		(void) printf(
127 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
128 		    dev, __progname);
129 		exit(8);
130 	}
131 }
132 
133 /*VARARGS*/
134 void
135 #if __STDC__
136 pfatal(const char *fmt, ...)
137 #else
138 pfatal(va_alist)
139 	va_dcl
140 #endif
141 {
142 	va_list ap;
143 
144 #if __STDC__
145 	va_start(ap, fmt);
146 #else
147 	const char *fmt;
148 
149 	va_start(ap);
150 	fmt = va_arg(ap, const char *);
151 #endif
152 	vmsg(1, fmt, ap);
153 	va_end(ap);
154 }
155 
156 /*VARARGS*/
157 void
158 #if __STDC__
159 pwarn(const char *fmt, ...)
160 #else
161 pwarn(va_alist)
162 	va_dcl
163 #endif
164 {
165 	va_list ap;
166 #if __STDC__
167 	va_start(ap, fmt);
168 #else
169 	const char *fmt;
170 
171 	va_start(ap);
172 	fmt = va_arg(ap, const char *);
173 #endif
174 	vmsg(0, fmt, ap);
175 	va_end(ap);
176 }
177 
178 void
179 perror(s)
180 	const char *s;
181 {
182 	pfatal("%s (%s)", s, strerror(errno));
183 }
184 
185 void
186 #if __STDC__
187 panic(const char *fmt, ...)
188 #else
189 panic(va_alist)
190 	va_dcl
191 #endif
192 {
193 	va_list ap;
194 
195 #if __STDC__
196 	va_start(ap, fmt);
197 #else
198 	const char *fmt;
199 
200 	va_start(ap);
201 	fmt = va_arg(ap, const char *);
202 #endif
203 	vmsg(1, fmt, ap);
204 	va_end(ap);
205 	exit(8);
206 }
207 
208 const char *
209 unrawname(name)
210 	const char *name;
211 {
212 	static char unrawbuf[32];
213 	const char *dp;
214 	struct stat stb;
215 
216 	if ((dp = strrchr(name, '/')) == 0)
217 		return (name);
218 	if (stat(name, &stb) < 0)
219 		return (name);
220 	if (!S_ISCHR(stb.st_mode))
221 		return (name);
222 	if (dp[1] != 'r')
223 		return (name);
224 	(void)snprintf(unrawbuf, 32, "%.*s/%s", (int)(dp - name), name, dp + 2);
225 	return (unrawbuf);
226 }
227 
228 const char *
229 rawname(name)
230 	const char *name;
231 {
232 	static char rawbuf[32];
233 	const char *dp;
234 
235 	if ((dp = strrchr(name, '/')) == 0)
236 		return (0);
237 	(void)snprintf(rawbuf, 32, "%.*s/r%s", (int)(dp - name), name, dp + 1);
238 	return (rawbuf);
239 }
240 
241 const char *
242 blockcheck(origname)
243 	const char *origname;
244 {
245 	struct stat stslash, stblock, stchar;
246 	const char *newname, *raw;
247 	struct fstab *fsp;
248 	int retried = 0;
249 
250 	hot = 0;
251 	if (stat("/", &stslash) < 0) {
252 		perror("/");
253 		printf("Can't stat root\n");
254 		return (origname);
255 	}
256 	newname = origname;
257 retry:
258 	if (stat(newname, &stblock) < 0) {
259 		perror(newname);
260 		printf("Can't stat %s\n", newname);
261 		return (origname);
262 	}
263 	if (S_ISBLK(stblock.st_mode)) {
264 		if (stslash.st_dev == stblock.st_rdev)
265 			hot++;
266 		raw = rawname(newname);
267 		if (stat(raw, &stchar) < 0) {
268 			perror(raw);
269 			printf("Can't stat %s\n", raw);
270 			return (origname);
271 		}
272 		if (S_ISCHR(stchar.st_mode)) {
273 			return (raw);
274 		} else {
275 			printf("%s is not a character device\n", raw);
276 			return (origname);
277 		}
278 	} else if (S_ISCHR(stblock.st_mode) && !retried) {
279 		newname = unrawname(newname);
280 		retried++;
281 		goto retry;
282 	} else if ((fsp = getfsfile(newname)) != 0 && !retried) {
283 		newname = fsp->fs_spec;
284 		retried++;
285 		goto retry;
286 	}
287 	/*
288 	 * Not a block or character device, just return name and
289 	 * let the user decide whether to use it.
290 	 */
291 	return (origname);
292 }
293 
294 
295 void *
296 emalloc(s)
297 	size_t s;
298 {
299 	void *p;
300 
301 	p = malloc(s);
302 	if (p == NULL)
303 		err(1, "malloc failed");
304 	return (p);
305 }
306 
307 
308 void *
309 erealloc(p, s)
310 	void *p;
311 	size_t s;
312 {
313 	void *q;
314 
315 	q = realloc(p, s);
316 	if (q == NULL)
317 		err(1, "realloc failed");
318 	return (q);
319 }
320 
321 
322 char *
323 estrdup(s)
324 	const char *s;
325 {
326 	char *p;
327 
328 	p = strdup(s);
329 	if (p == NULL)
330 		err(1, "strdup failed");
331 	return (p);
332 }
333