aruu

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

df.c (4293B)


      1 /* see LICENSE file for copyright and license details */
      2 
      3 #include "filesystem.h"
      4 #include "util.h"
      5 
      6 #include <errno.h>
      7 #include <stdio.h>
      8 #include <string.h>
      9 
     10 static long blksize = 512;
     11 static int  aflag   = 0;
     12 static int  hflag   = 0;
     13 static int  kflag   = 0;
     14 
     15 #define CALC_POWER(n, power, base, i)                                                              \
     16   do {                                                                                             \
     17     while (n > power) {                                                                            \
     18       power = power * base;                                                                        \
     19       i++;                                                                                         \
     20     }                                                                                              \
     21   } while (0)
     22 
     23 static void
     24 print_human(
     25     const char        *fsname,
     26     unsigned long long total,
     27     unsigned long long used,
     28     unsigned long long avail,
     29     int                capacity,
     30     const char        *dir
     31 )
     32 {
     33   long               base        = 1024;
     34   unsigned long long power_total = base;
     35   unsigned long long power_used  = base;
     36   unsigned long long power_avail = base;
     37   char               postfixes[] = {'B', 'K', 'M', 'G', 'T', 'P', 'E'};
     38   int                i = 0, j = 0, k = 0;
     39 
     40   total = total * blksize;
     41   used  = used * blksize;
     42   avail = avail * blksize;
     43 
     44   CALC_POWER(total, power_total, base, i);
     45   CALC_POWER(used, power_used, base, j);
     46   CALC_POWER(avail, power_avail, base, k);
     47 
     48   total = i ? total / (power_total / base) : total;
     49   used  = j ? used / (power_used / base) : used;
     50   avail = k ? avail / (power_avail / base) : avail;
     51   printf(
     52       "%-12s %9llu%c %9llu%c %9llu%c %7d%%  %s\n",
     53       fsname,
     54       total,
     55       postfixes[i],
     56       used,
     57       postfixes[j],
     58       avail,
     59       postfixes[k],
     60       capacity,
     61       dir
     62   );
     63 }
     64 
     65 static int
     66 mnt_show(const char *fsname, const char *dir)
     67 {
     68   struct FilesystemStatus status;
     69   unsigned long long      total, used, avail;
     70   int                     capacity = 0;
     71   int                     bs;
     72 
     73   if (fs_get_status(dir, &status) < 0)
     74     return -1;
     75 
     76   bs    = status.block_size / blksize;
     77   total = status.total_blocks * bs;
     78   avail = status.free_blocks * bs;
     79   used  = total - avail;
     80 
     81   if (used + avail) {
     82     capacity = (used * 100) / (used + avail);
     83     if (used * 100 != capacity * (used + avail))
     84       capacity++;
     85   }
     86 
     87   if (hflag)
     88     print_human(fsname, total, used, avail, capacity, dir);
     89   else
     90     printf("%-12s %9llu %9llu %9llu %7d%%  %s\n", fsname, total, used, avail, capacity, dir);
     91 
     92   return 0;
     93 }
     94 
     95 static void
     96 usage(void)
     97 {
     98   eprintf("usage: %s [-a]\n", argv0);
     99 }
    100 
    101 // ?man df: report disk space usage
    102 // ?man display free and used disk space on filesystems
    103 int
    104 main(int argc, char *argv[])
    105 {
    106   struct FilesystemMount *mounts = NULL;
    107   size_t                  count;
    108   size_t                  i;
    109   int                     ret = 0;
    110 
    111   ARGBEGIN
    112   {
    113     // ?man -a: print or show all entries
    114     case 'a':
    115       aflag = 1;
    116       break;
    117     // ?man -h: suppress headers or print help
    118     case 'h':
    119       hflag = 1;
    120       kflag = 0;
    121       break;
    122     // ?man -k: specify option flag
    123     case 'k':
    124       kflag   = 1;
    125       hflag   = 0;
    126       blksize = 1024;
    127       break;
    128     // ?man -s: silent mode or print summary
    129     case 's':
    130       eprintf("-s is not supported\n");
    131       break;
    132     // ?man -i: interactive mode or prompt for confirmation
    133     case 'i':
    134       eprintf("-i is not supported\n");
    135       break;
    136     default:
    137       usage();
    138   }
    139   ARGEND;
    140 
    141   if (hflag)
    142     printf(
    143         "Filesystem         Size       Used      "
    144         "Avail Capacity   Mounted on\n"
    145     );
    146   else
    147     printf(
    148         "Filesystem  %ld-blocks      Used     "
    149         "Avail Capacity  Mounted on\n",
    150         blksize
    151     );
    152 
    153   if (fs_get_mounts(&mounts, &count) < 0) {
    154     if (errno == ENOTSUP)
    155       eprintf("filesystem mount table is not supported\n");
    156     eprintf("cannot read filesystem mount table:");
    157   }
    158   for (i = 0; i < count; i++) {
    159     if (aflag == 0 && strcmp(mounts[i].type, "rootfs") == 0)
    160       continue;
    161     if (mnt_show(mounts[i].source, mounts[i].path) < 0)
    162       ret = 1;
    163   }
    164   fs_free_mounts(mounts, count);
    165 
    166   return ret;
    167 }