xref: /freebsd/sbin/fsck/fsutil.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*	$NetBSD: fsutil.c,v 1.7 1998/07/30 17:41:03 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: fsutil.c,v 1.7 1998/07/30 17:41:03 thorpej Exp $");
35 #endif /* not lint */
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/mount.h>
41 
42 #include <err.h>
43 #include <errno.h>
44 #include <fstab.h>
45 #include <paths.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include "fsutil.h"
52 
53 static const char *dev = NULL;
54 static int preen = 0;
55 
56 static void vmsg(int, const char *, va_list) __printflike(2, 0);
57 
58 void
59 setcdevname(const char *cd, int pr)
60 {
61 	dev = cd;
62 	preen = pr;
63 }
64 
65 const char *
66 cdevname(void)
67 {
68 	return dev;
69 }
70 
71 static void
72 vmsg(int fatal, const char *fmt, va_list ap)
73 {
74 	if (!fatal && preen)
75 		(void) printf("%s: ", dev);
76 
77 	(void) vprintf(fmt, ap);
78 
79 	if (fatal && preen)
80 		(void) printf("\n");
81 
82 	if (fatal && preen) {
83 		(void) printf(
84 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
85 		    dev, getprogname());
86 		exit(8);
87 	}
88 }
89 
90 /*VARARGS*/
91 void
92 pfatal(const char *fmt, ...)
93 {
94 	va_list ap;
95 
96 	va_start(ap, fmt);
97 	vmsg(1, fmt, ap);
98 	va_end(ap);
99 }
100 
101 /*VARARGS*/
102 void
103 pwarn(const char *fmt, ...)
104 {
105 	va_list ap;
106 
107 	va_start(ap, fmt);
108 	vmsg(0, fmt, ap);
109 	va_end(ap);
110 }
111 
112 void
113 perror(const char *s)
114 {
115 	pfatal("%s (%s)", s, strerror(errno));
116 }
117 
118 void
119 panic(const char *fmt, ...)
120 {
121 	va_list ap;
122 
123 	va_start(ap, fmt);
124 	vmsg(1, fmt, ap);
125 	va_end(ap);
126 	exit(8);
127 }
128 
129 const char *
130 devcheck(const char *origname)
131 {
132 	struct stat stslash, stchar;
133 
134 	if (stat("/", &stslash) < 0) {
135 		perror("/");
136 		printf("Can't stat root\n");
137 		return (origname);
138 	}
139 	if (stat(origname, &stchar) < 0) {
140 		perror(origname);
141 		printf("Can't stat %s\n", origname);
142 		return (origname);
143 	}
144 	if (!S_ISCHR(stchar.st_mode)) {
145 		perror(origname);
146 		printf("%s is not a char device\n", origname);
147 	}
148 	return (origname);
149 }
150 
151 /*
152  * Get the mount point information for name.
153  */
154 struct statfs *
155 getmntpt(const char *name)
156 {
157 	struct stat devstat, mntdevstat;
158 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
159 	char *devname;
160 	struct statfs *mntbuf, *statfsp;
161 	int i, mntsize, isdev;
162 
163 	if (stat(name, &devstat) != 0)
164 		return (NULL);
165 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
166 		isdev = 1;
167 	else
168 		isdev = 0;
169 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
170 	for (i = 0; i < mntsize; i++) {
171 		statfsp = &mntbuf[i];
172 		devname = statfsp->f_mntfromname;
173 		if (*devname != '/') {
174 			strcpy(device, _PATH_DEV);
175 			strcat(device, devname);
176 			strcpy(statfsp->f_mntfromname, device);
177 		}
178 		if (isdev == 0) {
179 			if (strcmp(name, statfsp->f_mntonname))
180 				continue;
181 			return (statfsp);
182 		}
183 		if (stat(devname, &mntdevstat) == 0 &&
184 		    mntdevstat.st_rdev == devstat.st_rdev)
185 			return (statfsp);
186 	}
187 	statfsp = NULL;
188 	return (statfsp);
189 }
190 
191 
192 void *
193 emalloc(size_t s)
194 {
195 	void *p;
196 
197 	p = malloc(s);
198 	if (p == NULL)
199 		err(1, "malloc failed");
200 	return (p);
201 }
202 
203 
204 void *
205 erealloc(void *p, size_t s)
206 {
207 	void *q;
208 
209 	q = realloc(p, s);
210 	if (q == NULL)
211 		err(1, "realloc failed");
212 	return (q);
213 }
214 
215 
216 char *
217 estrdup(const char *s)
218 {
219 	char *p;
220 
221 	p = strdup(s);
222 	if (p == NULL)
223 		err(1, "strdup failed");
224 	return (p);
225 }
226