sbase/df.c

137 lines
2.8 KiB
C
Raw Normal View History

/* See LICENSE file for copyright and license details. */
2014-06-30 18:03:41 +00:00
#include <sys/statvfs.h>
#include <mntent.h>
#include <stdio.h>
#include <stdlib.h>
2014-03-18 14:19:56 +00:00
#include <string.h>
2014-06-30 18:03:41 +00:00
#include "util.h"
2014-10-18 17:15:26 +00:00
static long blksize = 512;
static int aflag = 0;
static int hflag = 0;
static int kflag = 0;
static void mnt_show(const char *fsname, const char *dir);
static void
usage(void)
{
2013-09-01 16:53:21 +00:00
eprintf("usage: %s [-a]\n", argv0);
}
int
main(int argc, char *argv[])
{
struct mntent *me = NULL;
FILE *fp;
ARGBEGIN {
case 'a':
2014-03-18 14:19:56 +00:00
aflag = 1;
break;
case 'h':
2014-10-18 17:15:26 +00:00
hflag = 1;
kflag = 0;
2014-10-18 17:15:26 +00:00
break;
case 'k':
kflag = 1;
hflag = 0;
2014-10-18 17:15:26 +00:00
blksize = 1024;
break;
case 's':
case 'i':
eprintf("not implemented\n");
default:
usage();
} ARGEND;
2014-10-18 17:15:26 +00:00
if (hflag)
printf("Filesystem Size Used "
"Avail Capacity Mounted on\n");
else
printf("Filesystem %ld-blocks Used "
"Avail Capacity Mounted on\n", blksize);
fp = setmntent("/proc/mounts", "r");
if (!fp)
eprintf("setmntent %s:", "/proc/mounts");
2014-03-18 14:19:56 +00:00
while ((me = getmntent(fp)) != NULL) {
if (aflag == 0)
if (strcmp(me->mnt_type, "rootfs") == 0)
continue;
mnt_show(me->mnt_fsname, me->mnt_dir);
2014-03-18 14:19:56 +00:00
}
endmntent(fp);
2014-10-02 22:45:25 +00:00
return 0;
}
2014-10-18 17:15:26 +00:00
#define CALC_POWER(n, power, base, i) do { \
while (n > power) { \
power = power * base; \
i++; \
} \
} while(0)
static void
print_human(
const char *fsname,
unsigned long long total,
unsigned long long used,
unsigned long long avail,
int capacity,
const char *dir)
{
long base = 1024;
unsigned long long power_total = base;
unsigned long long power_used = base;
unsigned long long power_avail = base;
char postfixes[] = {'B', 'K', 'M', 'G', 'T', 'P', 'E'};
int i = 0, j = 0, k = 0;
total = total * blksize;
used = used * blksize;
avail = avail * blksize;
CALC_POWER(total, power_total, base, i);
CALC_POWER(used, power_used, base, j);
CALC_POWER(avail, power_avail, base, k);
total = i ? total / (power_total / base) : total;
used = j ? used / (power_used / base) : used;
avail = k ? avail / (power_avail / base) : avail;
printf("%-12s %9llu%c %9llu%c %9llu%c %7d%% %s\n",
fsname, total, postfixes[i], used, postfixes[j],
avail, postfixes[k], capacity, dir);
}
static void
mnt_show(const char *fsname, const char *dir)
{
struct statvfs s;
unsigned long long total, used, avail;
int capacity = 0;
int bs;
statvfs(dir, &s);
2014-10-18 17:15:26 +00:00
bs = s.f_frsize / blksize;
total = s.f_blocks * bs;
avail = s.f_bfree * bs;
used = total - avail;
if (used + avail) {
capacity = (used * 100) / (used + avail);
if (used * 100 != capacity * (used + avail))
capacity++;
}
2014-10-18 17:15:26 +00:00
if (hflag)
print_human(fsname, total, used, avail, capacity, dir);
else
printf("%-12s %9llu %9llu %9llu %7d%% %s\n",
fsname, total, used, avail, capacity, dir);
}