utilities.c revision 70166
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 70166 2000-12-18 21:14:25Z phk $";
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>
5566861Sadrian#include <stdio.h>
5666861Sadrian#include <stdlib.h>
5766861Sadrian#include <unistd.h>
5823675Speter
591558Srgrimes#include "fsck.h"
601558Srgrimes
611558Srgrimes
6266861Sadrianchar *
6366861Sadrianblockcheck(origname)
6466861Sadrian	char *origname;
6566861Sadrian{
6666861Sadrian	struct stat stslash, stblock, stchar;
6766861Sadrian	char *newname, *raw;
6866861Sadrian	struct fstab *fsinfo;
6966861Sadrian	int retried = 0, len;
7066861Sadrian
7166861Sadrian	newname = origname;
7266861Sadrianretry:
7366861Sadrian	if (stat(newname, &stblock) < 0) {
7466861Sadrian		printf("Can't stat %s: %s\n", newname, strerror(errno));
7566861Sadrian		return (origname);
7666861Sadrian	}
7766861Sadrian	switch(stblock.st_mode & S_IFMT) {
7866861Sadrian	case S_IFCHR:
7966861Sadrian	case S_IFBLK:
8066861Sadrian		return(newname);
8166861Sadrian	case S_IFDIR:
8266861Sadrian		if (retried)
8366861Sadrian			break;
8466861Sadrian
8566861Sadrian		len = strlen(origname) - 1;
8666861Sadrian		if (len > 0 && origname[len] == '/')
8766861Sadrian			/* remove trailing slash */
8866861Sadrian			origname[len] = '\0';
8966861Sadrian		if ((fsinfo = getfsfile(origname)) == NULL) {
9070166Sphk			printf(
9170166Sphk			    "Can't resolve %s to character special device.\n",
9266861Sadrian			    origname);
9366861Sadrian			return (0);
9466861Sadrian		}
9566861Sadrian		newname = fsinfo->fs_spec;
9666861Sadrian		retried++;
9766861Sadrian		goto retry;
9866861Sadrian	}
9966861Sadrian	/*
10066861Sadrian	 * Not a block or character device, just return name and
10166861Sadrian	 * let the user decide whether to use it.
10266861Sadrian	 */
10366861Sadrian	return (origname);
10466861Sadrian}
10570050Siedowse
10670050Siedowsevoid
10770050Siedowseinfohandler(sig)
10870050Siedowse	int sig;
10970050Siedowse{
11070050Siedowse	got_siginfo = 1;
11170050Siedowse}
112