Xinqi Bao's Git

cpu_perc: 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", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
20 }
21
22 const char *
23 disk_perc(const char *mnt)
24 {
25 int perc;
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 perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
34
35 return bprintf("%d", perc);
36 }
37
38 const char *
39 disk_total(const char *mnt)
40 {
41 struct statvfs fs;
42
43 if (statvfs(mnt, &fs) < 0) {
44 fprintf(stderr, "statvfs '%s': %s\n", mnt, strerror(errno));
45 return NULL;
46 }
47
48 return bprintf("%f", (float)fs.f_bsize * (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", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
62 }