Xinqi Bao's Git

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