/* $NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 1990, 19936* The Regents of the University of California. All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16* 3. Neither the name of the University nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include <sys/cdefs.h>34#ifndef lint35__RCSID("$NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $");36#endif /* not lint */37#include <sys/param.h>38#include <sys/stat.h>39#include <sys/mount.h>4041#include <err.h>42#include <fstab.h>43#include <paths.h>44#include <stdarg.h>45#include <stdio.h>46#include <stdlib.h>47#include <string.h>4849#include "fsutil.h"5051static const char *dev = NULL;52static int preen = 0;5354static void vmsg(int, const char *, va_list) __printflike(2, 0);5556/*57* The getfsopt() function checks whether an option is present in58* an fstab(5) fs_mntops entry. There are six possible cases:59*60* fs_mntops getfsopt result61* rw,foo foo true62* rw,nofoo nofoo true63* rw,nofoo foo false64* rw,foo nofoo false65* rw foo false66* rw nofoo false67*68* This function should be part of and documented in getfsent(3).69*/70int71getfsopt(struct fstab *fs, const char *option)72{73int negative, found;74char *opt, *optbuf;7576if (option[0] == 'n' && option[1] == 'o') {77negative = 1;78option += 2;79} else80negative = 0;81optbuf = strdup(fs->fs_mntops);82found = 0;83for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {84if (opt[0] == 'n' && opt[1] == 'o') {85if (!strcasecmp(opt + 2, option))86found = negative;87} else if (!strcasecmp(opt, option))88found = !negative;89}90free(optbuf);91return (found);92}9394void95setcdevname(const char *cd, int pr)96{97dev = cd;98preen = pr;99}100101const char *102cdevname(void)103{104return dev;105}106107static void108vmsg(int fatal, const char *fmt, va_list ap)109{110if (!fatal && preen)111(void) printf("%s: ", dev);112113(void) vprintf(fmt, ap);114115if (fatal && preen)116(void) printf("\n");117118if (fatal && preen) {119(void) printf(120"%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",121dev, getprogname());122exit(8);123}124}125126/*VARARGS*/127void128pfatal(const char *fmt, ...)129{130va_list ap;131132va_start(ap, fmt);133vmsg(1, fmt, ap);134va_end(ap);135}136137/*VARARGS*/138void139pwarn(const char *fmt, ...)140{141va_list ap;142143va_start(ap, fmt);144vmsg(0, fmt, ap);145va_end(ap);146}147148void149perr(const char *fmt, ...)150{151va_list ap;152153va_start(ap, fmt);154vmsg(1, fmt, ap);155va_end(ap);156}157158void159panic(const char *fmt, ...)160{161va_list ap;162163va_start(ap, fmt);164vmsg(1, fmt, ap);165va_end(ap);166exit(8);167}168169const char *170devcheck(const char *origname)171{172struct stat stslash, stchar;173174if (stat("/", &stslash) < 0) {175perr("Can't stat `/'");176return (origname);177}178if (stat(origname, &stchar) < 0) {179perr("Can't stat %s\n", origname);180return (origname);181}182if (!S_ISCHR(stchar.st_mode)) {183perr("%s is not a char device\n", origname);184}185return (origname);186}187188void *189emalloc(size_t s)190{191void *p;192193p = malloc(s);194if (p == NULL)195err(1, "malloc failed");196return (p);197}198199200void *201erealloc(void *p, size_t s)202{203void *q;204205q = realloc(p, s);206if (q == NULL)207err(1, "realloc failed");208return (q);209}210211212char *213estrdup(const char *s)214{215char *p;216217p = strdup(s);218if (p == NULL)219err(1, "strdup failed");220return (p);221}222223224