mem.c (733B)
1 #include <stdio.h> 2 #include <errno.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <stdint.h> 6 #include "mem.h" 7 8 int mem_init(struct mem *m) 9 { 10 (void)m; 11 return 0; 12 } 13 14 int mem_poll(struct mem *m, struct json *j) 15 { 16 int r = 0; 17 char line[256]; 18 uintmax_t val; 19 FILE *f = fopen("/proc/meminfo", "r"); 20 21 (void)m; 22 23 if (!f) 24 return errno; 25 while (fgets(line, sizeof(line), f)) { 26 #define X(a, b) \ 27 if (!memcmp(line, #a ":", sizeof(#a ":") - 1)) { \ 28 if (sscanf(line + sizeof(#a ":") - 1, "%ju", &val) != 1) { \ 29 r = ENODATA; \ 30 goto out; \ 31 } \ 32 if ((r = json_obj_key(j, #b))) \ 33 goto out; \ 34 if ((r = json_int(j, val))) \ 35 goto out; \ 36 continue; \ 37 } 38 MEM_FIELDS(X) 39 #undef X 40 } 41 out: 42 fclose(f); 43 return 0; 44 }