Added README file.
[dwm-status] / dwmstatus.c
/*
* SGK version of dwmstatus by 20h.
*/
#define _BSD_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <X11/Xlib.h>
static Display *dpy;
char *
smprintf(char *fmt, ...)
{
va_list fmtargs;
char *ret;
int len;
va_start(fmtargs, fmt);
len = vsnprintf(NULL, 0, fmt, fmtargs);
va_end(fmtargs);
ret = malloc(++len);
if (ret == NULL) {
perror("malloc");
exit(1);
}
va_start(fmtargs, fmt);
vsnprintf(ret, len, fmt, fmtargs);
va_end(fmtargs);
return ret;
}
char *
mktimes(char *fmt, char *tzname)
{
char buf[129];
time_t tim;
struct tm *timtm;
setenv("TZ", tzname, 1);
tim = time(NULL);
timtm = localtime(&tim);
if (timtm == NULL)
return smprintf("");
if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) {
fprintf(stderr, "strftime == 0\n");
return smprintf("");
}
return smprintf("%s", buf);
}
void
setstatus(char *str)
{
XStoreName(dpy, DefaultRootWindow(dpy), str);
XSync(dpy, False);
}
char *
loadavg(void)
{
double avgs[3];
if (getloadavg(avgs, 3) < 0)
return smprintf("");
return smprintf("%.2f", avgs[0]);
}
int
main(void)
{
char *load, *datetime, *status;
if (!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "dwmstatus: cannot open display.\n");
return 1;
}
for (;;sleep(5)) {
load = loadavg();
datetime = mktimes("%a %d %b %Y ][ %H:%M", "America/Los_Angeles");
status = smprintf("[ %s ][ %s ]", load, datetime);
setstatus(status);
free(load);
free(datetime);
free(status);
}
XCloseDisplay(dpy);
return 0;
}