]> git.sur5r.net Git - i3/i3status/blob - src/print_load.c
fix: use SYSCONFDIR in error message
[i3/i3status] / src / print_load.c
1 // vim:ts=4:sw=4:expandtab
2 #include <config.h>
3 #include "i3status.h"
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <yajl/yajl_gen.h>
8 #include <yajl/yajl_version.h>
9
10 void print_load(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const float max_threshold) {
11     char *outwalk = buffer;
12 /* Get load */
13
14 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__)
15     double loadavg[3];
16     const char *selected_format = format;
17     const char *walk;
18     bool colorful_output = false;
19
20     if (getloadavg(loadavg, 3) == -1)
21         goto error;
22
23     if (loadavg[0] >= max_threshold) {
24         START_COLOR("color_bad");
25         colorful_output = true;
26         if (format_above_threshold != NULL)
27             selected_format = format_above_threshold;
28     }
29
30     for (walk = selected_format; *walk != '\0'; walk++) {
31         if (*walk != '%') {
32             *(outwalk++) = *walk;
33
34         } else if (BEGINS_WITH(walk + 1, "1min")) {
35             outwalk += sprintf(outwalk, "%1.2f", loadavg[0]);
36             walk += strlen("1min");
37
38         } else if (BEGINS_WITH(walk + 1, "5min")) {
39             outwalk += sprintf(outwalk, "%1.2f", loadavg[1]);
40             walk += strlen("5min");
41
42         } else if (BEGINS_WITH(walk + 1, "15min")) {
43             outwalk += sprintf(outwalk, "%1.2f", loadavg[2]);
44             walk += strlen("15min");
45
46         } else {
47             *(outwalk++) = '%';
48         }
49     }
50
51     if (colorful_output)
52         END_COLOR;
53
54     *outwalk = '\0';
55     OUTPUT_FULL_TEXT(buffer);
56
57     return;
58 error:
59 #endif
60     OUTPUT_FULL_TEXT("cant read load");
61     (void)fputs("i3status: Cannot read system load using getloadavg()\n", stderr);
62 }