aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

fsutil.c (6037B)


      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 "freebsd-compat.h"
     35 #if defined(__RCSID) && !defined(lint)
     36 __RCSID("$NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $");
     37 #endif /* not lint */
     38 #include <sys/param.h>
     39 #include <sys/stat.h>
     40 #include <sys/mount.h>
     41 
     42 #include <err.h>
     43 #include <paths.h>
     44 #include <stdarg.h>
     45 #include <stdio.h>
     46 #include <stdlib.h>
     47 #include <string.h>
     48 
     49 #include "fsutil.h"
     50 #include "util.h"
     51 
     52 static const char *dev   = NULL;
     53 static int         preen = 0;
     54 
     55 static void vmsg(int, const char *, va_list) __printflike(2, 0);
     56 
     57 #ifdef HAVE_FSTAB_H
     58 #include <fstab.h>
     59 #include <strings.h>
     60 
     61 /*
     62  * The getfsopt() function checks whether an option is present in
     63  * an fstab(5) fs_mntops entry. There are six possible cases:
     64  *
     65  * fs_mntops  getfsopt  result
     66  *  rw,foo       foo    true
     67  *  rw,nofoo    nofoo   true
     68  *  rw,nofoo     foo    false
     69  *  rw,foo      nofoo   false
     70  *  rw           foo    false
     71  *  rw          nofoo   false
     72  *
     73  * This function should be part of and documented in getfsent(3).
     74  */
     75 int
     76 getfsopt(struct fstab *fs, const char *option)
     77 {
     78   int   negative, found;
     79   char *opt, *optbuf;
     80 
     81   if (option[0] == 'n' && option[1] == 'o') {
     82     negative = 1;
     83     option += 2;
     84   } else
     85     negative = 0;
     86   optbuf = strdup(fs->fs_mntops);
     87   found  = 0;
     88   for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
     89     if (opt[0] == 'n' && opt[1] == 'o') {
     90       if (!strcasecmp(opt + 2, option))
     91         found = negative;
     92     } else if (!strcasecmp(opt, option))
     93       found = !negative;
     94   }
     95   free(optbuf);
     96   return (found);
     97 }
     98 #endif
     99 
    100 void
    101 setcdevname(const char *cd, int pr)
    102 {
    103   dev   = cd;
    104   preen = pr;
    105 }
    106 
    107 const char *
    108 cdevname(void)
    109 {
    110   return dev;
    111 }
    112 
    113 static void
    114 vmsg(int fatal, const char *fmt, va_list ap)
    115 {
    116   if (!fatal && preen)
    117     (void)printf("%s: ", dev);
    118 
    119   (void)vprintf(fmt, ap);
    120 
    121   if (fatal && preen)
    122     (void)printf("\n");
    123 
    124   if (fatal && preen) {
    125     (void)printf("%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n", dev, argv0);
    126     exit(8);
    127   }
    128 }
    129 
    130 /*VARARGS*/
    131 void
    132 pfatal(const char *fmt, ...)
    133 {
    134   va_list ap;
    135 
    136   va_start(ap, fmt);
    137   vmsg(1, fmt, ap);
    138   va_end(ap);
    139 }
    140 
    141 /*VARARGS*/
    142 void
    143 pwarn(const char *fmt, ...)
    144 {
    145   va_list ap;
    146 
    147   va_start(ap, fmt);
    148   vmsg(0, fmt, ap);
    149   va_end(ap);
    150 }
    151 
    152 void
    153 perr(const char *fmt, ...)
    154 {
    155   va_list ap;
    156 
    157   va_start(ap, fmt);
    158   vmsg(1, fmt, ap);
    159   va_end(ap);
    160 }
    161 
    162 void
    163 panic(const char *fmt, ...)
    164 {
    165   va_list ap;
    166 
    167   va_start(ap, fmt);
    168   vmsg(1, fmt, ap);
    169   va_end(ap);
    170   exit(8);
    171 }
    172 
    173 const char *
    174 devcheck(const char *origname)
    175 {
    176   struct stat stslash, stchar;
    177 
    178   if (stat("/", &stslash) < 0) {
    179     perr("Can't stat `/'");
    180     return (origname);
    181   }
    182   if (stat(origname, &stchar) < 0) {
    183     perr("Can't stat %s\n", origname);
    184     return (origname);
    185   }
    186   if (!S_ISCHR(stchar.st_mode)) {
    187     perr("%s is not a char device\n", origname);
    188   }
    189   return (origname);
    190 }
    191 
    192 #ifdef HAVE_GETMNTINFO
    193 /*
    194  * Get the mount point information for name.
    195  */
    196 struct statfs *
    197 getmntpt(const char *name)
    198 {
    199   struct stat    devstat, mntdevstat;
    200   char           device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
    201   char          *dev_name;
    202   struct statfs *mntbuf, *statfsp;
    203   int            i, mntsize, isdev;
    204   if (stat(name, &devstat) != 0)
    205     return (NULL);
    206   if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
    207     isdev = 1;
    208   else
    209     isdev = 0;
    210   mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    211   for (i = 0; i < mntsize; i++) {
    212     statfsp  = &mntbuf[i];
    213     dev_name = statfsp->f_mntfromname;
    214     if (*dev_name != '/') {
    215       if (strlen(_PATH_DEV) + strlen(dev_name) + 1 > sizeof(statfsp->f_mntfromname))
    216         continue;
    217       strcpy(device, _PATH_DEV);
    218       strcat(device, dev_name);
    219       strcpy(statfsp->f_mntfromname, device);
    220     }
    221     if (isdev == 0) {
    222       if (strcmp(name, statfsp->f_mntonname))
    223         continue;
    224       return (statfsp);
    225     }
    226     if (stat(dev_name, &mntdevstat) == 0 && mntdevstat.st_rdev == devstat.st_rdev)
    227       return (statfsp);
    228   }
    229   statfsp = NULL;
    230   return (statfsp);
    231 }
    232 #endif
    233 
    234 void *
    235 emalloc(size_t s)
    236 {
    237   void *p;
    238 
    239   p = malloc(s);
    240   if (p == NULL)
    241     err(1, "malloc failed");
    242   return (p);
    243 }
    244 
    245 void *
    246 erealloc(void *p, size_t s)
    247 {
    248   void *q;
    249 
    250   q = realloc(p, s);
    251   if (q == NULL)
    252     err(1, "realloc failed");
    253   return (q);
    254 }
    255 
    256 char *
    257 estrdup(const char *s)
    258 {
    259   char *p;
    260 
    261   p = strdup(s);
    262   if (p == NULL)
    263     err(1, "strdup failed");
    264   return (p);
    265 }