2013-08-06 11:16:27 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-06-30 18:03:41 +00:00
|
|
|
#include <sys/statvfs.h>
|
|
|
|
|
2014-02-15 18:24:07 +00:00
|
|
|
#include <mntent.h>
|
2013-08-06 11:16:27 +00:00
|
|
|
#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
|
|
|
|
2013-08-06 11:16:27 +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;
|
|
|
|
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2015-01-26 18:53:14 +00:00
|
|
|
static int
|
2013-08-06 11:16:27 +00:00
|
|
|
mnt_show(const char *fsname, const char *dir)
|
|
|
|
{
|
|
|
|
struct statvfs s;
|
|
|
|
unsigned long long total, used, avail;
|
|
|
|
int capacity = 0;
|
|
|
|
int bs;
|
|
|
|
|
2016-02-16 15:19:25 +00:00
|
|
|
if (statvfs(dir, &s) < 0)
|
2015-01-26 18:53:14 +00:00
|
|
|
return -1;
|
2013-08-06 11:16:27 +00:00
|
|
|
|
2014-10-18 17:15:26 +00:00
|
|
|
bs = s.f_frsize / blksize;
|
2013-08-06 11:16:27 +00:00
|
|
|
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);
|
2015-01-26 18:53:14 +00:00
|
|
|
|
|
|
|
return 0;
|
2013-08-06 11:16:27 +00:00
|
|
|
}
|
2015-02-06 14:19:33 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-a]\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct mntent *me = NULL;
|
|
|
|
FILE *fp;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
hflag = 1;
|
|
|
|
kflag = 0;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
kflag = 1;
|
|
|
|
hflag = 0;
|
|
|
|
blksize = 1024;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
case 'i':
|
|
|
|
eprintf("not implemented\n");
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
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");
|
|
|
|
while ((me = getmntent(fp)) != NULL) {
|
|
|
|
if (aflag == 0)
|
|
|
|
if (strcmp(me->mnt_type, "rootfs") == 0)
|
|
|
|
continue;
|
|
|
|
if (mnt_show(me->mnt_fsname, me->mnt_dir) < 0)
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
endmntent(fp);
|
|
|
|
|
|
|
|
return ret;
|
2016-02-16 15:19:25 +00:00
|
|
|
}
|