ubase/df.c

77 lines
1.4 KiB
C
Raw Normal View History

/* See LICENSE file for copyright and license details. */
#include <mntent.h>
#include <stdio.h>
#include <stdlib.h>
2014-03-18 14:19:56 +00:00
#include <string.h>
#include <sys/statvfs.h>
#include "util.h"
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;
2014-03-18 14:19:56 +00:00
int aflag = 0;
ARGBEGIN {
case 'a':
2014-03-18 14:19:56 +00:00
aflag = 1;
break;
case 's':
case 'h':
case 'i':
eprintf("not implemented\n");
default:
usage();
} ARGEND;
printf("Filesystem 512-blocks Used Avail Capacity Mounted on\n");
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);
2013-10-07 18:11:40 +00:00
return EXIT_SUCCESS;
}
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);
bs = s.f_frsize / 512;
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++;
}
printf("%-12s %9llu %9llu %9llu %7d%% %s\n",
fsname, total, used, avail, capacity,
dir);
}