Home | History | Annotate | Line # | Download | only in fsck
fsutil.c revision 1.4.2.1
      1  1.4.2.1    mellon /*	$NetBSD: fsutil.c,v 1.4.2.1 1997/10/31 21:19:30 mellon Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*
      4      1.1  christos  * Copyright (c) 1990, 1993
      5      1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6      1.1  christos  *
      7      1.1  christos  * Redistribution and use in source and binary forms, with or without
      8      1.1  christos  * modification, are permitted provided that the following conditions
      9      1.1  christos  * are met:
     10      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  christos  *    documentation and/or other materials provided with the distribution.
     15      1.1  christos  * 3. All advertising materials mentioning features or use of this software
     16      1.1  christos  *    must display the following acknowledgement:
     17      1.1  christos  *	This product includes software developed by the University of
     18      1.1  christos  *	California, Berkeley and its contributors.
     19      1.1  christos  * 4. Neither the name of the University nor the names of its contributors
     20      1.1  christos  *    may be used to endorse or promote products derived from this software
     21      1.1  christos  *    without specific prior written permission.
     22      1.1  christos  *
     23      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24      1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25      1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26      1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27      1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28      1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29      1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30      1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31      1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32      1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33      1.1  christos  * SUCH DAMAGE.
     34      1.1  christos  */
     35      1.4     lukem 
     36      1.4     lukem #include <sys/cdefs.h>
     37      1.1  christos #ifndef lint
     38  1.4.2.1    mellon __RCSID("$NetBSD: fsutil.c,v 1.4.2.1 1997/10/31 21:19:30 mellon Exp $");
     39      1.1  christos #endif /* not lint */
     40      1.1  christos 
     41      1.1  christos #include <stdio.h>
     42      1.1  christos #include <string.h>
     43      1.1  christos #include <stdlib.h>
     44      1.1  christos #if __STDC__
     45      1.1  christos #include <stdarg.h>
     46      1.1  christos #else
     47      1.1  christos #include <varargs.h>
     48      1.1  christos #endif
     49      1.1  christos #include <errno.h>
     50      1.1  christos #include <fstab.h>
     51      1.1  christos #include <err.h>
     52      1.1  christos 
     53      1.1  christos #include <sys/types.h>
     54      1.1  christos #include <sys/stat.h>
     55      1.1  christos 
     56      1.1  christos #include "fsutil.h"
     57      1.1  christos 
     58      1.1  christos static const char *dev = NULL;
     59      1.1  christos static int hot = 0;
     60      1.1  christos static int preen = 0;
     61      1.1  christos 
     62      1.1  christos extern char *__progname;
     63      1.1  christos 
     64      1.1  christos static void vmsg __P((int, const char *, va_list));
     65      1.1  christos 
     66      1.1  christos void
     67      1.1  christos setcdevname(cd, pr)
     68      1.1  christos 	const char *cd;
     69      1.1  christos 	int pr;
     70      1.1  christos {
     71      1.1  christos 	dev = cd;
     72      1.1  christos 	preen = pr;
     73      1.1  christos }
     74      1.1  christos 
     75      1.1  christos const char *
     76      1.1  christos cdevname()
     77      1.1  christos {
     78      1.1  christos 	return dev;
     79      1.1  christos }
     80      1.1  christos 
     81      1.1  christos int
     82      1.1  christos hotroot()
     83      1.1  christos {
     84      1.1  christos 	return hot;
     85      1.1  christos }
     86      1.1  christos 
     87      1.1  christos /*VARARGS*/
     88      1.1  christos void
     89      1.1  christos #if __STDC__
     90      1.1  christos errexit(const char *fmt, ...)
     91      1.1  christos #else
     92      1.3  christos errexit(va_alist)
     93      1.1  christos 	va_dcl
     94      1.1  christos #endif
     95      1.1  christos {
     96      1.1  christos 	va_list ap;
     97      1.3  christos 
     98      1.1  christos #if __STDC__
     99      1.1  christos 	va_start(ap, fmt);
    100      1.1  christos #else
    101      1.3  christos 	const char *fmt;
    102      1.3  christos 
    103      1.1  christos 	va_start(ap);
    104      1.3  christos 	fmt = va_arg(ap, const char *);
    105      1.1  christos #endif
    106      1.1  christos 	(void) vfprintf(stderr, fmt, ap);
    107      1.1  christos 	va_end(ap);
    108      1.1  christos 	exit(8);
    109      1.1  christos }
    110      1.1  christos 
    111      1.1  christos static void
    112      1.1  christos vmsg(fatal, fmt, ap)
    113      1.1  christos 	int fatal;
    114      1.1  christos 	const char *fmt;
    115      1.1  christos 	va_list ap;
    116      1.1  christos {
    117      1.1  christos 	if (!fatal && preen)
    118      1.1  christos 		(void) printf("%s: ", dev);
    119      1.1  christos 
    120      1.1  christos 	(void) vprintf(fmt, ap);
    121      1.1  christos 
    122      1.1  christos 	if (fatal && preen)
    123      1.1  christos 		(void) printf("\n");
    124      1.1  christos 
    125      1.1  christos 	if (fatal && preen) {
    126      1.1  christos 		(void) printf(
    127      1.1  christos 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
    128      1.1  christos 		    dev, __progname);
    129      1.1  christos 		exit(8);
    130      1.1  christos 	}
    131      1.1  christos }
    132      1.1  christos 
    133      1.1  christos /*VARARGS*/
    134      1.1  christos void
    135      1.1  christos #if __STDC__
    136      1.1  christos pfatal(const char *fmt, ...)
    137      1.1  christos #else
    138      1.3  christos pfatal(va_alist)
    139      1.1  christos 	va_dcl
    140      1.1  christos #endif
    141      1.1  christos {
    142      1.1  christos 	va_list ap;
    143      1.3  christos 
    144      1.1  christos #if __STDC__
    145      1.1  christos 	va_start(ap, fmt);
    146      1.1  christos #else
    147      1.3  christos 	const char *fmt;
    148      1.3  christos 
    149      1.1  christos 	va_start(ap);
    150      1.3  christos 	fmt = va_arg(ap, const char *);
    151      1.1  christos #endif
    152      1.1  christos 	vmsg(1, fmt, ap);
    153      1.1  christos 	va_end(ap);
    154      1.1  christos }
    155      1.1  christos 
    156      1.1  christos /*VARARGS*/
    157      1.1  christos void
    158      1.1  christos #if __STDC__
    159      1.1  christos pwarn(const char *fmt, ...)
    160      1.1  christos #else
    161      1.3  christos pwarn(va_alist)
    162      1.1  christos 	va_dcl
    163      1.1  christos #endif
    164      1.1  christos {
    165      1.1  christos 	va_list ap;
    166      1.1  christos #if __STDC__
    167      1.1  christos 	va_start(ap, fmt);
    168      1.1  christos #else
    169      1.3  christos 	const char *fmt;
    170      1.3  christos 
    171      1.1  christos 	va_start(ap);
    172      1.3  christos 	fmt = va_arg(ap, const char *);
    173      1.1  christos #endif
    174      1.1  christos 	vmsg(0, fmt, ap);
    175      1.1  christos 	va_end(ap);
    176      1.1  christos }
    177      1.1  christos 
    178      1.1  christos void
    179      1.1  christos perror(s)
    180      1.1  christos 	const char *s;
    181      1.1  christos {
    182      1.1  christos 	pfatal("%s (%s)", s, strerror(errno));
    183      1.1  christos }
    184      1.1  christos 
    185      1.1  christos void
    186      1.1  christos #if __STDC__
    187      1.1  christos panic(const char *fmt, ...)
    188      1.1  christos #else
    189      1.3  christos panic(va_alist)
    190      1.1  christos 	va_dcl
    191      1.1  christos #endif
    192      1.1  christos {
    193      1.1  christos 	va_list ap;
    194      1.3  christos 
    195      1.1  christos #if __STDC__
    196      1.1  christos 	va_start(ap, fmt);
    197      1.1  christos #else
    198      1.3  christos 	const char *fmt;
    199      1.3  christos 
    200      1.1  christos 	va_start(ap);
    201      1.3  christos 	fmt = va_arg(ap, const char *);
    202      1.1  christos #endif
    203      1.1  christos 	vmsg(1, fmt, ap);
    204      1.1  christos 	va_end(ap);
    205      1.1  christos 	exit(8);
    206      1.1  christos }
    207      1.1  christos 
    208      1.1  christos char *
    209      1.1  christos unrawname(name)
    210      1.1  christos 	char *name;
    211      1.1  christos {
    212      1.1  christos 	char *dp;
    213      1.1  christos 	struct stat stb;
    214      1.1  christos 
    215      1.1  christos 	if ((dp = strrchr(name, '/')) == 0)
    216      1.1  christos 		return (name);
    217      1.1  christos 	if (stat(name, &stb) < 0)
    218      1.1  christos 		return (name);
    219      1.1  christos 	if (!S_ISCHR(stb.st_mode))
    220      1.1  christos 		return (name);
    221      1.1  christos 	if (dp[1] != 'r')
    222      1.1  christos 		return (name);
    223      1.1  christos 	(void)strcpy(&dp[1], &dp[2]);
    224      1.1  christos 	return (name);
    225      1.1  christos }
    226      1.1  christos 
    227      1.1  christos char *
    228      1.1  christos rawname(name)
    229      1.1  christos 	char *name;
    230      1.1  christos {
    231      1.1  christos 	static char rawbuf[32];
    232      1.1  christos 	char *dp;
    233      1.1  christos 
    234      1.1  christos 	if ((dp = strrchr(name, '/')) == 0)
    235      1.1  christos 		return (0);
    236      1.1  christos 	*dp = 0;
    237      1.1  christos 	(void)strcpy(rawbuf, name);
    238      1.1  christos 	*dp = '/';
    239      1.1  christos 	(void)strcat(rawbuf, "/r");
    240      1.1  christos 	(void)strcat(rawbuf, &dp[1]);
    241      1.1  christos 	return (rawbuf);
    242      1.1  christos }
    243      1.1  christos 
    244      1.1  christos char *
    245      1.1  christos blockcheck(origname)
    246      1.1  christos 	char *origname;
    247      1.1  christos {
    248      1.1  christos 	struct stat stslash, stblock, stchar;
    249      1.1  christos 	char *newname, *raw;
    250      1.1  christos 	struct fstab *fsp;
    251      1.1  christos 	int retried = 0;
    252      1.1  christos 
    253      1.1  christos 	hot = 0;
    254      1.1  christos 	if (stat("/", &stslash) < 0) {
    255      1.1  christos 		perror("/");
    256      1.1  christos 		printf("Can't stat root\n");
    257      1.1  christos 		return (origname);
    258      1.1  christos 	}
    259      1.1  christos 	newname = origname;
    260      1.1  christos retry:
    261      1.1  christos 	if (stat(newname, &stblock) < 0) {
    262      1.1  christos 		perror(newname);
    263      1.1  christos 		printf("Can't stat %s\n", newname);
    264      1.1  christos 		return (origname);
    265      1.1  christos 	}
    266      1.1  christos 	if (S_ISBLK(stblock.st_mode)) {
    267      1.1  christos 		if (stslash.st_dev == stblock.st_rdev)
    268      1.1  christos 			hot++;
    269      1.1  christos 		raw = rawname(newname);
    270      1.1  christos 		if (stat(raw, &stchar) < 0) {
    271      1.1  christos 			perror(raw);
    272      1.1  christos 			printf("Can't stat %s\n", raw);
    273      1.1  christos 			return (origname);
    274      1.1  christos 		}
    275      1.1  christos 		if (S_ISCHR(stchar.st_mode)) {
    276      1.1  christos 			return (raw);
    277      1.1  christos 		} else {
    278      1.1  christos 			printf("%s is not a character device\n", raw);
    279      1.1  christos 			return (origname);
    280      1.1  christos 		}
    281      1.1  christos 	} else if (S_ISCHR(stblock.st_mode) && !retried) {
    282      1.1  christos 		newname = unrawname(newname);
    283      1.1  christos 		retried++;
    284      1.1  christos 		goto retry;
    285      1.1  christos 	} else if ((fsp = getfsfile(newname)) != 0 && !retried) {
    286      1.1  christos 		newname = fsp->fs_spec;
    287      1.1  christos 		retried++;
    288      1.1  christos 		goto retry;
    289      1.1  christos 	}
    290      1.1  christos 	/*
    291      1.1  christos 	 * Not a block or character device, just return name and
    292      1.1  christos 	 * let the user decide whether to use it.
    293      1.1  christos 	 */
    294      1.1  christos 	return (origname);
    295      1.1  christos }
    296      1.1  christos 
    297      1.1  christos 
    298      1.1  christos void *
    299      1.1  christos emalloc(s)
    300      1.1  christos 	size_t s;
    301      1.1  christos {
    302  1.4.2.1    mellon 	void *p;
    303  1.4.2.1    mellon 
    304  1.4.2.1    mellon 	p = malloc(s);
    305      1.1  christos 	if (p == NULL)
    306      1.1  christos 		err(1, "malloc failed");
    307  1.4.2.1    mellon 	return (p);
    308      1.2  christos }
    309      1.2  christos 
    310      1.2  christos 
    311      1.2  christos void *
    312      1.2  christos erealloc(p, s)
    313      1.2  christos 	void *p;
    314      1.2  christos 	size_t s;
    315      1.2  christos {
    316  1.4.2.1    mellon 	void *q;
    317  1.4.2.1    mellon 
    318  1.4.2.1    mellon 	q = realloc(p, s);
    319  1.4.2.1    mellon 	if (q == NULL)
    320      1.2  christos 		err(1, "realloc failed");
    321  1.4.2.1    mellon 	return (q);
    322      1.1  christos }
    323      1.1  christos 
    324      1.1  christos 
    325      1.1  christos char *
    326      1.1  christos estrdup(s)
    327      1.1  christos 	const char *s;
    328      1.1  christos {
    329  1.4.2.1    mellon 	char *p;
    330  1.4.2.1    mellon 
    331  1.4.2.1    mellon 	p = strdup(s);
    332      1.1  christos 	if (p == NULL)
    333      1.1  christos 		err(1, "strdup failed");
    334  1.4.2.1    mellon 	return (p);
    335      1.1  christos }
    336