+ va_list ap;
+ char *ret;
+ int len;
+
+ va_start(ap, fmt);
+ len = vsnprintf(NULL, 0, fmt, ap);
+ va_end(ap);
+
+ ret = malloc(++len);
+ if (ret == NULL) {
+ perror("malloc");
+ exit(1);
+ }
+
+ va_start(ap, fmt);
+ vsnprintf(ret, len, fmt, ap);
+ va_end(ap);
+
+ return ret;
+}
+
+static char *
+battery_perc(const char *battery)
+{
+ int now, full, perc;
+ char batterynowfile[64];
+ char batteryfullfile[64];
+ FILE *fp = fopen(batterynowfile, "r");
+
+ 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 == NULL ) {
+ fprintf(stderr, "Error opening battery file: %s: %s\n",
+ batterynowfile,
+ strerror(errno));
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fscanf(fp, "%i", &now);
+ fclose(fp);
+
+ fp = fopen(batteryfullfile, "r");
+ if (fp == NULL) {
+ fprintf(stderr, "Error opening battery file: %s\n",
+ strerror(errno));
+ 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 = fopen("/proc/stat","r");
+
+ if (fp == NULL) {
+ fprintf(stderr, "Error opening stat file: %s\n",
+ strerror(errno));
+ 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);
+
+ fp = fopen("/proc/stat","r");
+ if (fp == NULL) {
+ fprintf(stderr, "Error opening stat file: %s\n",
+ strerror(errno));
+ 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: %s\n",
+ strerror(errno));
+ 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: %s\n",
+ strerror(errno));
+ 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;