xref: /freebsd/sbin/fsck/fsutil.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*	$NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 1990, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $");
37 #endif /* not lint */
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/stat.h>
42 #include <sys/mount.h>
43 
44 #include <err.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 perr(const char *fmt, ...)
114 {
115 	va_list ap;
116 
117 	va_start(ap, fmt);
118 	vmsg(1, fmt, ap);
119 	va_end(ap);
120 }
121 
122 void
123 panic(const char *fmt, ...)
124 {
125 	va_list ap;
126 
127 	va_start(ap, fmt);
128 	vmsg(1, fmt, ap);
129 	va_end(ap);
130 	exit(8);
131 }
132 
133 const char *
134 devcheck(const char *origname)
135 {
136 	struct stat stslash, stchar;
137 
138 	if (stat("/", &stslash) < 0) {
139 		perr("Can't stat `/'");
140 		return (origname);
141 	}
142 	if (stat(origname, &stchar) < 0) {
143 		perr("Can't stat %s\n", origname);
144 		return (origname);
145 	}
146 	if (!S_ISCHR(stchar.st_mode)) {
147 		perr("%s is not a char device\n", origname);
148 	}
149 	return (origname);
150 }
151 
152 /*
153  * Get the mount point information for name.
154  */
155 struct statfs *
156 getmntpt(const char *name)
157 {
158 	struct stat devstat, mntdevstat;
159 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
160 	char *dev_name;
161 	struct statfs *mntbuf, *statfsp;
162 	int i, mntsize, isdev;
163 
164 	if (stat(name, &devstat) != 0)
165 		return (NULL);
166 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
167 		isdev = 1;
168 	else
169 		isdev = 0;
170 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
171 	for (i = 0; i < mntsize; i++) {
172 		statfsp = &mntbuf[i];
173 		dev_name = statfsp->f_mntfromname;
174 		if (*dev_name != '/') {
175 			if (strlen(_PATH_DEV) + strlen(dev_name) + 1 >
176 			    sizeof(statfsp->f_mntfromname))
177 				continue;
178 			strcpy(device, _PATH_DEV);
179 			strcat(device, dev_name);
180 			strcpy(statfsp->f_mntfromname, device);
181 		}
182 		if (isdev == 0) {
183 			if (strcmp(name, statfsp->f_mntonname))
184 				continue;
185 			return (statfsp);
186 		}
187 		if (stat(dev_name, &mntdevstat) == 0 &&
188 		    mntdevstat.st_rdev == devstat.st_rdev)
189 			return (statfsp);
190 	}
191 	statfsp = NULL;
192 	return (statfsp);
193 }
194 
195 
196 void *
197 emalloc(size_t s)
198 {
199 	void *p;
200 
201 	p = malloc(s);
202 	if (p == NULL)
203 		err(1, "malloc failed");
204 	return (p);
205 }
206 
207 
208 void *
209 erealloc(void *p, size_t s)
210 {
211 	void *q;
212 
213 	q = realloc(p, s);
214 	if (q == NULL)
215 		err(1, "realloc failed");
216 	return (q);
217 }
218 
219 
220 char *
221 estrdup(const char *s)
222 {
223 	char *p;
224 
225 	p = strdup(s);
226 	if (p == NULL)
227 		err(1, "strdup failed");
228 	return (p);
229 }
230