summaryrefslogtreecommitdiff
path: root/src/get_load.c
diff options
context:
space:
mode:
authorMichael Stapelberg <michael@stapelberg.de>2009-07-22 00:09:32 +0200
committerMichael Stapelberg <michael@stapelberg.de>2009-07-22 00:09:32 +0200
commitbb50e22fd3ad82381dbf750a790b8380a1a1bba5 (patch)
tree0d9dbad757dd38afebcc70dfed8598d19346ce2d /src/get_load.c
parentbd3f63428aeb9cc4de5a54f1b8723105fb82e6d6 (diff)
Use getloadavg() instead of using /proc, patch by Baptiste Daroussin
Diffstat (limited to 'src/get_load.c')
-rw-r--r--src/get_load.c24
1 files changed, 10 insertions, 14 deletions
diff --git a/src/get_load.c b/src/get_load.c
index 4f82813..4f04b94 100644
--- a/src/get_load.c
+++ b/src/get_load.c
@@ -1,25 +1,21 @@
// vim:ts=8:expandtab
#include "i3status.h"
+#include <err.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
const char *get_load() {
static char part[512];
/* Get load */
-#ifdef LINUX
- slurp("/proc/loadavg", part, sizeof(part));
- *skip_character(part, ' ', 3) = '\0';
+#if defined(__FreeBSD__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun)
+ double loadavg[3];
+ if (getloadavg(loadavg, 3) == -1)
+ errx(-1, "getloadavg() failed\n");
+ (void)snprintf(part, sizeof(part), "%1.2f %1.2f %1.2f", loadavg[0], loadavg[1], loadavg[2]);
#else
- /* TODO: correctly check for NetBSD, check if it works the same on *BSD */
- struct loadavg load;
- size_t length = sizeof(struct loadavg);
- int mib[2] = { CTL_VM, VM_LOADAVG };
- if (sysctl(mib, 2, &load, &length, NULL, 0) < 0)
- die("Could not sysctl({ CTL_VM, VM_LOADAVG })\n");
- double scale = load.fscale;
- (void)snprintf(part, sizeof(part), "%.02f %.02f %.02f",
- (double)load.ldavg[0] / scale,
- (double)load.ldavg[1] / scale,
- (double)load.ldavg[2] / scale);
+ part[0] = '\0';
#endif
return part;