+#include "config.h"
+
+static void
+setstatus(const char *str)
+{
+ /* set WM_NAME via X11 */
+ XStoreName(dpy, DefaultRootWindow(dpy), str);
+ XSync(dpy, False);
+}
+
+static char *
+smprintf(const char *fmt, ...)
+{
+ va_list fmtargs;
+ char *ret = NULL;
+
+ va_start(fmtargs, fmt);
+ if (vasprintf(&ret, fmt, fmtargs) < 0)
+ return NULL;
+ va_end(fmtargs);
+
+ return ret;
+}
+
+static char *
+battery_perc(const char *battery)
+{
+ int now, full, perc;
+ char batterynowfile[64] = "";
+ char batteryfullfile[64] = "";
+ FILE *fp;
+
+ strlcat(batterynowfile, batterypath, sizeof(batterynowfile));
+ strlcat(batterynowfile, battery, sizeof(batterynowfile));
+ strlcat(batterynowfile, "/", sizeof(batterynowfile));
+ strlcat(batterynowfile, batterynow, sizeof(batterynowfile));
+
+ strlcat(batteryfullfile, batterypath, sizeof(batteryfullfile));
+ strlcat(batteryfullfile, battery, sizeof(batteryfullfile));
+ strlcat(batteryfullfile, "/", sizeof(batteryfullfile));
+ strlcat(batteryfullfile, batteryfull, sizeof(batteryfullfile));
+
+ if (!(fp = fopen(batterynowfile, "r"))) {
+ fprintf(stderr, "Error opening battery file: %s.\n", batterynowfile);
+ return smprintf(unknowntext);
+ }
+
+ fscanf(fp, "%i", &now);
+ fclose(fp);
+
+ if (!(fp = fopen(batteryfullfile, "r"))) {
+ fprintf(stderr, "Error opening battery file.\n");
+ return smprintf(unknowntext);
+ }
+
+ fscanf(fp, "%i", &full);
+ fclose(fp);
+
+ perc = now / (full / 100);
+
+ return smprintf("%d%%", perc);
+}
+
+static char *
+cpu_perc(void)
+{
+ int perc;
+ long double a[4], b[4];
+ FILE *fp;
+
+ if (!(fp = fopen("/proc/stat","r"))) {
+ fprintf(stderr, "Error opening stat file.\n");
+ return smprintf(unknowntext);
+ }
+
+ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
+ fclose(fp);
+
+ /* wait a second (for avg values) */
+ sleep(1);
+
+ if (!(fp = fopen("/proc/stat","r"))) {
+ fprintf(stderr, "Error opening stat file.\n");
+ return smprintf(unknowntext);
+ }
+
+ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
+ fclose(fp);
+ perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
+ return smprintf("%d%%", perc);
+}
+
+static char *
+datetime(const char *timeformat)
+{
+ time_t tm;
+ size_t bufsize = 64;
+ char *buf = malloc(bufsize);
+ if (buf == NULL) {
+ fprintf(stderr, "Failed to get date/time.\n");
+ return smprintf(unknowntext);
+ }
+
+ time(&tm);
+ setlocale(LC_TIME, "");
+ if (!strftime(buf, bufsize, timeformat, localtime(&tm))) {
+ setlocale(LC_TIME, "C");
+ free(buf);
+ fprintf(stderr, "Strftime failed.\n");
+ return smprintf(unknowntext);
+ }
+
+ setlocale(LC_TIME, "C");
+ char *ret = smprintf("%s", buf);
+ free(buf);
+ return ret;
+}
+
+static char *
+disk_free(const char *mountpoint)
+{
+ struct statvfs fs;
+
+ if (statvfs(mountpoint, &fs) < 0) {
+ fprintf(stderr, "Could not get filesystem info.\n");
+ return smprintf(unknowntext);
+ }
+ return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
+}
+
+static char *
+disk_perc(const char *mountpoint)