xref: /freebsd/sbin/fsck_ffs/utilities.c (revision 15fca934f63619777be51e7b38a7f55dc69294d1)
18fae3551SRodney W. Grimes /*
28fae3551SRodney W. Grimes  * Copyright (c) 1980, 1986, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
68fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
78fae3551SRodney W. Grimes  * are met:
88fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
98fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
108fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
128fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
138fae3551SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
148fae3551SRodney W. Grimes  *    must display the following acknowledgement:
158fae3551SRodney W. Grimes  *	This product includes software developed by the University of
168fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
178fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
188fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
198fae3551SRodney W. Grimes  *    without specific prior written permission.
208fae3551SRodney W. Grimes  *
218fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318fae3551SRodney W. Grimes  * SUCH DAMAGE.
328fae3551SRodney W. Grimes  */
338fae3551SRodney W. Grimes 
348fae3551SRodney W. Grimes #ifndef lint
356b100474SJulian Elischer #if 0
36780a5c1eSPeter Wemm static const char sccsid[] = "@(#)utilities.c	8.6 (Berkeley) 5/19/95";
376b100474SJulian Elischer #endif
386b100474SJulian Elischer static const char rcsid[] =
397f3dea24SPeter Wemm   "$FreeBSD$";
408fae3551SRodney W. Grimes #endif /* not lint */
418fae3551SRodney W. Grimes 
428fae3551SRodney W. Grimes #include <sys/param.h>
439ea6f4f0SAdrian Chadd #include <sys/types.h>
449ea6f4f0SAdrian Chadd #include <sys/stat.h>
45780a5c1eSPeter Wemm 
468fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
478fae3551SRodney W. Grimes #include <ufs/ufs/dir.h>
488fae3551SRodney W. Grimes #include <ufs/ffs/fs.h>
4951a5cf90SBruce Evans 
50780a5c1eSPeter Wemm #include <err.h>
519ea6f4f0SAdrian Chadd #include <errno.h>
5251a5cf90SBruce Evans #include <string.h>
539ea6f4f0SAdrian Chadd #include <ctype.h>
549ea6f4f0SAdrian Chadd #include <fstab.h>
5515fca934SKirk McKusick #include <paths.h>
569ea6f4f0SAdrian Chadd #include <stdio.h>
579ea6f4f0SAdrian Chadd #include <stdlib.h>
589ea6f4f0SAdrian Chadd #include <unistd.h>
59780a5c1eSPeter Wemm 
608fae3551SRodney W. Grimes #include "fsck.h"
618fae3551SRodney W. Grimes 
629ea6f4f0SAdrian Chadd 
639ea6f4f0SAdrian Chadd char *
649ea6f4f0SAdrian Chadd blockcheck(origname)
659ea6f4f0SAdrian Chadd 	char *origname;
669ea6f4f0SAdrian Chadd {
679ea6f4f0SAdrian Chadd 	struct stat stslash, stblock, stchar;
6815fca934SKirk McKusick 	char *newname, *raw, *cp;
699ea6f4f0SAdrian Chadd 	struct fstab *fsinfo;
709ea6f4f0SAdrian Chadd 	int retried = 0, len;
7115fca934SKirk McKusick 	static char device[MAXPATHLEN];
729ea6f4f0SAdrian Chadd 
739ea6f4f0SAdrian Chadd 	newname = origname;
7415fca934SKirk McKusick 	if (stat(newname, &stblock) < 0) {
7515fca934SKirk McKusick 		cp = strrchr(newname, '/');
7615fca934SKirk McKusick 		if (cp == 0) {
7715fca934SKirk McKusick 			(void)snprintf(device, sizeof(device), "%s%s",
7815fca934SKirk McKusick 				_PATH_DEV, newname);
7915fca934SKirk McKusick 			newname = device;
8015fca934SKirk McKusick 		}
8115fca934SKirk McKusick 	}
829ea6f4f0SAdrian Chadd retry:
839ea6f4f0SAdrian Chadd 	if (stat(newname, &stblock) < 0) {
849ea6f4f0SAdrian Chadd 		printf("Can't stat %s: %s\n", newname, strerror(errno));
859ea6f4f0SAdrian Chadd 		return (origname);
869ea6f4f0SAdrian Chadd 	}
879ea6f4f0SAdrian Chadd 	switch(stblock.st_mode & S_IFMT) {
889ea6f4f0SAdrian Chadd 	case S_IFCHR:
899ea6f4f0SAdrian Chadd 	case S_IFBLK:
909ea6f4f0SAdrian Chadd 		return(newname);
919ea6f4f0SAdrian Chadd 	case S_IFDIR:
929ea6f4f0SAdrian Chadd 		if (retried)
939ea6f4f0SAdrian Chadd 			break;
949ea6f4f0SAdrian Chadd 
959ea6f4f0SAdrian Chadd 		len = strlen(origname) - 1;
969ea6f4f0SAdrian Chadd 		if (len > 0 && origname[len] == '/')
979ea6f4f0SAdrian Chadd 			/* remove trailing slash */
989ea6f4f0SAdrian Chadd 			origname[len] = '\0';
999ea6f4f0SAdrian Chadd 		if ((fsinfo = getfsfile(origname)) == NULL) {
10070d8bdefSPoul-Henning Kamp 			printf(
10170d8bdefSPoul-Henning Kamp 			    "Can't resolve %s to character special device.\n",
1029ea6f4f0SAdrian Chadd 			    origname);
10315fca934SKirk McKusick 			return (origname);
1049ea6f4f0SAdrian Chadd 		}
1059ea6f4f0SAdrian Chadd 		newname = fsinfo->fs_spec;
1069ea6f4f0SAdrian Chadd 		retried++;
1079ea6f4f0SAdrian Chadd 		goto retry;
1089ea6f4f0SAdrian Chadd 	}
1099ea6f4f0SAdrian Chadd 	/*
1109ea6f4f0SAdrian Chadd 	 * Not a block or character device, just return name and
1119ea6f4f0SAdrian Chadd 	 * let the user decide whether to use it.
1129ea6f4f0SAdrian Chadd 	 */
1139ea6f4f0SAdrian Chadd 	return (origname);
1149ea6f4f0SAdrian Chadd }
1156db798caSIan Dowse 
1166db798caSIan Dowse void
1176db798caSIan Dowse infohandler(sig)
1186db798caSIan Dowse 	int sig;
1196db798caSIan Dowse {
1206db798caSIan Dowse 	got_siginfo = 1;
1216db798caSIan Dowse }
122