1d704245uqs/*	$NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $	*/
29fab809adrian
3872b698pfg/*-
4872b698pfg * SPDX-License-Identifier: BSD-3-Clause
5872b698pfg *
69fab809adrian * Copyright (c) 1990, 1993
79fab809adrian *	The Regents of the University of California.  All rights reserved.
89fab809adrian *
99fab809adrian * Redistribution and use in source and binary forms, with or without
109fab809adrian * modification, are permitted provided that the following conditions
119fab809adrian * are met:
129fab809adrian * 1. Redistributions of source code must retain the above copyright
139fab809adrian *    notice, this list of conditions and the following disclaimer.
149fab809adrian * 2. Redistributions in binary form must reproduce the above copyright
159fab809adrian *    notice, this list of conditions and the following disclaimer in the
169fab809adrian *    documentation and/or other materials provided with the distribution.
17d704245uqs * 3. Neither the name of the University nor the names of its contributors
189fab809adrian *    may be used to endorse or promote products derived from this software
199fab809adrian *    without specific prior written permission.
209fab809adrian *
219fab809adrian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229fab809adrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239fab809adrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249fab809adrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259fab809adrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269fab809adrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279fab809adrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289fab809adrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299fab809adrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309fab809adrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319fab809adrian * SUCH DAMAGE.
329fab809adrian */
339fab809adrian
349fab809adrian#include <sys/cdefs.h>
359fab809adrian#ifndef lint
36d704245uqs__RCSID("$NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $");
379fab809adrian#endif /* not lint */
38bda6eb4markm__FBSDID("$FreeBSD$");
399fab809adrian
40658667dmckusick#include <sys/param.h>
419fab809adrian#include <sys/stat.h>
42658667dmckusick#include <sys/mount.h>
439fab809adrian
44bda6eb4markm#include <err.h>
45bda6eb4markm#include <paths.h>
46bda6eb4markm#include <stdarg.h>
47bda6eb4markm#include <stdio.h>
48bda6eb4markm#include <stdlib.h>
49bda6eb4markm#include <string.h>
50bda6eb4markm
519fab809adrian#include "fsutil.h"
529fab809adrian
539fab809adrianstatic const char *dev = NULL;
549fab809adrianstatic int preen = 0;
559fab809adrian
56120c3c2impstatic void vmsg(int, const char *, va_list) __printflike(2, 0);
579fab809adrian
589fab809adrianvoid
59120c3c2impsetcdevname(const char *cd, int pr)
609fab809adrian{
619fab809adrian	dev = cd;
629fab809adrian	preen = pr;
639fab809adrian}
649fab809adrian
659fab809adrianconst char *
66120c3c2impcdevname(void)
679fab809adrian{
689fab809adrian	return dev;
699fab809adrian}
709fab809adrian
719fab809adrianstatic void
72120c3c2impvmsg(int fatal, const char *fmt, va_list ap)
739fab809adrian{
749fab809adrian	if (!fatal && preen)
759fab809adrian		(void) printf("%s: ", dev);
769fab809adrian
779fab809adrian	(void) vprintf(fmt, ap);
789fab809adrian
799fab809adrian	if (fatal && preen)
809fab809adrian		(void) printf("\n");
819fab809adrian
829fab809adrian	if (fatal && preen) {
839fab809adrian		(void) printf(
849fab809adrian		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
85bda6eb4markm		    dev, getprogname());
869fab809adrian		exit(8);
879fab809adrian	}
889fab809adrian}
899fab809adrian
909fab809adrian/*VARARGS*/
919fab809adrianvoid
929fab809adrianpfatal(const char *fmt, ...)
939fab809adrian{
949fab809adrian	va_list ap;
959fab809adrian
969fab809adrian	va_start(ap, fmt);
979fab809adrian	vmsg(1, fmt, ap);
989fab809adrian	va_end(ap);
999fab809adrian}
1009fab809adrian
1019fab809adrian/*VARARGS*/
1029fab809adrianvoid
1039fab809adrianpwarn(const char *fmt, ...)
1049fab809adrian{
1059fab809adrian	va_list ap;
1069fab809adrian
107120c3c2imp	va_start(ap, fmt);
1089fab809adrian	vmsg(0, fmt, ap);
1099fab809adrian	va_end(ap);
1109fab809adrian}
1119fab809adrian
1129fab809adrianvoid
113d704245uqsperr(const char *fmt, ...)
1149fab809adrian{
115d704245uqs	va_list ap;
116d704245uqs
117d704245uqs	va_start(ap, fmt);
118d704245uqs	vmsg(1, fmt, ap);
119d704245uqs	va_end(ap);
1209fab809adrian}
1219fab809adrian
1229fab809adrianvoid
1239fab809adrianpanic(const char *fmt, ...)
1249fab809adrian{
1259fab809adrian	va_list ap;
1269fab809adrian
1279fab809adrian	va_start(ap, fmt);
1289fab809adrian	vmsg(1, fmt, ap);
1299fab809adrian	va_end(ap);
1309fab809adrian	exit(8);
1319fab809adrian}
1329fab809adrian
1339fab809adrianconst char *
134120c3c2impdevcheck(const char *origname)
1359fab809adrian{
1369fab809adrian	struct stat stslash, stchar;
1379fab809adrian
1389fab809adrian	if (stat("/", &stslash) < 0) {
139d704245uqs		perr("Can't stat `/'");
1409fab809adrian		return (origname);
1419fab809adrian	}
1429fab809adrian	if (stat(origname, &stchar) < 0) {
143d704245uqs		perr("Can't stat %s\n", origname);
1449fab809adrian		return (origname);
1459fab809adrian	}
1469fab809adrian	if (!S_ISCHR(stchar.st_mode)) {
147d704245uqs		perr("%s is not a char device\n", origname);
1489fab809adrian	}
1499fab809adrian	return (origname);
1509fab809adrian}
1519fab809adrian
152658667dmckusick/*
153658667dmckusick * Get the mount point information for name.
154658667dmckusick */
155658667dmckusickstruct statfs *
156120c3c2impgetmntpt(const char *name)
157658667dmckusick{
158658667dmckusick	struct stat devstat, mntdevstat;
159658667dmckusick	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
160d704245uqs	char *dev_name;
161658667dmckusick	struct statfs *mntbuf, *statfsp;
162658667dmckusick	int i, mntsize, isdev;
163658667dmckusick
164658667dmckusick	if (stat(name, &devstat) != 0)
165658667dmckusick		return (NULL);
166658667dmckusick	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
167658667dmckusick		isdev = 1;
168658667dmckusick	else
169658667dmckusick		isdev = 0;
170658667dmckusick	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
171658667dmckusick	for (i = 0; i < mntsize; i++) {
172658667dmckusick		statfsp = &mntbuf[i];
173d704245uqs		dev_name = statfsp->f_mntfromname;
174d704245uqs		if (*dev_name != '/') {
1758e40525cem			if (strlen(_PATH_DEV) + strlen(dev_name) + 1 >
1768e40525cem			    sizeof(statfsp->f_mntfromname))
1778e40525cem				continue;
178658667dmckusick			strcpy(device, _PATH_DEV);
179d704245uqs			strcat(device, dev_name);
180658667dmckusick			strcpy(statfsp->f_mntfromname, device);
181658667dmckusick		}
182658667dmckusick		if (isdev == 0) {
183658667dmckusick			if (strcmp(name, statfsp->f_mntonname))
184658667dmckusick				continue;
185658667dmckusick			return (statfsp);
186658667dmckusick		}
187d704245uqs		if (stat(dev_name, &mntdevstat) == 0 &&
188658667dmckusick		    mntdevstat.st_rdev == devstat.st_rdev)
189658667dmckusick			return (statfsp);
190658667dmckusick	}
191658667dmckusick	statfsp = NULL;
192658667dmckusick	return (statfsp);
193658667dmckusick}
194658667dmckusick
1959fab809adrian
1969fab809adrianvoid *
197120c3c2impemalloc(size_t s)
1989fab809adrian{
1999fab809adrian	void *p;
2009fab809adrian
2019fab809adrian	p = malloc(s);
2029fab809adrian	if (p == NULL)
2039fab809adrian		err(1, "malloc failed");
2049fab809adrian	return (p);
2059fab809adrian}
2069fab809adrian
2079fab809adrian
2089fab809adrianvoid *
209120c3c2imperealloc(void *p, size_t s)
2109fab809adrian{
2119fab809adrian	void *q;
2129fab809adrian
2139fab809adrian	q = realloc(p, s);
2149fab809adrian	if (q == NULL)
2159fab809adrian		err(1, "realloc failed");
2169fab809adrian	return (q);
2179fab809adrian}
2189fab809adrian
2199fab809adrian
2209fab809adrianchar *
221120c3c2impestrdup(const char *s)
2229fab809adrian{
2239fab809adrian	char *p;
2249fab809adrian
2259fab809adrian	p = strdup(s);
2269fab809adrian	if (p == NULL)
2279fab809adrian		err(1, "strdup failed");
2289fab809adrian	return (p);
2299fab809adrian}
230