blob: 9cd07431a923a8652d5c8a2d8f5c35fe4d689448 [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';
Michal Frynas1f90dcd2011-07-14 15:20:05 +020012 long long t;
13
14 n *= 10;
15
16 if (n > 1024*1024*10) {
Brian Swetland09dd3e52010-09-11 18:19:35 -070017 n /= 1024;
18 unit = 'M';
19 }
Michal Frynas1f90dcd2011-07-14 15:20:05 +020020
21 if (n > 1024*1024*10) {
Brian Swetland09dd3e52010-09-11 18:19:35 -070022 n /= 1024;
23 unit = 'G';
24 }
Michal Frynas1f90dcd2011-07-14 15:20:05 +020025
26 t = (n + 512) / 1024;
27 printf("%4lld.%1lld%c", t/10, t%10, unit);
Brian Swetland09dd3e52010-09-11 18:19:35 -070028}
29
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030static void df(char *s, int always) {
31 struct statfs st;
32
33 if (statfs(s, &st) < 0) {
34 fprintf(stderr, "%s: %s\n", s, strerror(errno));
35 ok = EXIT_FAILURE;
36 } else {
37 if (st.f_blocks == 0 && !always)
Brian Swetland09dd3e52010-09-11 18:19:35 -070038 return;
39 printf("%-20s ", s);
40 printsize((long long)st.f_blocks * (long long)st.f_bsize);
41 printf(" ");
42 printsize((long long)(st.f_blocks - (long long)st.f_bfree) * st.f_bsize);
43 printf(" ");
44 printsize((long long)st.f_bfree * (long long)st.f_bsize);
45 printf(" %d\n", (int) st.f_bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046 }
47}
48
49int df_main(int argc, char *argv[]) {
Michal Frynas1f90dcd2011-07-14 15:20:05 +020050 printf("Filesystem Size Used Free Blksize\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051 if (argc == 1) {
52 char s[2000];
53 FILE *f = fopen("/proc/mounts", "r");
54
55 while (fgets(s, 2000, f)) {
56 char *c, *e = s;
57
58 for (c = s; *c; c++) {
59 if (*c == ' ') {
60 e = c + 1;
61 break;
62 }
63 }
64
65 for (c = e; *c; c++) {
66 if (*c == ' ') {
67 *c = '\0';
68 break;
69 }
70 }
71
72 df(e, 0);
73 }
74
75 fclose(f);
76 } else {
77 int i;
78
79 for (i = 1; i < argc; i++) {
80 df(argv[i], 1);
81 }
82 }
83
84 exit(ok);
85}