2009-03-04 11:32:55 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
|
|
|
|
static int ok = EXIT_SUCCESS;
|
|
|
|
|
2010-09-12 09:19:35 +08:00
|
|
|
static void printsize(long long n)
|
|
|
|
{
|
|
|
|
char unit = 'K';
|
2011-07-14 21:20:05 +08:00
|
|
|
long long t;
|
|
|
|
|
|
|
|
n *= 10;
|
|
|
|
|
|
|
|
if (n > 1024*1024*10) {
|
2010-09-12 09:19:35 +08:00
|
|
|
n /= 1024;
|
|
|
|
unit = 'M';
|
|
|
|
}
|
2011-07-14 21:20:05 +08:00
|
|
|
|
|
|
|
if (n > 1024*1024*10) {
|
2010-09-12 09:19:35 +08:00
|
|
|
n /= 1024;
|
|
|
|
unit = 'G';
|
|
|
|
}
|
2011-07-14 21:20:05 +08:00
|
|
|
|
|
|
|
t = (n + 512) / 1024;
|
|
|
|
printf("%4lld.%1lld%c", t/10, t%10, unit);
|
2010-09-12 09:19:35 +08:00
|
|
|
}
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
static void df(char *s, int always) {
|
|
|
|
struct statfs st;
|
|
|
|
|
|
|
|
if (statfs(s, &st) < 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", s, strerror(errno));
|
|
|
|
ok = EXIT_FAILURE;
|
|
|
|
} else {
|
|
|
|
if (st.f_blocks == 0 && !always)
|
2010-09-12 09:19:35 +08:00
|
|
|
return;
|
|
|
|
printf("%-20s ", s);
|
|
|
|
printsize((long long)st.f_blocks * (long long)st.f_bsize);
|
|
|
|
printf(" ");
|
|
|
|
printsize((long long)(st.f_blocks - (long long)st.f_bfree) * st.f_bsize);
|
|
|
|
printf(" ");
|
|
|
|
printsize((long long)st.f_bfree * (long long)st.f_bsize);
|
|
|
|
printf(" %d\n", (int) st.f_bsize);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int df_main(int argc, char *argv[]) {
|
2011-07-14 21:20:05 +08:00
|
|
|
printf("Filesystem Size Used Free Blksize\n");
|
2009-03-04 11:32:55 +08:00
|
|
|
if (argc == 1) {
|
|
|
|
char s[2000];
|
|
|
|
FILE *f = fopen("/proc/mounts", "r");
|
|
|
|
|
|
|
|
while (fgets(s, 2000, f)) {
|
|
|
|
char *c, *e = s;
|
|
|
|
|
|
|
|
for (c = s; *c; c++) {
|
|
|
|
if (*c == ' ') {
|
|
|
|
e = c + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (c = e; *c; c++) {
|
|
|
|
if (*c == ' ') {
|
|
|
|
*c = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
df(e, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
df(argv[i], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(ok);
|
|
|
|
}
|