utilities.c revision 86514
11558Srgrimes/*
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 * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
3541477Sjulian#if 0
3623675Speterstatic const char sccsid[] = "@(#)utilities.c	8.6 (Berkeley) 5/19/95";
3741477Sjulian#endif
3841477Sjulianstatic const char rcsid[] =
3950476Speter  "$FreeBSD: head/sbin/fsck_ffs/utilities.c 86514 2001-11-17 23:48:21Z iedowse $";
401558Srgrimes#endif /* not lint */
411558Srgrimes
421558Srgrimes#include <sys/param.h>
4366861Sadrian#include <sys/types.h>
4466861Sadrian#include <sys/stat.h>
4523675Speter
461558Srgrimes#include <ufs/ufs/dinode.h>
471558Srgrimes#include <ufs/ufs/dir.h>
481558Srgrimes#include <ufs/ffs/fs.h>
4923799Sbde
5023675Speter#include <err.h>
5166861Sadrian#include <errno.h>
5223799Sbde#include <string.h>
5366861Sadrian#include <ctype.h>
5466861Sadrian#include <fstab.h>
5575927Smckusick#include <paths.h>
5666861Sadrian#include <stdio.h>
5766861Sadrian#include <stdlib.h>
5866861Sadrian#include <unistd.h>
5923675Speter
601558Srgrimes#include "fsck.h"
611558Srgrimes
621558Srgrimes
6366861Sadrianchar *
6466861Sadrianblockcheck(origname)
6566861Sadrian	char *origname;
6666861Sadrian{
6786514Siedowse	struct stat stblock;
6886514Siedowse	char *newname, *cp;
6966861Sadrian	struct fstab *fsinfo;
7066861Sadrian	int retried = 0, len;
7175927Smckusick	static char device[MAXPATHLEN];
7266861Sadrian
7366861Sadrian	newname = origname;
7475927Smckusick	if (stat(newname, &stblock) < 0) {
7575927Smckusick		cp = strrchr(newname, '/');
7675927Smckusick		if (cp == 0) {
7775927Smckusick			(void)snprintf(device, sizeof(device), "%s%s",
7875927Smckusick				_PATH_DEV, newname);
7975927Smckusick			newname = device;
8075927Smckusick		}
8175927Smckusick	}
8266861Sadrianretry:
8366861Sadrian	if (stat(newname, &stblock) < 0) {
8466861Sadrian		printf("Can't stat %s: %s\n", newname, strerror(errno));
8566861Sadrian		return (origname);
8666861Sadrian	}
8766861Sadrian	switch(stblock.st_mode & S_IFMT) {
8866861Sadrian	case S_IFCHR:
8966861Sadrian	case S_IFBLK:
9066861Sadrian		return(newname);
9166861Sadrian	case S_IFDIR:
9266861Sadrian		if (retried)
9366861Sadrian			break;
9466861Sadrian
9566861Sadrian		len = strlen(origname) - 1;
9666861Sadrian		if (len > 0 && origname[len] == '/')
9766861Sadrian			/* remove trailing slash */
9866861Sadrian			origname[len] = '\0';
9966861Sadrian		if ((fsinfo = getfsfile(origname)) == NULL) {
10070166Sphk			printf(
10170166Sphk			    "Can't resolve %s to character special device.\n",
10266861Sadrian			    origname);
10375927Smckusick			return (origname);
10466861Sadrian		}
10566861Sadrian		newname = fsinfo->fs_spec;
10666861Sadrian		retried++;
10766861Sadrian		goto retry;
10866861Sadrian	}
10966861Sadrian	/*
11066861Sadrian	 * Not a block or character device, just return name and
11166861Sadrian	 * let the user decide whether to use it.
11266861Sadrian	 */
11366861Sadrian	return (origname);
11466861Sadrian}
11570050Siedowse
11670050Siedowsevoid
11770050Siedowseinfohandler(sig)
11870050Siedowse	int sig;
11970050Siedowse{
12070050Siedowse	got_siginfo = 1;
12170050Siedowse}
122