Xinqi Bao's Git

Return actual percentage for wifi_perc()
[slstatus.git] / components / disk.c
1 /* See LICENSE file for copyright and license details. */
2 #include <err.h>
3 #include <stdio.h>
4 #include <sys/statvfs.h>
5
6 #include "../util.h"
7
8 const char *
9 disk_free(const char *mnt)
10 {
11 struct statvfs fs;
12
13 if (statvfs(mnt, &fs) < 0) {
14 warn("Failed to get filesystem info");
15 return NULL;
16 }
17
18 return bprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
19 }
20
21 const char *
22 disk_perc(const char *mnt)
23 {
24 int perc;
25 struct statvfs fs;
26
27 if (statvfs(mnt, &fs) < 0) {
28 warn("Failed to get filesystem info");
29 return NULL;
30 }
31
32 perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
33
34 return bprintf("%d", perc);
35 }
36
37 const char *
38 disk_total(const char *mnt)
39 {
40 struct statvfs fs;
41
42 if (statvfs(mnt, &fs) < 0) {
43 warn("Failed to get filesystem info");
44 return NULL;
45 }
46
47 return bprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
48 }
49
50 const char *
51 disk_used(const char *mnt)
52 {
53 struct statvfs fs;
54
55 if (statvfs(mnt, &fs) < 0) {
56 warn("Failed to get filesystem info");
57 return NULL;
58 }
59
60 return bprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
61 }