xref: /freebsd/sbin/fsck/fsutil.c (revision 1d386b48)
16cf357bcSUlrich Spörlein /*	$NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $	*/
2da7e7114SAdrian Chadd 
38a16b7a1SPedro F. Giffuni /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
6da7e7114SAdrian Chadd  * Copyright (c) 1990, 1993
7da7e7114SAdrian Chadd  *	The Regents of the University of California.  All rights reserved.
8da7e7114SAdrian Chadd  *
9da7e7114SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
10da7e7114SAdrian Chadd  * modification, are permitted provided that the following conditions
11da7e7114SAdrian Chadd  * are met:
12da7e7114SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
13da7e7114SAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
14da7e7114SAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
15da7e7114SAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
16da7e7114SAdrian Chadd  *    documentation and/or other materials provided with the distribution.
176cf357bcSUlrich Spörlein  * 3. Neither the name of the University nor the names of its contributors
18da7e7114SAdrian Chadd  *    may be used to endorse or promote products derived from this software
19da7e7114SAdrian Chadd  *    without specific prior written permission.
20da7e7114SAdrian Chadd  *
21da7e7114SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22da7e7114SAdrian Chadd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23da7e7114SAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24da7e7114SAdrian Chadd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25da7e7114SAdrian Chadd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26da7e7114SAdrian Chadd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27da7e7114SAdrian Chadd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28da7e7114SAdrian Chadd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29da7e7114SAdrian Chadd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30da7e7114SAdrian Chadd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31da7e7114SAdrian Chadd  * SUCH DAMAGE.
32da7e7114SAdrian Chadd  */
33da7e7114SAdrian Chadd 
34da7e7114SAdrian Chadd #include <sys/cdefs.h>
35da7e7114SAdrian Chadd #ifndef lint
366cf357bcSUlrich Spörlein __RCSID("$NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $");
37da7e7114SAdrian Chadd #endif /* not lint */
38a02a0079SKirk McKusick #include <sys/param.h>
39da7e7114SAdrian Chadd #include <sys/stat.h>
40a02a0079SKirk McKusick #include <sys/mount.h>
41da7e7114SAdrian Chadd 
42b813a714SMark Murray #include <err.h>
43c72372c6SKirk McKusick #include <fstab.h>
44b813a714SMark Murray #include <paths.h>
45b813a714SMark Murray #include <stdarg.h>
46b813a714SMark Murray #include <stdio.h>
47b813a714SMark Murray #include <stdlib.h>
48b813a714SMark Murray #include <string.h>
49b813a714SMark Murray 
50da7e7114SAdrian Chadd #include "fsutil.h"
51da7e7114SAdrian Chadd 
52da7e7114SAdrian Chadd static const char *dev = NULL;
53da7e7114SAdrian Chadd static int preen = 0;
54da7e7114SAdrian Chadd 
55b70cd7eeSWarner Losh static void vmsg(int, const char *, va_list) __printflike(2, 0);
56da7e7114SAdrian Chadd 
57c72372c6SKirk McKusick /*
58c72372c6SKirk McKusick  * The getfsopt() function checks whether an option is present in
59c72372c6SKirk McKusick  * an fstab(5) fs_mntops entry. There are six possible cases:
60c72372c6SKirk McKusick  *
61c72372c6SKirk McKusick  * fs_mntops  getfsopt  result
62c72372c6SKirk McKusick  *  rw,foo       foo    true
63c72372c6SKirk McKusick  *  rw,nofoo    nofoo   true
64c72372c6SKirk McKusick  *  rw,nofoo     foo    false
65c72372c6SKirk McKusick  *  rw,foo      nofoo   false
66c72372c6SKirk McKusick  *  rw           foo    false
67c72372c6SKirk McKusick  *  rw          nofoo   false
68c72372c6SKirk McKusick  *
69c72372c6SKirk McKusick  * This function should be part of and documented in getfsent(3).
70c72372c6SKirk McKusick  */
71c72372c6SKirk McKusick int
getfsopt(struct fstab * fs,const char * option)72c72372c6SKirk McKusick getfsopt(struct fstab *fs, const char *option)
73c72372c6SKirk McKusick {
74c72372c6SKirk McKusick 	int negative, found;
75c72372c6SKirk McKusick 	char *opt, *optbuf;
76c72372c6SKirk McKusick 
77c72372c6SKirk McKusick 	if (option[0] == 'n' && option[1] == 'o') {
78c72372c6SKirk McKusick 		negative = 1;
79c72372c6SKirk McKusick 		option += 2;
80c72372c6SKirk McKusick 	} else
81c72372c6SKirk McKusick 		negative = 0;
82c72372c6SKirk McKusick 	optbuf = strdup(fs->fs_mntops);
83c72372c6SKirk McKusick 	found = 0;
84c72372c6SKirk McKusick 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
85c72372c6SKirk McKusick 		if (opt[0] == 'n' && opt[1] == 'o') {
86c72372c6SKirk McKusick 			if (!strcasecmp(opt + 2, option))
87c72372c6SKirk McKusick 				found = negative;
88c72372c6SKirk McKusick 		} else if (!strcasecmp(opt, option))
89c72372c6SKirk McKusick 			found = !negative;
90c72372c6SKirk McKusick 	}
91c72372c6SKirk McKusick 	free(optbuf);
92c72372c6SKirk McKusick 	return (found);
93c72372c6SKirk McKusick }
94c72372c6SKirk McKusick 
95da7e7114SAdrian Chadd void
setcdevname(const char * cd,int pr)96b70cd7eeSWarner Losh setcdevname(const char *cd, int pr)
97da7e7114SAdrian Chadd {
98da7e7114SAdrian Chadd 	dev = cd;
99da7e7114SAdrian Chadd 	preen = pr;
100da7e7114SAdrian Chadd }
101da7e7114SAdrian Chadd 
102da7e7114SAdrian Chadd const char *
cdevname(void)103b70cd7eeSWarner Losh cdevname(void)
104da7e7114SAdrian Chadd {
105da7e7114SAdrian Chadd 	return dev;
106da7e7114SAdrian Chadd }
107da7e7114SAdrian Chadd 
108da7e7114SAdrian Chadd static void
vmsg(int fatal,const char * fmt,va_list ap)109b70cd7eeSWarner Losh vmsg(int fatal, const char *fmt, va_list ap)
110da7e7114SAdrian Chadd {
111da7e7114SAdrian Chadd 	if (!fatal && preen)
112da7e7114SAdrian Chadd 		(void) printf("%s: ", dev);
113da7e7114SAdrian Chadd 
114da7e7114SAdrian Chadd 	(void) vprintf(fmt, ap);
115da7e7114SAdrian Chadd 
116da7e7114SAdrian Chadd 	if (fatal && preen)
117da7e7114SAdrian Chadd 		(void) printf("\n");
118da7e7114SAdrian Chadd 
119da7e7114SAdrian Chadd 	if (fatal && preen) {
120da7e7114SAdrian Chadd 		(void) printf(
121da7e7114SAdrian Chadd 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
122b813a714SMark Murray 		    dev, getprogname());
123da7e7114SAdrian Chadd 		exit(8);
124da7e7114SAdrian Chadd 	}
125da7e7114SAdrian Chadd }
126da7e7114SAdrian Chadd 
127da7e7114SAdrian Chadd /*VARARGS*/
128da7e7114SAdrian Chadd void
pfatal(const char * fmt,...)129da7e7114SAdrian Chadd pfatal(const char *fmt, ...)
130da7e7114SAdrian Chadd {
131da7e7114SAdrian Chadd 	va_list ap;
132da7e7114SAdrian Chadd 
133da7e7114SAdrian Chadd 	va_start(ap, fmt);
134da7e7114SAdrian Chadd 	vmsg(1, fmt, ap);
135da7e7114SAdrian Chadd 	va_end(ap);
136da7e7114SAdrian Chadd }
137da7e7114SAdrian Chadd 
138da7e7114SAdrian Chadd /*VARARGS*/
139da7e7114SAdrian Chadd void
pwarn(const char * fmt,...)140da7e7114SAdrian Chadd pwarn(const char *fmt, ...)
141da7e7114SAdrian Chadd {
142da7e7114SAdrian Chadd 	va_list ap;
143da7e7114SAdrian Chadd 
144b70cd7eeSWarner Losh 	va_start(ap, fmt);
145da7e7114SAdrian Chadd 	vmsg(0, fmt, ap);
146da7e7114SAdrian Chadd 	va_end(ap);
147da7e7114SAdrian Chadd }
148da7e7114SAdrian Chadd 
149da7e7114SAdrian Chadd void
perr(const char * fmt,...)1506cf357bcSUlrich Spörlein perr(const char *fmt, ...)
151da7e7114SAdrian Chadd {
1526cf357bcSUlrich Spörlein 	va_list ap;
1536cf357bcSUlrich Spörlein 
1546cf357bcSUlrich Spörlein 	va_start(ap, fmt);
1556cf357bcSUlrich Spörlein 	vmsg(1, fmt, ap);
1566cf357bcSUlrich Spörlein 	va_end(ap);
157da7e7114SAdrian Chadd }
158da7e7114SAdrian Chadd 
159da7e7114SAdrian Chadd void
panic(const char * fmt,...)160da7e7114SAdrian Chadd panic(const char *fmt, ...)
161da7e7114SAdrian Chadd {
162da7e7114SAdrian Chadd 	va_list ap;
163da7e7114SAdrian Chadd 
164da7e7114SAdrian Chadd 	va_start(ap, fmt);
165da7e7114SAdrian Chadd 	vmsg(1, fmt, ap);
166da7e7114SAdrian Chadd 	va_end(ap);
167da7e7114SAdrian Chadd 	exit(8);
168da7e7114SAdrian Chadd }
169da7e7114SAdrian Chadd 
170da7e7114SAdrian Chadd const char *
devcheck(const char * origname)171b70cd7eeSWarner Losh devcheck(const char *origname)
172da7e7114SAdrian Chadd {
173da7e7114SAdrian Chadd 	struct stat stslash, stchar;
174da7e7114SAdrian Chadd 
175da7e7114SAdrian Chadd 	if (stat("/", &stslash) < 0) {
1766cf357bcSUlrich Spörlein 		perr("Can't stat `/'");
177da7e7114SAdrian Chadd 		return (origname);
178da7e7114SAdrian Chadd 	}
179da7e7114SAdrian Chadd 	if (stat(origname, &stchar) < 0) {
1806cf357bcSUlrich Spörlein 		perr("Can't stat %s\n", origname);
181da7e7114SAdrian Chadd 		return (origname);
182da7e7114SAdrian Chadd 	}
183da7e7114SAdrian Chadd 	if (!S_ISCHR(stchar.st_mode)) {
1846cf357bcSUlrich Spörlein 		perr("%s is not a char device\n", origname);
185da7e7114SAdrian Chadd 	}
186da7e7114SAdrian Chadd 	return (origname);
187da7e7114SAdrian Chadd }
188da7e7114SAdrian Chadd 
189da7e7114SAdrian Chadd void *
emalloc(size_t s)190b70cd7eeSWarner Losh emalloc(size_t s)
191da7e7114SAdrian Chadd {
192da7e7114SAdrian Chadd 	void *p;
193da7e7114SAdrian Chadd 
194da7e7114SAdrian Chadd 	p = malloc(s);
195da7e7114SAdrian Chadd 	if (p == NULL)
196da7e7114SAdrian Chadd 		err(1, "malloc failed");
197da7e7114SAdrian Chadd 	return (p);
198da7e7114SAdrian Chadd }
199da7e7114SAdrian Chadd 
200da7e7114SAdrian Chadd 
201da7e7114SAdrian Chadd void *
erealloc(void * p,size_t s)202b70cd7eeSWarner Losh erealloc(void *p, size_t s)
203da7e7114SAdrian Chadd {
204da7e7114SAdrian Chadd 	void *q;
205da7e7114SAdrian Chadd 
206da7e7114SAdrian Chadd 	q = realloc(p, s);
207da7e7114SAdrian Chadd 	if (q == NULL)
208da7e7114SAdrian Chadd 		err(1, "realloc failed");
209da7e7114SAdrian Chadd 	return (q);
210da7e7114SAdrian Chadd }
211da7e7114SAdrian Chadd 
212da7e7114SAdrian Chadd 
213da7e7114SAdrian Chadd char *
estrdup(const char * s)214b70cd7eeSWarner Losh estrdup(const char *s)
215da7e7114SAdrian Chadd {
216da7e7114SAdrian Chadd 	char *p;
217da7e7114SAdrian Chadd 
218da7e7114SAdrian Chadd 	p = strdup(s);
219da7e7114SAdrian Chadd 	if (p == NULL)
220da7e7114SAdrian Chadd 		err(1, "strdup failed");
221da7e7114SAdrian Chadd 	return (p);
222da7e7114SAdrian Chadd }
223