1#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 9static 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 24static 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) 32 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); 40 } 41} 42 43int df_main(int argc, char *argv[]) { 44 printf("Filesystem Size Used Free Blksize\n"); 45 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} 80