]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck/fsutil.c
Decode Deallocate Logical Block Features.
[FreeBSD/FreeBSD.git] / sbin / fsck / fsutil.c
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 <errno.h>
46 #include <fstab.h>
47 #include <paths.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include "fsutil.h"
54
55 static const char *dev = NULL;
56 static int preen = 0;
57
58 static void vmsg(int, const char *, va_list) __printflike(2, 0);
59
60 void
61 setcdevname(const char *cd, int pr)
62 {
63         dev = cd;
64         preen = pr;
65 }
66
67 const char *
68 cdevname(void)
69 {
70         return dev;
71 }
72
73 static void
74 vmsg(int fatal, const char *fmt, va_list ap)
75 {
76         if (!fatal && preen)
77                 (void) printf("%s: ", dev);
78
79         (void) vprintf(fmt, ap);
80
81         if (fatal && preen)
82                 (void) printf("\n");
83
84         if (fatal && preen) {
85                 (void) printf(
86                     "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
87                     dev, getprogname());
88                 exit(8);
89         }
90 }
91
92 /*VARARGS*/
93 void
94 pfatal(const char *fmt, ...)
95 {
96         va_list ap;
97
98         va_start(ap, fmt);
99         vmsg(1, fmt, ap);
100         va_end(ap);
101 }
102
103 /*VARARGS*/
104 void
105 pwarn(const char *fmt, ...)
106 {
107         va_list ap;
108
109         va_start(ap, fmt);
110         vmsg(0, fmt, ap);
111         va_end(ap);
112 }
113
114 void
115 perr(const char *fmt, ...)
116 {
117         va_list ap;
118
119         va_start(ap, fmt);
120         vmsg(1, fmt, ap);
121         va_end(ap);
122 }
123
124 void
125 panic(const char *fmt, ...)
126 {
127         va_list ap;
128
129         va_start(ap, fmt);
130         vmsg(1, fmt, ap);
131         va_end(ap);
132         exit(8);
133 }
134
135 const char *
136 devcheck(const char *origname)
137 {
138         struct stat stslash, stchar;
139
140         if (stat("/", &stslash) < 0) {
141                 perr("Can't stat `/'");
142                 return (origname);
143         }
144         if (stat(origname, &stchar) < 0) {
145                 perr("Can't stat %s\n", origname);
146                 return (origname);
147         }
148         if (!S_ISCHR(stchar.st_mode)) {
149                 perr("%s is not a char device\n", origname);
150         }
151         return (origname);
152 }
153
154 /*
155  * Get the mount point information for name.
156  */
157 struct statfs *
158 getmntpt(const char *name)
159 {
160         struct stat devstat, mntdevstat;
161         char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
162         char *dev_name;
163         struct statfs *mntbuf, *statfsp;
164         int i, mntsize, isdev;
165
166         if (stat(name, &devstat) != 0)
167                 return (NULL);
168         if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
169                 isdev = 1;
170         else
171                 isdev = 0;
172         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
173         for (i = 0; i < mntsize; i++) {
174                 statfsp = &mntbuf[i];
175                 dev_name = statfsp->f_mntfromname;
176                 if (*dev_name != '/') {
177                         if (strlen(_PATH_DEV) + strlen(dev_name) + 1 >
178                             sizeof(statfsp->f_mntfromname))
179                                 continue;
180                         strcpy(device, _PATH_DEV);
181                         strcat(device, dev_name);
182                         strcpy(statfsp->f_mntfromname, device);
183                 }
184                 if (isdev == 0) {
185                         if (strcmp(name, statfsp->f_mntonname))
186                                 continue;
187                         return (statfsp);
188                 }
189                 if (stat(dev_name, &mntdevstat) == 0 &&
190                     mntdevstat.st_rdev == devstat.st_rdev)
191                         return (statfsp);
192         }
193         statfsp = NULL;
194         return (statfsp);
195 }
196
197
198 void *
199 emalloc(size_t s)
200 {
201         void *p;
202
203         p = malloc(s);
204         if (p == NULL)
205                 err(1, "malloc failed");
206         return (p);
207 }
208
209
210 void *
211 erealloc(void *p, size_t s)
212 {
213         void *q;
214
215         q = realloc(p, s);
216         if (q == NULL)
217                 err(1, "realloc failed");
218         return (q);
219 }
220
221
222 char *
223 estrdup(const char *s)
224 {
225         char *p;
226
227         p = strdup(s);
228         if (p == NULL)
229                 err(1, "strdup failed");
230         return (p);
231 }