Xinqi Bao's Git

entropy: Port to OpenBSD
[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_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
21 }
22
23 const char *
24 disk_perc(const char *mnt)
25 {
26 int perc;
27 struct statvfs fs;
28
29 if (statvfs(mnt, &fs) < 0) {
30 fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
31 return NULL;
32 }
33
34 perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
35
36 return bprintf("%d", perc);
37 }
38
39 const char *
40 disk_total(const char *mnt)
41 {
42 struct statvfs fs;
43
44 if (statvfs(mnt, &fs) < 0) {
45 fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
46 return NULL;
47 }
48
49 return bprintf("%f",
50 (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
51 }
52
53 const char *
54 disk_used(const char *mnt)
55 {
56 struct statvfs fs;
57
58 if (statvfs(mnt, &fs) < 0) {
59 fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
60 return NULL;
61 }
62
63 return bprintf("%f",
64 (float)fs.f_bsize * ((float)fs.f_blocks -
65 (float)fs.f_bfree) / 1024 / 1024 / 1024);
66 }