1 /* $NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 1990, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $");
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
42 #include <sys/mount.h>
55 static const char *dev = NULL;
58 static void vmsg(int, const char *, va_list) __printflike(2, 0);
61 setcdevname(const char *cd, int pr)
74 vmsg(int fatal, const char *fmt, va_list ap)
77 (void) printf("%s: ", dev);
79 (void) vprintf(fmt, ap);
86 "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
94 pfatal(const char *fmt, ...)
105 pwarn(const char *fmt, ...)
115 perr(const char *fmt, ...)
125 panic(const char *fmt, ...)
136 devcheck(const char *origname)
138 struct stat stslash, stchar;
140 if (stat("/", &stslash) < 0) {
141 perr("Can't stat `/'");
144 if (stat(origname, &stchar) < 0) {
145 perr("Can't stat %s\n", origname);
148 if (!S_ISCHR(stchar.st_mode)) {
149 perr("%s is not a char device\n", origname);
155 * Get the mount point information for name.
158 getmntpt(const char *name)
160 struct stat devstat, mntdevstat;
161 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
163 struct statfs *mntbuf, *statfsp;
164 int i, mntsize, isdev;
166 if (stat(name, &devstat) != 0)
168 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
172 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
173 for (i = 0; i < mntsize; i++) {
174 statfsp = &mntbuf[i];
175 dev_name = statfsp->f_mntfromname;
176 if (*dev_name != '/') {
177 if (strlen(_PATH_DEV) + strlen(dev_name) + 1 >
178 sizeof(statfsp->f_mntfromname))
180 strcpy(device, _PATH_DEV);
181 strcat(device, dev_name);
182 strcpy(statfsp->f_mntfromname, device);
185 if (strcmp(name, statfsp->f_mntonname))
189 if (stat(dev_name, &mntdevstat) == 0 &&
190 mntdevstat.st_rdev == devstat.st_rdev)
205 err(1, "malloc failed");
211 erealloc(void *p, size_t s)
217 err(1, "realloc failed");
223 estrdup(const char *s)
229 err(1, "strdup failed");