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