blob: 63940a153087be0b63d1f3a657319e446ccdd89d [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <errno.h>
5#include <sys/statfs.h>
6
7static int ok = EXIT_SUCCESS;
8
Brian Swetland09dd3e52010-09-11 18:19:35 -07009static void printsize(long long n)
10{
11 char unit = 'K';
12 n /= 1024;
13 if (n > 1024) {
14 n /= 1024;
15 unit = 'M';
16 }
17 if (n > 1024) {
18 n /= 1024;
19 unit = 'G';
20 }
21 printf("%4lld%c", n, unit);
22}
23
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024static void df(char *s, int always) {
25 struct statfs st;
26
27 if (statfs(s, &st) < 0) {
28 fprintf(stderr, "%s: %s\n", s, strerror(errno));
29 ok = EXIT_FAILURE;
30 } else {
31 if (st.f_blocks == 0 && !always)
Brian Swetland09dd3e52010-09-11 18:19:35 -070032 return;
33 printf("%-20s ", s);
34 printsize((long long)st.f_blocks * (long long)st.f_bsize);
35 printf(" ");
36 printsize((long long)(st.f_blocks - (long long)st.f_bfree) * st.f_bsize);
37 printf(" ");
38 printsize((long long)st.f_bfree * (long long)st.f_bsize);
39 printf(" %d\n", (int) st.f_bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040 }
41}
42
43int df_main(int argc, char *argv[]) {
Brian Swetland09dd3e52010-09-11 18:19:35 -070044 printf("Filesystem Size Used Free Blksize\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045 if (argc == 1) {
46 char s[2000];
47 FILE *f = fopen("/proc/mounts", "r");
48
49 while (fgets(s, 2000, f)) {
50 char *c, *e = s;
51
52 for (c = s; *c; c++) {
53 if (*c == ' ') {
54 e = c + 1;
55 break;
56 }
57 }
58
59 for (c = e; *c; c++) {
60 if (*c == ' ') {
61 *c = '\0';
62 break;
63 }
64 }
65
66 df(e, 0);
67 }
68
69 fclose(f);
70 } else {
71 int i;
72
73 for (i = 1; i < argc; i++) {
74 df(argv[i], 1);
75 }
76 }
77
78 exit(ok);
79}