xref: /netbsd-src/sbin/fsck/fsutil.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: fsutil.c,v 1.14 2003/10/20 12:04:38 dsl 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: fsutil.c,v 1.14 2003/10/20 12:04:38 dsl Exp $");
35 #endif /* not lint */
36 
37 #include <sys/param.h>
38 
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <stdarg.h>
43 #include <errno.h>
44 #include <fstab.h>
45 #include <err.h>
46 
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 
50 #include "fsutil.h"
51 
52 static const char *dev = NULL;
53 static int hot = 0;
54 static int preen = 0;
55 int quiet;
56 
57 void
58 setcdevname(const char *cd, int pr)
59 {
60 
61 	dev = cd;
62 	preen = pr;
63 }
64 
65 const char *
66 cdevname(void)
67 {
68 
69 	return dev;
70 }
71 
72 int
73 hotroot(void)
74 {
75 
76 	return hot;
77 }
78 
79 /*VARARGS*/
80 void
81 errexit(const char *fmt, ...)
82 {
83 	va_list ap;
84 
85 	va_start(ap, fmt);
86 	(void) vfprintf(stderr, fmt, ap);
87 	va_end(ap);
88 	exit(8);
89 }
90 
91 void
92 vmsg(int fatal, const char *fmt, va_list ap)
93 {
94 
95 	if (!fatal && preen)
96 		(void)printf("%s: ", dev);
97 	if (quiet && !preen) {
98 		(void)printf("** %s (vmsg)\n", dev);
99 		quiet = 0;
100 	}
101 
102 	(void) vprintf(fmt, ap);
103 
104 	if (fatal && preen)
105 		(void) printf("\n");
106 
107 	if (fatal && preen) {
108 		(void) printf(
109 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
110 		    dev, getprogname());
111 		exit(8);
112 	}
113 }
114 
115 /*VARARGS*/
116 void
117 pfatal(const char *fmt, ...)
118 {
119 	va_list ap;
120 
121 	va_start(ap, fmt);
122 	vmsg(1, fmt, ap);
123 	va_end(ap);
124 }
125 
126 /*VARARGS*/
127 void
128 pwarn(const char *fmt, ...)
129 {
130 	va_list ap;
131 
132 	va_start(ap, fmt);
133 	vmsg(0, fmt, ap);
134 	va_end(ap);
135 }
136 
137 void
138 perror(const char *s)
139 {
140 
141 	pfatal("%s (%s)", s, strerror(errno));
142 }
143 
144 void
145 panic(const char *fmt, ...)
146 {
147 	va_list ap;
148 
149 	va_start(ap, fmt);
150 	vmsg(1, fmt, ap);
151 	va_end(ap);
152 	exit(8);
153 }
154 
155 const char *
156 unrawname(const char *name)
157 {
158 	static char unrawbuf[MAXPATHLEN];
159 	const char *dp;
160 	struct stat stb;
161 
162 	if ((dp = strrchr(name, '/')) == 0)
163 		return (name);
164 	if (stat(name, &stb) < 0)
165 		return (name);
166 	if (!S_ISCHR(stb.st_mode))
167 		return (name);
168 	if (dp[1] != 'r')
169 		return (name);
170 	(void)snprintf(unrawbuf, sizeof(unrawbuf), "%.*s/%s",
171 	    (int)(dp - name), name, dp + 2);
172 	return (unrawbuf);
173 }
174 
175 const char *
176 rawname(const char *name)
177 {
178 	static char rawbuf[MAXPATHLEN];
179 	const char *dp;
180 
181 	if ((dp = strrchr(name, '/')) == 0)
182 		return (0);
183 	(void)snprintf(rawbuf, sizeof(rawbuf), "%.*s/r%s",
184 	    (int)(dp - name), name, dp + 1);
185 	return (rawbuf);
186 }
187 
188 const char *
189 blockcheck(const char *origname)
190 {
191 	struct stat stslash, stblock, stchar;
192 	const char *newname, *raw;
193 	struct fstab *fsp;
194 	int retried = 0;
195 
196 	hot = 0;
197 	if (stat("/", &stslash) < 0) {
198 		perror("/");
199 		printf("Can't stat root\n");
200 		return (origname);
201 	}
202 	newname = origname;
203 retry:
204 	if (stat(newname, &stblock) < 0) {
205 		perror(newname);
206 		printf("Can't stat %s\n", newname);
207 		return (origname);
208 	}
209 	if (S_ISBLK(stblock.st_mode)) {
210 		if (stslash.st_dev == stblock.st_rdev)
211 			hot++;
212 		raw = rawname(newname);
213 		if (stat(raw, &stchar) < 0) {
214 			perror(raw);
215 			printf("Can't stat %s\n", raw);
216 			return (origname);
217 		}
218 		if (S_ISCHR(stchar.st_mode)) {
219 			return (raw);
220 		} else {
221 			printf("%s is not a character device\n", raw);
222 			return (origname);
223 		}
224 	} else if (S_ISCHR(stblock.st_mode) && !retried) {
225 		newname = unrawname(newname);
226 		retried++;
227 		goto retry;
228 	} else if ((fsp = getfsfile(newname)) != 0 && !retried) {
229 		newname = fsp->fs_spec;
230 		retried++;
231 		goto retry;
232 	}
233 	/*
234 	 * Not a block or character device, just return name and
235 	 * let the user decide whether to use it.
236 	 */
237 	return (origname);
238 }
239 
240 
241 void *
242 emalloc(size_t s)
243 {
244 	void *p;
245 
246 	p = malloc(s);
247 	if (p == NULL)
248 		err(1, "malloc failed");
249 	return (p);
250 }
251 
252 
253 void *
254 erealloc(void *p, size_t s)
255 {
256 	void *q;
257 
258 	q = realloc(p, s);
259 	if (q == NULL)
260 		err(1, "realloc failed");
261 	return (q);
262 }
263 
264 
265 char *
266 estrdup(const char *s)
267 {
268 	char *p;
269 
270 	p = strdup(s);
271 	if (p == NULL)
272 		err(1, "strdup failed");
273 	return (p);
274 }
275