utilities.c revision 126345
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
34114589Sobrien#if 0
351558Srgrimes#ifndef lint
3623675Speterstatic const char sccsid[] = "@(#)utilities.c	8.6 (Berkeley) 5/19/95";
37114589Sobrien#endif /* not lint */
3841477Sjulian#endif
39114589Sobrien#include <sys/cdefs.h>
40114589Sobrien__FBSDID("$FreeBSD: head/sbin/fsck_ffs/utilities.c 126345 2004-02-28 07:50:42Z scottl $");
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 *
6492839Simpblockcheck(char *origname)
6566861Sadrian{
6686514Siedowse	struct stat stblock;
6786514Siedowse	char *newname, *cp;
6866861Sadrian	struct fstab *fsinfo;
6966861Sadrian	int retried = 0, len;
7075927Smckusick	static char device[MAXPATHLEN];
7166861Sadrian
7266861Sadrian	newname = origname;
7375927Smckusick	if (stat(newname, &stblock) < 0) {
7475927Smckusick		cp = strrchr(newname, '/');
7575927Smckusick		if (cp == 0) {
7675927Smckusick			(void)snprintf(device, sizeof(device), "%s%s",
7775927Smckusick				_PATH_DEV, newname);
7875927Smckusick			newname = device;
7975927Smckusick		}
8075927Smckusick	}
8166861Sadrianretry:
8266861Sadrian	if (stat(newname, &stblock) < 0) {
8366861Sadrian		printf("Can't stat %s: %s\n", newname, strerror(errno));
8466861Sadrian		return (origname);
8566861Sadrian	}
8666861Sadrian	switch(stblock.st_mode & S_IFMT) {
8766861Sadrian	case S_IFCHR:
8866861Sadrian	case S_IFBLK:
8966861Sadrian		return(newname);
9066861Sadrian	case S_IFDIR:
9166861Sadrian		if (retried)
9266861Sadrian			break;
9366861Sadrian
9466861Sadrian		len = strlen(origname) - 1;
9566861Sadrian		if (len > 0 && origname[len] == '/')
9666861Sadrian			/* remove trailing slash */
9766861Sadrian			origname[len] = '\0';
9866861Sadrian		if ((fsinfo = getfsfile(origname)) == NULL) {
9970166Sphk			printf(
10070166Sphk			    "Can't resolve %s to character special device.\n",
10166861Sadrian			    origname);
10275927Smckusick			return (origname);
10366861Sadrian		}
10466861Sadrian		newname = fsinfo->fs_spec;
10566861Sadrian		retried++;
10666861Sadrian		goto retry;
10766861Sadrian	}
10866861Sadrian	/*
10966861Sadrian	 * Not a block or character device, just return name and
11066861Sadrian	 * let the user decide whether to use it.
11166861Sadrian	 */
11266861Sadrian	return (origname);
11366861Sadrian}
11470050Siedowse
11570050Siedowsevoid
116100935Sphkinfohandler(int sig __unused)
11770050Siedowse{
11870050Siedowse	got_siginfo = 1;
11970050Siedowse}
120126345Sscottl
121126345Sscottlvoid
122126345Sscottlalarmhandler(int sig __unused)
123126345Sscottl{
124126345Sscottl	got_sigalarm = 1;
125126345Sscottl}
126