+ va_list ap;
+ char tmp[120];
+ char *ret = NULL;
+
+ va_start(ap, fmt);
+ vsnprintf(tmp, sizeof(tmp)-1, fmt, ap);
+ tmp[strlen(tmp)+1] = '\0';
+
+ if (asprintf(&ret, "%s", tmp) < 0)
+ return NULL;
+
+ va_end(ap);
+ return ret;
+}
+
+static char *
+battery_perc(const char *battery)
+{
+ int now, full, perc;
+ char batterynowfile[64];
+ char batteryfullfile[64];
+ FILE *fp;
+
+ strlcpy(batterynowfile, BATTERY_PATH, sizeof(batterynowfile));
+ strlcat(batterynowfile, battery, sizeof(batterynowfile));
+ strlcat(batterynowfile, "/", sizeof(batterynowfile));
+ strlcat(batterynowfile, BATTERY_NOW, sizeof(batterynowfile));
+
+ strlcpy(batteryfullfile, BATTERY_PATH, sizeof(batteryfullfile));
+ strlcat(batteryfullfile, battery, sizeof(batteryfullfile));
+ strlcat(batteryfullfile, "/", sizeof(batteryfullfile));
+ strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile));
+
+ if (!(fp = fopen(batterynowfile, "r"))) {
+ fprintf(stderr, "Error opening battery file: %s.\n", batterynowfile);
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fscanf(fp, "%i", &now);
+ fclose(fp);
+
+ if (!(fp = fopen(batteryfullfile, "r"))) {
+ fprintf(stderr, "Error opening battery file.\n");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ 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(UNKNOWN_STR);
+ }
+
+ 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(UNKNOWN_STR);
+ }
+
+ 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 t;
+ char timestr[80];
+
+ t = time(NULL);
+ if (strftime(timestr, sizeof(timestr), timeformat, localtime(&t)) == 0)
+ return smprintf(UNKNOWN_STR);
+
+ return smprintf("%s", timestr);
+}
+
+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(UNKNOWN_STR);
+ }
+ return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
+}
+
+static char *
+disk_perc(const char *mountpoint)
+{
+ int perc = 0;
+ struct statvfs fs;
+
+ if (statvfs(mountpoint, &fs) < 0) {
+ fprintf(stderr, "Could not get filesystem info.\n");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
+ return smprintf("%d%%", perc);
+}
+
+static char *
+disk_total(const char *mountpoint)
+{
+ struct statvfs fs;