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 printsize(long long n) 10 { 11 char unit = 'K'; 12 long long t; 13 14 n *= 10; 15 16 if (n > 1024*1024*10) { 17 n /= 1024; 18 unit = 'M'; 19 } 20 21 if (n > 1024*1024*10) { 22 n /= 1024; 23 unit = 'G'; 24 } 25 26 t = (n + 512) / 1024; 27 printf("%4lld.%1lld%c", t/10, t%10, unit); 28 } 29 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) 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); 46 } 47 } 48 49 int df_main(int argc, char *argv[]) { 50 printf("Filesystem Size Used Free Blksize\n"); 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 } 86