1331722Seadler/*
21558Srgrimes * Copyright (c) 1980, 1986, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes
30114589Sobrien#if 0
311558Srgrimes#ifndef lint
3223675Speterstatic const char sccsid[] = "@(#)utilities.c	8.6 (Berkeley) 5/19/95";
33114589Sobrien#endif /* not lint */
3441477Sjulian#endif
35114589Sobrien#include <sys/cdefs.h>
36114589Sobrien__FBSDID("$FreeBSD$");
371558Srgrimes
381558Srgrimes#include <sys/param.h>
3966861Sadrian#include <sys/types.h>
4066861Sadrian#include <sys/stat.h>
4123675Speter
421558Srgrimes#include <ufs/ufs/dinode.h>
431558Srgrimes#include <ufs/ufs/dir.h>
441558Srgrimes#include <ufs/ffs/fs.h>
4523799Sbde
4623675Speter#include <err.h>
4766861Sadrian#include <errno.h>
4823799Sbde#include <string.h>
4966861Sadrian#include <ctype.h>
5066861Sadrian#include <fstab.h>
5175927Smckusick#include <paths.h>
5266861Sadrian#include <stdio.h>
5366861Sadrian#include <stdlib.h>
5466861Sadrian#include <unistd.h>
5523675Speter
561558Srgrimes#include "fsck.h"
571558Srgrimes
581558Srgrimes
5966861Sadrianchar *
6092839Simpblockcheck(char *origname)
6166861Sadrian{
6286514Siedowse	struct stat stblock;
6386514Siedowse	char *newname, *cp;
6466861Sadrian	struct fstab *fsinfo;
6566861Sadrian	int retried = 0, len;
6675927Smckusick	static char device[MAXPATHLEN];
6766861Sadrian
6866861Sadrian	newname = origname;
6975927Smckusick	if (stat(newname, &stblock) < 0) {
7075927Smckusick		cp = strrchr(newname, '/');
71297886Spfg		if (cp == NULL) {
7275927Smckusick			(void)snprintf(device, sizeof(device), "%s%s",
7375927Smckusick				_PATH_DEV, newname);
7475927Smckusick			newname = device;
7575927Smckusick		}
7675927Smckusick	}
7766861Sadrianretry:
7866861Sadrian	if (stat(newname, &stblock) < 0) {
7966861Sadrian		printf("Can't stat %s: %s\n", newname, strerror(errno));
8066861Sadrian		return (origname);
8166861Sadrian	}
8266861Sadrian	switch(stblock.st_mode & S_IFMT) {
8366861Sadrian	case S_IFCHR:
8466861Sadrian	case S_IFBLK:
8566861Sadrian		return(newname);
8666861Sadrian	case S_IFDIR:
8766861Sadrian		if (retried)
8866861Sadrian			break;
89221110Sdes
9066861Sadrian		len = strlen(origname) - 1;
9166861Sadrian		if (len > 0 && origname[len] == '/')
9266861Sadrian			/* remove trailing slash */
9366861Sadrian			origname[len] = '\0';
9466861Sadrian		if ((fsinfo = getfsfile(origname)) == NULL) {
9570166Sphk			printf(
9670166Sphk			    "Can't resolve %s to character special device.\n",
9766861Sadrian			    origname);
9875927Smckusick			return (origname);
9966861Sadrian		}
10066861Sadrian		newname = fsinfo->fs_spec;
10166861Sadrian		retried++;
10266861Sadrian		goto retry;
10366861Sadrian	}
10466861Sadrian	/*
10566861Sadrian	 * Not a block or character device, just return name and
10666861Sadrian	 * let the user decide whether to use it.
10766861Sadrian	 */
10866861Sadrian	return (origname);
10966861Sadrian}
11070050Siedowse
111