xref: /freebsd/sbin/fsck/fsutil.c (revision 894198ea)
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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __RCSID("$NetBSD: fsutil.c,v 1.7 1998/07/30 17:41:03 thorpej Exp $");
39 #endif /* not lint */
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/mount.h>
45 
46 #include <err.h>
47 #include <errno.h>
48 #include <fstab.h>
49 #include <paths.h>
50 #include <stdarg.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 
55 #include "fsutil.h"
56 
57 static const char *dev = NULL;
58 static int preen = 0;
59 
60 static void vmsg(int, const char *, va_list) __printflike(2, 0);
61 
62 void
63 setcdevname(const char *cd, int pr)
64 {
65 	dev = cd;
66 	preen = pr;
67 }
68 
69 const char *
70 cdevname(void)
71 {
72 	return dev;
73 }
74 
75 static void
76 vmsg(int fatal, const char *fmt, va_list ap)
77 {
78 	if (!fatal && preen)
79 		(void) printf("%s: ", dev);
80 
81 	(void) vprintf(fmt, ap);
82 
83 	if (fatal && preen)
84 		(void) printf("\n");
85 
86 	if (fatal && preen) {
87 		(void) printf(
88 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
89 		    dev, getprogname());
90 		exit(8);
91 	}
92 }
93 
94 /*VARARGS*/
95 void
96 pfatal(const char *fmt, ...)
97 {
98 	va_list ap;
99 
100 	va_start(ap, fmt);
101 	vmsg(1, fmt, ap);
102 	va_end(ap);
103 }
104 
105 /*VARARGS*/
106 void
107 pwarn(const char *fmt, ...)
108 {
109 	va_list ap;
110 
111 	va_start(ap, fmt);
112 	vmsg(0, fmt, ap);
113 	va_end(ap);
114 }
115 
116 void
117 perror(const char *s)
118 {
119 	pfatal("%s (%s)", s, strerror(errno));
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 		perror("/");
140 		printf("Can't stat root\n");
141 		return (origname);
142 	}
143 	if (stat(origname, &stchar) < 0) {
144 		perror(origname);
145 		printf("Can't stat %s\n", origname);
146 		return (origname);
147 	}
148 	if (!S_ISCHR(stchar.st_mode)) {
149 		perror(origname);
150 		printf("%s is not a char device\n", origname);
151 	}
152 	return (origname);
153 }
154 
155 /*
156  * Get the mount point information for name.
157  */
158 struct statfs *
159 getmntpt(const char *name)
160 {
161 	struct stat devstat, mntdevstat;
162 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
163 	char *devname;
164 	struct statfs *mntbuf, *statfsp;
165 	int i, mntsize, isdev;
166 
167 	if (stat(name, &devstat) != 0)
168 		return (NULL);
169 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
170 		isdev = 1;
171 	else
172 		isdev = 0;
173 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
174 	for (i = 0; i < mntsize; i++) {
175 		statfsp = &mntbuf[i];
176 		devname = statfsp->f_mntfromname;
177 		if (*devname != '/') {
178 			strcpy(device, _PATH_DEV);
179 			strcat(device, devname);
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(devname, &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