1 /* $NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos 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. 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.
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
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $");
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
40 #include <sys/mount.h>
53 static const char *dev = NULL;
56 static void vmsg(int, const char *, va_list) __printflike(2, 0);
59 setcdevname(const char *cd, int pr)
72 vmsg(int fatal, const char *fmt, va_list ap)
75 (void) printf("%s: ", dev);
77 (void) vprintf(fmt, ap);
84 "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
92 pfatal(const char *fmt, ...)
103 pwarn(const char *fmt, ...)
113 perr(const char *fmt, ...)
123 panic(const char *fmt, ...)
134 devcheck(const char *origname)
136 struct stat stslash, stchar;
138 if (stat("/", &stslash) < 0) {
139 perr("Can't stat `/'");
142 if (stat(origname, &stchar) < 0) {
143 perr("Can't stat %s\n", origname);
146 if (!S_ISCHR(stchar.st_mode)) {
147 perr("%s is not a char device\n", origname);
153 * Get the mount point information for name.
156 getmntpt(const char *name)
158 struct stat devstat, mntdevstat;
159 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
161 struct statfs *mntbuf, *statfsp;
162 int i, mntsize, isdev;
164 if (stat(name, &devstat) != 0)
166 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
170 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
171 for (i = 0; i < mntsize; i++) {
172 statfsp = &mntbuf[i];
173 dev_name = statfsp->f_mntfromname;
174 if (*dev_name != '/') {
175 strcpy(device, _PATH_DEV);
176 strcat(device, dev_name);
177 strcpy(statfsp->f_mntfromname, device);
180 if (strcmp(name, statfsp->f_mntonname))
184 if (stat(dev_name, &mntdevstat) == 0 &&
185 mntdevstat.st_rdev == devstat.st_rdev)
200 err(1, "malloc failed");
206 erealloc(void *p, size_t s)
212 err(1, "realloc failed");
218 estrdup(const char *s)
224 err(1, "strdup failed");