1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
// vim:ts=8:expandtab
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (__OpenBSD__) || defined(__DragonFly__)
#include <sys/param.h>
#include <sys/mount.h>
#endif
#include <yajl/yajl_gen.h>
#include <yajl/yajl_version.h>
#include "i3status.h"
#define BINARY_BASE UINT64_C(1024)
#define DECIMAL_BASE UINT64_C(1000)
#define MAX_EXPONENT 4
static const char * const iec_symbols[MAX_EXPONENT+1] = {"", "Ki", "Mi", "Gi", "Ti"};
static const char * const si_symbols[MAX_EXPONENT+1] = {"", "k", "M", "G", "T"};
static const char * const custom_symbols[MAX_EXPONENT+1] = {"", "K", "M", "G", "T"};
/*
* Formats bytes according to the given base and set of symbols.
*
*/
static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char * const symbols[]) {
double size = bytes;
int exponent = 0;
while (size >= base && exponent < MAX_EXPONENT) {
size /= base;
exponent += 1;
}
return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
}
/*
* Prints the given amount of bytes in a human readable manner.
*
*/
static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
if (strncmp(prefix_type, "decimal", strlen(prefix_type)) == 0) {
return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
} else if (strncmp(prefix_type, "custom", strlen(prefix_type)) == 0) {
return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
} else {
return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
}
}
/*
* Does a statvfs and prints either free, used or total amounts of bytes in a
* human readable manner.
*
*/
void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type) {
const char *walk;
char *outwalk = buffer;
INSTANCE(path);
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
struct statfs buf;
if (statfs(path, &buf) == -1)
return;
#else
struct statvfs buf;
if (statvfs(path, &buf) == -1)
return;
#endif
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk+1, "free")) {
outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
walk += strlen("free");
}
if (BEGINS_WITH(walk+1, "used")) {
outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
walk += strlen("used");
}
if (BEGINS_WITH(walk+1, "total")) {
outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
walk += strlen("total");
}
if (BEGINS_WITH(walk+1, "avail")) {
outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
walk += strlen("avail");
}
if (BEGINS_WITH(walk+1, "percentage_free")) {
outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks);
walk += strlen("percentage_free");
}
if (BEGINS_WITH(walk+1, "percentage_used_of_avail")) {
outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks);
walk += strlen("percentage_used_of_avail");
}
if (BEGINS_WITH(walk+1, "percentage_used")) {
outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks);
walk += strlen("percentage_used");
}
if (BEGINS_WITH(walk+1, "percentage_avail")) {
outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks);
walk += strlen("percentage_avail");
}
}
*outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
}
|