aboutsummaryrefslogtreecommitdiff
path: root/src/blocks/ram.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/blocks/ram.c')
-rw-r--r--src/blocks/ram.c47
1 files changed, 28 insertions, 19 deletions
diff --git a/src/blocks/ram.c b/src/blocks/ram.c
index 4400a59..780ff9e 100644
--- a/src/blocks/ram.c
+++ b/src/blocks/ram.c
@@ -7,15 +7,6 @@
#include "../any_log.h"
-static const char *ram_formats[] = {
- "%{total}",
- "%{free}",
- "%{used}",
- "%{free-percentage}",
- "%{used-percentage}",
- NULL,
-};
-
static void block_ram_update(block_t *block)
{
FILE *meminfo = fopen("/proc/meminfo", "rb");
@@ -31,15 +22,34 @@ static void block_ram_update(block_t *block)
&total, &unused, &available, &buffers, &cached));
fclose(meminfo);
- char ram_values[6][32] = { 0 };
-
- snprintf(ram_values[0], 32, "%ld", total);
- snprintf(ram_values[1], 32, "%ld", available);
- snprintf(ram_values[2], 32, "%ld", total - available);
- snprintf(ram_values[3], 32, "%ld", 100 * available / total);
- snprintf(ram_values[4], 32, "%ld", 100 * (total - available) / total);
- block->text.text = strformat(block->state, ram_formats, (const char **)ram_values);
+ static const char *ram_formats[] = {
+ "total",
+ "free",
+ "used",
+ "free-percentage",
+ "used-percentage",
+ NULL,
+ };
+
+ char buffer[32][5] = { 0 };
+ snprintf(buffer[0], 32, "%ld", total);
+ snprintf(buffer[1], 32, "%ld", available);
+ snprintf(buffer[2], 32, "%ld", total - available);
+ snprintf(buffer[3], 32, "%ld", 100 * available / total);
+ snprintf(buffer[4], 32, "%ld", 100 * (total - available) / total);
+
+ const char *ram_values[] = {
+ buffer[0],
+ buffer[1],
+ buffer[2],
+ buffer[3],
+ buffer[4],
+ NULL,
+ };
+
+
+ block->text.text = strformat(block->state, '%', ram_formats, ram_values);
assert(block->text.text != NULL);
}
@@ -58,8 +68,7 @@ static bool block_ram_validate(block_t *block, const block_scheme_t *scheme)
block->state = block->text.text;
block->text.text = NULL;
- size_t count = strcount(block->state, ram_formats);
- if (count == 0) {
+ if (strstr(block->state, "%{") == NULL) {
log_warn("Block '%s' does not use any ram variable", block->label);
block->update_cb = NULL;