1 /* $NetBSD: fsutil.c,v 1.7 1998/07/30 17:41:03 thorpej Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
38 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: fsutil.c,v 1.7 1998/07/30 17:41:03 thorpej Exp $");
56 #include <sys/param.h>
58 #include <sys/mount.h>
62 static const char *dev = NULL;
66 extern char *__progname;
68 static void vmsg __P((int, const char *, va_list)) __printflike(2, 0);
94 errexit(const char *fmt, ...)
108 fmt = va_arg(ap, const char *);
110 (void) vfprintf(stderr, fmt, ap);
122 (void) printf("%s: ", dev);
124 (void) vprintf(fmt, ap);
129 if (fatal && preen) {
131 "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
140 pfatal(const char *fmt, ...)
154 fmt = va_arg(ap, const char *);
163 pwarn(const char *fmt, ...)
176 fmt = va_arg(ap, const char *);
186 pfatal("%s (%s)", s, strerror(errno));
191 panic(const char *fmt, ...)
205 fmt = va_arg(ap, const char *);
216 static char unrawbuf[32];
220 if ((dp = strrchr(name, '/')) == 0)
222 if (stat(name, &stb) < 0)
224 if (!S_ISCHR(stb.st_mode))
228 (void)snprintf(unrawbuf, 32, "%.*s/%s", (int)(dp - name), name, dp + 2);
236 static char rawbuf[32];
239 if ((dp = strrchr(name, '/')) == 0)
241 (void)snprintf(rawbuf, 32, "%.*s/r%s", (int)(dp - name), name, dp + 1);
247 const char *origname;
249 struct stat stslash, stchar;
251 if (stat("/", &stslash) < 0) {
253 printf("Can't stat root\n");
256 if (stat(origname, &stchar) < 0) {
258 printf("Can't stat %s\n", origname);
261 if (!S_ISCHR(stchar.st_mode)) {
263 printf("%s is not a char device\n", origname);
269 * Get the mount point information for name.
275 struct stat devstat, mntdevstat;
276 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
278 struct statfs *mntbuf, *statfsp;
279 int i, mntsize, isdev;
281 if (stat(name, &devstat) != 0)
283 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
287 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
288 for (i = 0; i < mntsize; i++) {
289 statfsp = &mntbuf[i];
290 devname = statfsp->f_mntfromname;
291 if (*devname != '/') {
292 strcpy(device, _PATH_DEV);
293 strcat(device, devname);
294 strcpy(statfsp->f_mntfromname, device);
297 if (strcmp(name, statfsp->f_mntonname))
301 if (stat(devname, &mntdevstat) == 0 &&
302 mntdevstat.st_rdev == devstat.st_rdev)
311 * XXX this code is from NetBSD, but fails in FreeBSD because we
312 * don't have blockdevs. I don't think its needed.
316 const char *origname;
318 struct stat stslash, stblock, stchar;
319 const char *newname, *raw;
324 if (stat("/", &stslash) < 0) {
326 printf("Can't stat root\n");
331 if (stat(newname, &stblock) < 0) {
333 printf("Can't stat %s\n", newname);
336 if (S_ISBLK(stblock.st_mode)) {
337 if (stslash.st_dev == stblock.st_rdev)
339 raw = rawname(newname);
340 if (stat(raw, &stchar) < 0) {
342 printf("Can't stat %s\n", raw);
345 if (S_ISCHR(stchar.st_mode)) {
348 printf("%s is not a character device\n", raw);
351 } else if (S_ISCHR(stblock.st_mode) && !retried) {
352 newname = unrawname(newname);
355 } else if ((fsp = getfsfile(newname)) != 0 && !retried) {
356 newname = fsp->fs_spec;
361 * Not a block or character device, just return name and
362 * let the user decide whether to use it.
377 err(1, "malloc failed");
391 err(1, "realloc failed");
404 err(1, "strdup failed");