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. 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
31 * $FreeBSD: src/sbin/fsck/fsutil.c,v 1.2.2.1 2001/08/01 05:47:55 obrien Exp $
43 #include <sys/param.h>
45 #include <sys/mount.h>
49 static const char *dev = NULL;
53 extern char *__progname;
55 static void vmsg(int, const char *, va_list) __printflike(2, 0);
58 setcdevname(const char *cd, int pr)
78 errexit(const char *fmt, ...)
83 vfprintf(stderr, fmt, ap);
89 vmsg(int fatal, const char *fmt, va_list ap)
101 "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
109 pfatal(const char *fmt, ...)
120 pwarn(const char *fmt, ...)
129 perr(const char *fmt, ...)
139 perror(const char *s)
141 pfatal("%s (%s)", s, strerror(errno));
145 panic(const char *fmt, ...)
156 unrawname(const char *name)
158 static char unrawbuf[32];
162 if ((dp = strrchr(name, '/')) == NULL)
164 if (stat(name, &stb) < 0)
166 if (!S_ISCHR(stb.st_mode))
170 snprintf(unrawbuf, 32, "%.*s/%s", (int)(dp - name), name, dp + 2);
175 rawname(const char *name)
177 static char rawbuf[32];
180 if ((dp = strrchr(name, '/')) == NULL)
182 snprintf(rawbuf, 32, "%.*s/r%s", (int)(dp - name), name, dp + 1);
187 devcheck(const char *origname)
189 struct stat stslash, stchar;
191 if (stat("/", &stslash) < 0) {
193 printf("Can't stat root\n");
196 if (stat(origname, &stchar) < 0) {
198 printf("Can't stat %s\n", origname);
201 if (!S_ISCHR(stchar.st_mode)) {
203 printf("%s is not a char device\n", origname);
209 * Get the mount point information for name.
212 getmntpt(const char *name)
214 struct stat devstat, mntdevstat;
215 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
217 struct statfs *mntbuf, *statfsp;
218 int i, mntsize, isdev;
220 if (stat(name, &devstat) != 0)
222 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
226 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
227 for (i = 0; i < mntsize; i++) {
228 statfsp = &mntbuf[i];
229 devname = statfsp->f_mntfromname;
230 if (*devname != '/') {
231 strcpy(device, _PATH_DEV);
232 strcat(device, devname);
233 strcpy(statfsp->f_mntfromname, device);
236 if (strcmp(name, statfsp->f_mntonname))
240 if (stat(devname, &mntdevstat) == 0 &&
241 mntdevstat.st_rdev == devstat.st_rdev)
250 * XXX this code is from NetBSD, but fails in FreeBSD because we
251 * don't have blockdevs. I don't think its needed.
254 blockcheck(const char *origname)
256 struct stat stslash, stblock, stchar;
257 const char *newname, *raw;
262 if (stat("/", &stslash) < 0) {
264 printf("Can't stat root\n");
269 if (stat(newname, &stblock) < 0) {
271 printf("Can't stat %s\n", newname);
274 if (S_ISBLK(stblock.st_mode)) {
275 if (stslash.st_dev == stblock.st_rdev)
277 raw = rawname(newname);
278 if (stat(raw, &stchar) < 0) {
280 printf("Can't stat %s\n", raw);
283 if (S_ISCHR(stchar.st_mode)) {
286 printf("%s is not a character device\n", raw);
289 } else if (S_ISCHR(stblock.st_mode) && !retried) {
290 newname = unrawname(newname);
293 } else if ((fsp = getfsfile(newname)) != 0 && !retried) {
294 newname = fsp->fs_spec;
299 * Not a block or character device, just return name and
300 * let the user decide whether to use it.