| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | #include <stdio.h> | 
|  | 2 | #include <stdlib.h> | 
|  | 3 | #include <string.h> | 
|  | 4 | #include <errno.h> | 
|  | 5 | #include <sys/statfs.h> | 
|  | 6 |  | 
|  | 7 | static int ok = EXIT_SUCCESS; | 
|  | 8 |  | 
| Brian Swetland | 09dd3e5 | 2010-09-11 18:19:35 -0700 | [diff] [blame] | 9 | static void printsize(long long n) | 
|  | 10 | { | 
|  | 11 | char unit = 'K'; | 
| Michal Frynas | 1f90dcd | 2011-07-14 15:20:05 +0200 | [diff] [blame] | 12 | long long t; | 
|  | 13 |  | 
|  | 14 | n *= 10; | 
|  | 15 |  | 
|  | 16 | if (n > 1024*1024*10) { | 
| Brian Swetland | 09dd3e5 | 2010-09-11 18:19:35 -0700 | [diff] [blame] | 17 | n /= 1024; | 
|  | 18 | unit = 'M'; | 
|  | 19 | } | 
| Michal Frynas | 1f90dcd | 2011-07-14 15:20:05 +0200 | [diff] [blame] | 20 |  | 
|  | 21 | if (n > 1024*1024*10) { | 
| Brian Swetland | 09dd3e5 | 2010-09-11 18:19:35 -0700 | [diff] [blame] | 22 | n /= 1024; | 
|  | 23 | unit = 'G'; | 
|  | 24 | } | 
| Michal Frynas | 1f90dcd | 2011-07-14 15:20:05 +0200 | [diff] [blame] | 25 |  | 
|  | 26 | t = (n + 512) / 1024; | 
|  | 27 | printf("%4lld.%1lld%c", t/10, t%10, unit); | 
| Brian Swetland | 09dd3e5 | 2010-09-11 18:19:35 -0700 | [diff] [blame] | 28 | } | 
|  | 29 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | static 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 Swetland | 09dd3e5 | 2010-09-11 18:19:35 -0700 | [diff] [blame] | 38 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | } | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | int df_main(int argc, char *argv[]) { | 
| Michal Frynas | 1f90dcd | 2011-07-14 15:20:05 +0200 | [diff] [blame] | 50 | printf("Filesystem               Size     Used     Free   Blksize\n"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | 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 | } |