Home | History | Annotate | Download | only in toolbox
      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 
      9 static void df(char *s, int always) {
     10     struct statfs st;
     11 
     12     if (statfs(s, &st) < 0) {
     13         fprintf(stderr, "%s: %s\n", s, strerror(errno));
     14         ok = EXIT_FAILURE;
     15     } else {
     16         if (st.f_blocks == 0 && !always)
     17             return;
     18 
     19         printf("%s: %lldK total, %lldK used, %lldK available (block size %d)\n",
     20                s,
     21                ((long long)st.f_blocks * (long long)st.f_bsize) / 1024,
     22                ((long long)(st.f_blocks - (long long)st.f_bfree) * st.f_bsize) / 1024,
     23                ((long long)st.f_bfree * (long long)st.f_bsize) / 1024,
     24                (int) st.f_bsize);
     25     }
     26 }
     27 
     28 int df_main(int argc, char *argv[]) {
     29     if (argc == 1) {
     30         char s[2000];
     31         FILE *f = fopen("/proc/mounts", "r");
     32 
     33         while (fgets(s, 2000, f)) {
     34             char *c, *e = s;
     35 
     36             for (c = s; *c; c++) {
     37                 if (*c == ' ') {
     38                     e = c + 1;
     39                     break;
     40                 }
     41             }
     42 
     43             for (c = e; *c; c++) {
     44                 if (*c == ' ') {
     45                     *c = '\0';
     46                     break;
     47                 }
     48             }
     49 
     50             df(e, 0);
     51         }
     52 
     53         fclose(f);
     54     } else {
     55         int i;
     56 
     57         for (i = 1; i < argc; i++) {
     58             df(argv[i], 1);
     59         }
     60     }
     61 
     62     exit(ok);
     63 }
     64