91 lines
2.0 KiB
C
91 lines
2.0 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
#include <mntent.h>
|
|
#include <sys/statvfs.h>
|
|
#include "human.h"
|
|
#include "config.h"
|
|
|
|
char a_flag;
|
|
char h_flag;
|
|
off_t block = 1024;
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int opt;
|
|
while ((opt = getopt(argc, argv, "hHka")) != -1) {
|
|
switch (opt) {
|
|
case 'h':
|
|
h_flag = 1;
|
|
break;
|
|
|
|
case 'H':
|
|
h_flag = 1;
|
|
block = 1000;
|
|
break;
|
|
|
|
case 'a':
|
|
a_flag = 1;
|
|
break;
|
|
|
|
default:
|
|
printf("df [rHa]\n\t-h Human readable (1024)\n\t-a Show all\n\t-H Human readable (1000)\n");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
FILE *fp = setmntent("/proc/mounts", "r");
|
|
if (fp == NULL) {
|
|
fprintf(stderr, "df: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
puts("Filesystem Size Used Available Use% Mounted on");
|
|
|
|
int ret = 0;
|
|
struct mntent *me;
|
|
struct statvfs disk;
|
|
|
|
while ((me = getmntent(fp)) != NULL) {
|
|
if (!strcmp(me->mnt_fsname, "none"))
|
|
continue;
|
|
|
|
if (statvfs(me->mnt_dir, &disk) < 0) {
|
|
ret = 1;
|
|
continue;
|
|
}
|
|
|
|
if (!a_flag)
|
|
if (!strcmp(me->mnt_fsname, "/dev/root") || disk.f_blocks == 0)
|
|
continue;
|
|
|
|
off_t bs = disk.f_frsize / block;
|
|
off_t total = disk.f_blocks * bs;
|
|
off_t avail = disk.f_bfree * bs;
|
|
off_t used = total - avail;
|
|
off_t capacity = (used * 100 + 1) / (avail + used + 1);
|
|
|
|
if (!h_flag)
|
|
printf("%-20s %12ju %12ju %12ju %12ju%% %s\n", me->mnt_fsname, total, used, avail, capacity, me->mnt_dir);
|
|
|
|
else {
|
|
char total_s[MU_HUMAN_BUF_SIZE + 1];
|
|
char used_s[MU_HUMAN_BUF_SIZE + 1];
|
|
char avail_s[MU_HUMAN_BUF_SIZE + 1];
|
|
|
|
snprintf(total_s, sizeof(total_s), "%s", mu_humansize(total * block, block));
|
|
snprintf(used_s, sizeof(used_s), "%s", mu_humansize(used * block, block));
|
|
snprintf(avail_s, sizeof(avail_s), "%s", mu_humansize(avail * block, block));
|
|
|
|
printf("%-20s %10s %9s %9s %3ju%% %s\n", me->mnt_fsname, total_s, used_s, avail_s, capacity, me->mnt_dir);
|
|
}
|
|
}
|
|
|
|
endmntent(fp);
|
|
return ret;
|
|
}
|
|
|