Xinqi Bao's Git

3d8140e64e9cb1c51169aef0fec82be9b528c6dc
[slstatus.git] / components / disk.c
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 *mnt)
9 {
10 struct statvfs fs;
11
12 if (statvfs(mnt, &fs) < 0) {
13 fprintf(stderr, "Failed to get filesystem info");
14 return NULL;
15 }
16
17 return bprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
18 }
19
20 const char *
21 disk_perc(const char *mnt)
22 {
23 int perc;
24 struct statvfs fs;
25
26 if (statvfs(mnt, &fs) < 0) {
27 fprintf(stderr, "Failed to get filesystem info");
28 return NULL;
29 }
30
31 perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
32
33 return bprintf("%d", perc);
34 }
35
36 const char *
37 disk_total(const char *mnt)
38 {
39 struct statvfs fs;
40
41 if (statvfs(mnt, &fs) < 0) {
42 fprintf(stderr, "Failed to get filesystem info");
43 return NULL;
44 }
45
46 return bprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
47 }
48
49 const char *
50 disk_used(const char *mnt)
51 {
52 struct statvfs fs;
53
54 if (statvfs(mnt, &fs) < 0) {
55 fprintf(stderr, "Failed to get filesystem info");
56 return NULL;
57 }
58
59 return bprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
60 }