Home | History | Annotate | Line # | Download | only in fsck
fsutil.c revision 1.1
      1 /*	$NetBSD: fsutil.c,v 1.1 1996/09/27 22:38:41 christos 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 #ifndef lint
     36 static char rcsid[] = "$NetBSD: fsutil.c,v 1.1 1996/09/27 22:38:41 christos Exp $";
     37 #endif /* not lint */
     38 
     39 #include <stdio.h>
     40 #include <string.h>
     41 #include <stdlib.h>
     42 #if __STDC__
     43 #include <stdarg.h>
     44 #else
     45 #include <varargs.h>
     46 #endif
     47 #include <errno.h>
     48 #include <fstab.h>
     49 #include <err.h>
     50 
     51 #include <sys/types.h>
     52 #include <sys/stat.h>
     53 
     54 #include "fsutil.h"
     55 
     56 static const char *dev = NULL;
     57 static int hot = 0;
     58 static int preen = 0;
     59 
     60 extern char *__progname;
     61 
     62 static void vmsg __P((int, const char *, va_list));
     63 
     64 void
     65 setcdevname(cd, pr)
     66 	const char *cd;
     67 	int pr;
     68 {
     69 	dev = cd;
     70 	preen = pr;
     71 }
     72 
     73 const char *
     74 cdevname()
     75 {
     76 	return dev;
     77 }
     78 
     79 int
     80 hotroot()
     81 {
     82 	return hot;
     83 }
     84 
     85 /*VARARGS*/
     86 void
     87 #if __STDC__
     88 errexit(const char *fmt, ...)
     89 #else
     90 errexit(fmt, va_alist)
     91 	char *fmt;
     92 	va_dcl
     93 #endif
     94 {
     95 	va_list ap;
     96 
     97 #if __STDC__
     98 	va_start(ap, fmt);
     99 #else
    100 	va_start(ap);
    101 #endif
    102 	(void) vfprintf(stderr, fmt, ap);
    103 	va_end(ap);
    104 	exit(8);
    105 }
    106 
    107 static void
    108 vmsg(fatal, fmt, ap)
    109 	int fatal;
    110 	const char *fmt;
    111 	va_list ap;
    112 {
    113 	if (!fatal && preen)
    114 		(void) printf("%s: ", dev);
    115 
    116 	(void) vprintf(fmt, ap);
    117 
    118 	if (fatal && preen)
    119 		(void) printf("\n");
    120 
    121 	if (fatal && preen) {
    122 		(void) printf(
    123 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
    124 		    dev, __progname);
    125 		exit(8);
    126 	}
    127 }
    128 
    129 /*VARARGS*/
    130 void
    131 #if __STDC__
    132 pfatal(const char *fmt, ...)
    133 #else
    134 pfatal(fmt, va_alist)
    135 	char *fmt;
    136 	va_dcl
    137 #endif
    138 {
    139 	va_list ap;
    140 
    141 #if __STDC__
    142 	va_start(ap, fmt);
    143 #else
    144 	va_start(ap);
    145 #endif
    146 	vmsg(1, fmt, ap);
    147 	va_end(ap);
    148 }
    149 
    150 /*VARARGS*/
    151 void
    152 #if __STDC__
    153 pwarn(const char *fmt, ...)
    154 #else
    155 pwarn(fmt, va_alist)
    156 	char *fmt;
    157 	va_dcl
    158 #endif
    159 {
    160 	va_list ap;
    161 
    162 #if __STDC__
    163 	va_start(ap, fmt);
    164 #else
    165 	va_start(ap);
    166 #endif
    167 	vmsg(0, fmt, ap);
    168 	va_end(ap);
    169 }
    170 
    171 void
    172 perror(s)
    173 	const char *s;
    174 {
    175 	pfatal("%s (%s)", s, strerror(errno));
    176 }
    177 
    178 void
    179 #if __STDC__
    180 panic(const char *fmt, ...)
    181 #else
    182 panic(fmt, va_alist)
    183 	char *fmt;
    184 	va_dcl
    185 #endif
    186 {
    187 	va_list ap;
    188 
    189 #if __STDC__
    190 	va_start(ap, fmt);
    191 #else
    192 	va_start(ap);
    193 #endif
    194 	vmsg(1, fmt, ap);
    195 	va_end(ap);
    196 	exit(8);
    197 }
    198 
    199 char *
    200 unrawname(name)
    201 	char *name;
    202 {
    203 	char *dp;
    204 	struct stat stb;
    205 
    206 	if ((dp = strrchr(name, '/')) == 0)
    207 		return (name);
    208 	if (stat(name, &stb) < 0)
    209 		return (name);
    210 	if (!S_ISCHR(stb.st_mode))
    211 		return (name);
    212 	if (dp[1] != 'r')
    213 		return (name);
    214 	(void)strcpy(&dp[1], &dp[2]);
    215 	return (name);
    216 }
    217 
    218 char *
    219 rawname(name)
    220 	char *name;
    221 {
    222 	static char rawbuf[32];
    223 	char *dp;
    224 
    225 	if ((dp = strrchr(name, '/')) == 0)
    226 		return (0);
    227 	*dp = 0;
    228 	(void)strcpy(rawbuf, name);
    229 	*dp = '/';
    230 	(void)strcat(rawbuf, "/r");
    231 	(void)strcat(rawbuf, &dp[1]);
    232 	return (rawbuf);
    233 }
    234 
    235 char *
    236 blockcheck(origname)
    237 	char *origname;
    238 {
    239 	struct stat stslash, stblock, stchar;
    240 	char *newname, *raw;
    241 	struct fstab *fsp;
    242 	int retried = 0;
    243 
    244 	hot = 0;
    245 	if (stat("/", &stslash) < 0) {
    246 		perror("/");
    247 		printf("Can't stat root\n");
    248 		return (origname);
    249 	}
    250 	newname = origname;
    251 retry:
    252 	if (stat(newname, &stblock) < 0) {
    253 		perror(newname);
    254 		printf("Can't stat %s\n", newname);
    255 		return (origname);
    256 	}
    257 	if (S_ISBLK(stblock.st_mode)) {
    258 		if (stslash.st_dev == stblock.st_rdev)
    259 			hot++;
    260 		raw = rawname(newname);
    261 		if (stat(raw, &stchar) < 0) {
    262 			perror(raw);
    263 			printf("Can't stat %s\n", raw);
    264 			return (origname);
    265 		}
    266 		if (S_ISCHR(stchar.st_mode)) {
    267 			return (raw);
    268 		} else {
    269 			printf("%s is not a character device\n", raw);
    270 			return (origname);
    271 		}
    272 	} else if (S_ISCHR(stblock.st_mode) && !retried) {
    273 		newname = unrawname(newname);
    274 		retried++;
    275 		goto retry;
    276 	} else if ((fsp = getfsfile(newname)) != 0 && !retried) {
    277 		newname = fsp->fs_spec;
    278 		retried++;
    279 		goto retry;
    280 	}
    281 	/*
    282 	 * Not a block or character device, just return name and
    283 	 * let the user decide whether to use it.
    284 	 */
    285 	return (origname);
    286 }
    287 
    288 
    289 void *
    290 emalloc(s)
    291 	size_t s;
    292 {
    293 	void *p = malloc(s);
    294 	if (p == NULL)
    295 		err(1, "malloc failed");
    296 	return p;
    297 }
    298 
    299 
    300 char *
    301 estrdup(s)
    302 	const char *s;
    303 {
    304 	char *p = strdup(s);
    305 	if (p == NULL)
    306 		err(1, "strdup failed");
    307 	return p;
    308 }
    309