disk.c (992B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <stdio.h> 3 #include <sys/statvfs.h> 4 5 #include "../util.h" 6 7 const char * 8 disk_free(const char *path) 9 { 10 struct statvfs fs; 11 12 if (statvfs(path, &fs) < 0) { 13 warn("statvfs '%s':", path); 14 return NULL; 15 } 16 17 return fmt_human(fs.f_frsize * fs.f_bavail, 1024); 18 } 19 20 const char * 21 disk_perc(const char *path) 22 { 23 struct statvfs fs; 24 25 if (statvfs(path, &fs) < 0) { 26 warn("statvfs '%s':", path); 27 return NULL; 28 } 29 30 return bprintf("%d", (int)(100 * 31 (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks)))); 32 } 33 34 const char * 35 disk_total(const char *path) 36 { 37 struct statvfs fs; 38 39 if (statvfs(path, &fs) < 0) { 40 warn("statvfs '%s':", path); 41 return NULL; 42 } 43 44 return fmt_human(fs.f_frsize * fs.f_blocks, 1024); 45 } 46 47 const char * 48 disk_used(const char *path) 49 { 50 struct statvfs fs; 51 52 if (statvfs(path, &fs) < 0) { 53 warn("statvfs '%s':", path); 54 return NULL; 55 } 56 57 return fmt_human(fs.f_frsize * (fs.f_blocks - fs.f_bfree), 1024); 58 }