utilities.c revision 75927
1263002Sroyger/*
2263002Sroyger * Copyright (c) 1980, 1986, 1993
3264882Sroyger *	The Regents of the University of California.  All rights reserved.
4263002Sroyger *
5263002Sroyger * Redistribution and use in source and binary forms, with or without
6263002Sroyger * modification, are permitted provided that the following conditions
7264882Sroyger * are met:
8264882Sroyger * 1. Redistributions of source code must retain the above copyright
9264882Sroyger *    notice, this list of conditions and the following disclaimer.
10263002Sroyger * 2. Redistributions in binary form must reproduce the above copyright
11263002Sroyger *    notice, this list of conditions and the following disclaimer in the
12263002Sroyger *    documentation and/or other materials provided with the distribution.
13263002Sroyger * 3. All advertising materials mentioning features or use of this software
14263002Sroyger *    must display the following acknowledgement:
15263002Sroyger *	This product includes software developed by the University of
16263002Sroyger *	California, Berkeley and its contributors.
17263002Sroyger * 4. Neither the name of the University nor the names of its contributors
18263002Sroyger *    may be used to endorse or promote products derived from this software
19263002Sroyger *    without specific prior written permission.
20263002Sroyger *
21263002Sroyger * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22263002Sroyger * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23263002Sroyger * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24263002Sroyger * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25263002Sroyger * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26263002Sroyger * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27263002Sroyger * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28263002Sroyger * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29263002Sroyger * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30263002Sroyger * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31263002Sroyger * SUCH DAMAGE.
32263002Sroyger */
33263002Sroyger
34263002Sroyger#ifndef lint
35263002Sroyger#if 0
36263002Sroygerstatic const char sccsid[] = "@(#)utilities.c	8.6 (Berkeley) 5/19/95";
37263002Sroyger#endif
38263002Sroygerstatic const char rcsid[] =
39263002Sroyger  "$FreeBSD: head/sbin/fsck_ffs/utilities.c 75927 2001-04-24 22:38:08Z mckusick $";
40263002Sroyger#endif /* not lint */
41263002Sroyger
42263002Sroyger#include <sys/param.h>
43263002Sroyger#include <sys/types.h>
44263002Sroyger#include <sys/stat.h>
45263002Sroyger
46263002Sroyger#include <ufs/ufs/dinode.h>
47263002Sroyger#include <ufs/ufs/dir.h>
48263002Sroyger#include <ufs/ffs/fs.h>
49263002Sroyger
50284870Sroyger#include <err.h>
51263002Sroyger#include <errno.h>
52263002Sroyger#include <string.h>
53263002Sroyger#include <ctype.h>
54263002Sroyger#include <fstab.h>
55263002Sroyger#include <paths.h>
56263002Sroyger#include <stdio.h>
57263002Sroyger#include <stdlib.h>
58263002Sroyger#include <unistd.h>
59263002Sroyger
60263002Sroyger#include "fsck.h"
61263002Sroyger
62263002Sroyger
63263002Sroygerchar *
64263002Sroygerblockcheck(origname)
65263002Sroyger	char *origname;
66263002Sroyger{
67263002Sroyger	struct stat stslash, stblock, stchar;
68263002Sroyger	char *newname, *raw, *cp;
69263002Sroyger	struct fstab *fsinfo;
70263002Sroyger	int retried = 0, len;
71263002Sroyger	static char device[MAXPATHLEN];
72263002Sroyger
73263002Sroyger	newname = origname;
74263002Sroyger	if (stat(newname, &stblock) < 0) {
75263002Sroyger		cp = strrchr(newname, '/');
76263002Sroyger		if (cp == 0) {
77263002Sroyger			(void)snprintf(device, sizeof(device), "%s%s",
78263002Sroyger				_PATH_DEV, newname);
79263002Sroyger			newname = device;
80263002Sroyger		}
81263002Sroyger	}
82263002Sroygerretry:
83263002Sroyger	if (stat(newname, &stblock) < 0) {
84263002Sroyger		printf("Can't stat %s: %s\n", newname, strerror(errno));
85263002Sroyger		return (origname);
86263002Sroyger	}
87263002Sroyger	switch(stblock.st_mode & S_IFMT) {
88	case S_IFCHR:
89	case S_IFBLK:
90		return(newname);
91	case S_IFDIR:
92		if (retried)
93			break;
94
95		len = strlen(origname) - 1;
96		if (len > 0 && origname[len] == '/')
97			/* remove trailing slash */
98			origname[len] = '\0';
99		if ((fsinfo = getfsfile(origname)) == NULL) {
100			printf(
101			    "Can't resolve %s to character special device.\n",
102			    origname);
103			return (origname);
104		}
105		newname = fsinfo->fs_spec;
106		retried++;
107		goto retry;
108	}
109	/*
110	 * Not a block or character device, just return name and
111	 * let the user decide whether to use it.
112	 */
113	return (origname);
114}
115
116void
117infohandler(sig)
118	int sig;
119{
120	got_siginfo = 1;
121}
122