Xinqi Bao's Git

swap_perc: check for division by zero on obsd too
[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 warn("statvfs '%s':", mnt);
16 return NULL;
17 }
18
19 return fmt_human_2(fs.f_frsize * fs.f_bavail, "B");
20 }
21
22 const char *
23 disk_perc(const char *mnt)
24 {
25 struct statvfs fs;
26
27 if (statvfs(mnt, &fs) < 0) {
28 warn("statvfs '%s':", mnt);
29 return NULL;
30 }
31
32 return bprintf("%d%%", (int)(100 *
33 (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks))));
34 }
35
36 const char *
37 disk_total(const char *mnt)
38 {
39 struct statvfs fs;
40
41 if (statvfs(mnt, &fs) < 0) {
42 warn("statvfs '%s':", mnt);
43 return NULL;
44 }
45
46 return fmt_human_2(fs.f_frsize * fs.f_blocks, "B");
47 }
48
49 const char *
50 disk_used(const char *mnt)
51 {
52 struct statvfs fs;
53
54 if (statvfs(mnt, &fs) < 0) {
55 warn("statvfs '%s':", mnt);
56 return NULL;
57 }
58
59 return fmt_human_2(fs.f_frsize * (fs.f_blocks - fs.f_bfree), "B");
60 }