Home | History | Annotate | Line # | Download | only in fsck
fsutil.c revision 1.10
      1 /*	$NetBSD: fsutil.c,v 1.10 2001/06/18 02:31:09 lukem 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. 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.
     22  *
     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
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __RCSID("$NetBSD: fsutil.c,v 1.10 2001/06/18 02:31:09 lukem Exp $");
     39 #endif /* not lint */
     40 
     41 #include <stdio.h>
     42 #include <string.h>
     43 #include <stdlib.h>
     44 #include <stdarg.h>
     45 #include <errno.h>
     46 #include <fstab.h>
     47 #include <err.h>
     48 
     49 #include <sys/types.h>
     50 #include <sys/stat.h>
     51 
     52 #include "fsutil.h"
     53 
     54 static const char *dev = NULL;
     55 static int hot = 0;
     56 static int preen = 0;
     57 
     58 static void vmsg(int, const char *, va_list)
     59      __attribute((__format__(__printf__,2,0)));
     60 
     61 void
     62 setcdevname(const char *cd, int pr)
     63 {
     64 
     65 	dev = cd;
     66 	preen = pr;
     67 }
     68 
     69 const char *
     70 cdevname(void)
     71 {
     72 
     73 	return dev;
     74 }
     75 
     76 int
     77 hotroot(void)
     78 {
     79 
     80 	return hot;
     81 }
     82 
     83 /*VARARGS*/
     84 void
     85 errexit(const char *fmt, ...)
     86 {
     87 	va_list ap;
     88 
     89 	va_start(ap, fmt);
     90 	(void) vfprintf(stderr, fmt, ap);
     91 	va_end(ap);
     92 	exit(8);
     93 }
     94 
     95 static void
     96 vmsg(int fatal, const char *fmt, va_list ap)
     97 {
     98 
     99 	if (!fatal && preen)
    100 		(void) printf("%s: ", dev);
    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[32];
    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, 32, "%.*s/%s", (int)(dp - name), name, dp + 2);
    171 	return (unrawbuf);
    172 }
    173 
    174 const char *
    175 rawname(const char *name)
    176 {
    177 	static char rawbuf[32];
    178 	const char *dp;
    179 
    180 	if ((dp = strrchr(name, '/')) == 0)
    181 		return (0);
    182 	(void)snprintf(rawbuf, 32, "%.*s/r%s", (int)(dp - name), name, dp + 1);
    183 	return (rawbuf);
    184 }
    185 
    186 const char *
    187 blockcheck(const char *origname)
    188 {
    189 	struct stat stslash, stblock, stchar;
    190 	const char *newname, *raw;
    191 	struct fstab *fsp;
    192 	int retried = 0;
    193 
    194 	hot = 0;
    195 	if (stat("/", &stslash) < 0) {
    196 		perror("/");
    197 		printf("Can't stat root\n");
    198 		return (origname);
    199 	}
    200 	newname = origname;
    201 retry:
    202 	if (stat(newname, &stblock) < 0) {
    203 		perror(newname);
    204 		printf("Can't stat %s\n", newname);
    205 		return (origname);
    206 	}
    207 	if (S_ISBLK(stblock.st_mode)) {
    208 		if (stslash.st_dev == stblock.st_rdev)
    209 			hot++;
    210 		raw = rawname(newname);
    211 		if (stat(raw, &stchar) < 0) {
    212 			perror(raw);
    213 			printf("Can't stat %s\n", raw);
    214 			return (origname);
    215 		}
    216 		if (S_ISCHR(stchar.st_mode)) {
    217 			return (raw);
    218 		} else {
    219 			printf("%s is not a character device\n", raw);
    220 			return (origname);
    221 		}
    222 	} else if (S_ISCHR(stblock.st_mode) && !retried) {
    223 		newname = unrawname(newname);
    224 		retried++;
    225 		goto retry;
    226 	} else if ((fsp = getfsfile(newname)) != 0 && !retried) {
    227 		newname = fsp->fs_spec;
    228 		retried++;
    229 		goto retry;
    230 	}
    231 	/*
    232 	 * Not a block or character device, just return name and
    233 	 * let the user decide whether to use it.
    234 	 */
    235 	return (origname);
    236 }
    237 
    238 
    239 void *
    240 emalloc(size_t s)
    241 {
    242 	void *p;
    243 
    244 	p = malloc(s);
    245 	if (p == NULL)
    246 		err(1, "malloc failed");
    247 	return (p);
    248 }
    249 
    250 
    251 void *
    252 erealloc(void *p, size_t s)
    253 {
    254 	void *q;
    255 
    256 	q = realloc(p, s);
    257 	if (q == NULL)
    258 		err(1, "realloc failed");
    259 	return (q);
    260 }
    261 
    262 
    263 char *
    264 estrdup(const char *s)
    265 {
    266 	char *p;
    267 
    268 	p = strdup(s);
    269 	if (p == NULL)
    270 		err(1, "strdup failed");
    271 	return (p);
    272 }
    273