Standardize battery.c
[dwm-status.git] / load.c
blob1d20b4e3a5ef0f6bc7e773bd2fd6fa4ccd69080e
1 /*-
2 * "THE BEER-WARE LICENSE" (Revision 42):
3 * <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
4 * you can do whatever you want with this stuff. If we meet some day, and you
5 * think this stuff is worth it, you can buy me a beer in return.
6 * Tobias Rehbein
7 */
9 #include <assert.h>
10 #include <err.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sysexits.h>
15 #include "buffers.h"
16 #include "load.h"
19 struct load_context {
20 char load_str[LOAD_BUFFLEN];
23 struct load_context *
24 load_context_open()
26 struct load_context *ctx;
28 if ((ctx = malloc(sizeof(*ctx))) == NULL)
29 err(EX_SOFTWARE, "malloc(%d) load_context", sizeof(*ctx));
31 return (ctx);
34 void
35 load_context_close(struct load_context *ctx)
37 assert(ctx != NULL);
39 free(ctx);
42 char *
43 load_str(struct load_context *ctx)
45 double la[3];
47 assert(ctx != NULL);
49 if (getloadavg(la, 3) == -1)
50 errx(EX_SOFTWARE, "getloadavg");
52 snprintf(ctx->load_str, sizeof(ctx->load_str), "%.2f %.2f %.2f", la[0], la[1], la[2]);
54 return (ctx->load_str);