xref: /freebsd/sbin/fsck/fsutil.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 <fstab.h>
46 #include <paths.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include "fsutil.h"
53 
54 static const char *dev = NULL;
55 static int preen = 0;
56 
57 static void vmsg(int, const char *, va_list) __printflike(2, 0);
58 
59 /*
60  * The getfsopt() function checks whether an option is present in
61  * an fstab(5) fs_mntops entry. There are six possible cases:
62  *
63  * fs_mntops  getfsopt  result
64  *  rw,foo       foo    true
65  *  rw,nofoo    nofoo   true
66  *  rw,nofoo     foo    false
67  *  rw,foo      nofoo   false
68  *  rw           foo    false
69  *  rw          nofoo   false
70  *
71  * This function should be part of and documented in getfsent(3).
72  */
73 int
74 getfsopt(struct fstab *fs, const char *option)
75 {
76 	int negative, found;
77 	char *opt, *optbuf;
78 
79 	if (option[0] == 'n' && option[1] == 'o') {
80 		negative = 1;
81 		option += 2;
82 	} else
83 		negative = 0;
84 	optbuf = strdup(fs->fs_mntops);
85 	found = 0;
86 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
87 		if (opt[0] == 'n' && opt[1] == 'o') {
88 			if (!strcasecmp(opt + 2, option))
89 				found = negative;
90 		} else if (!strcasecmp(opt, option))
91 			found = !negative;
92 	}
93 	free(optbuf);
94 	return (found);
95 }
96 
97 void
98 setcdevname(const char *cd, int pr)
99 {
100 	dev = cd;
101 	preen = pr;
102 }
103 
104 const char *
105 cdevname(void)
106 {
107 	return dev;
108 }
109 
110 static void
111 vmsg(int fatal, const char *fmt, va_list ap)
112 {
113 	if (!fatal && preen)
114 		(void) printf("%s: ", dev);
115 
116 	(void) vprintf(fmt, ap);
117 
118 	if (fatal && preen)
119 		(void) printf("\n");
120 
121 	if (fatal && preen) {
122 		(void) printf(
123 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
124 		    dev, getprogname());
125 		exit(8);
126 	}
127 }
128 
129 /*VARARGS*/
130 void
131 pfatal(const char *fmt, ...)
132 {
133 	va_list ap;
134 
135 	va_start(ap, fmt);
136 	vmsg(1, fmt, ap);
137 	va_end(ap);
138 }
139 
140 /*VARARGS*/
141 void
142 pwarn(const char *fmt, ...)
143 {
144 	va_list ap;
145 
146 	va_start(ap, fmt);
147 	vmsg(0, fmt, ap);
148 	va_end(ap);
149 }
150 
151 void
152 perr(const char *fmt, ...)
153 {
154 	va_list ap;
155 
156 	va_start(ap, fmt);
157 	vmsg(1, fmt, ap);
158 	va_end(ap);
159 }
160 
161 void
162 panic(const char *fmt, ...)
163 {
164 	va_list ap;
165 
166 	va_start(ap, fmt);
167 	vmsg(1, fmt, ap);
168 	va_end(ap);
169 	exit(8);
170 }
171 
172 const char *
173 devcheck(const char *origname)
174 {
175 	struct stat stslash, stchar;
176 
177 	if (stat("/", &stslash) < 0) {
178 		perr("Can't stat `/'");
179 		return (origname);
180 	}
181 	if (stat(origname, &stchar) < 0) {
182 		perr("Can't stat %s\n", origname);
183 		return (origname);
184 	}
185 	if (!S_ISCHR(stchar.st_mode)) {
186 		perr("%s is not a char device\n", origname);
187 	}
188 	return (origname);
189 }
190 
191 void *
192 emalloc(size_t s)
193 {
194 	void *p;
195 
196 	p = malloc(s);
197 	if (p == NULL)
198 		err(1, "malloc failed");
199 	return (p);
200 }
201 
202 
203 void *
204 erealloc(void *p, size_t s)
205 {
206 	void *q;
207 
208 	q = realloc(p, s);
209 	if (q == NULL)
210 		err(1, "realloc failed");
211 	return (q);
212 }
213 
214 
215 char *
216 estrdup(const char *s)
217 {
218 	char *p;
219 
220 	p = strdup(s);
221 	if (p == NULL)
222 		err(1, "strdup failed");
223 	return (p);
224 }
225