+static char *
+smprintf(const char *fmt, ...)
+{
+ 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) {
+ err(1, "malloc");
+ }
+
+ va_start(ap, fmt);
+ vsnprintf(ret, len, fmt, ap);
+ va_end(ap);
+
+ return ret;
+}
+
+static char *
+battery_perc(const char *bat)
+{
+ int perc;
+ char path[PATH_MAX];
+ FILE *fp;
+
+ snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/capacity");
+ fp = fopen(path, "r");
+ if (fp == NULL) {
+ warn("Failed to open file %s", path);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ fscanf(fp, "%i", &perc);
+ fclose(fp);
+
+ return smprintf("%d", perc);
+}
+
+static char *
+battery_power(const char *bat)
+{
+ char path[PATH_MAX];
+ FILE *fp;
+ int watts;
+
+ snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/power_now");
+ fp = fopen(path, "r");
+ if (fp == NULL) {
+ warn("Failed to open file %s", path);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ fscanf(fp, "%i", &watts);
+ fclose(fp);
+
+ return smprintf("%d", (watts + 500000) / 1000000);
+}
+
+static char *
+battery_state(const char *bat)
+{
+ char path[PATH_MAX];
+ char state[12];
+ FILE *fp;
+
+ snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status");
+ fp = fopen(path, "r");
+ if (fp == NULL) {
+ warn("Failed to open file %s", path);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ fscanf(fp, "%12s", state);
+ fclose(fp);
+
+ if (strcmp(state, "Charging") == 0) {
+ return smprintf("+");
+ } else if (strcmp(state, "Discharging") == 0) {
+ return smprintf("-");
+ } else if (strcmp(state, "Full") == 0) {
+ return smprintf("=");
+ } else if (strcmp(state, "Unknown") == 0) {
+ return smprintf("/");
+ } else {
+ return smprintf("?");
+ }
+}
+
+static char *
+cpu_freq(void)
+{
+ int freq;
+ FILE *fp;
+
+ fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ fscanf(fp, "%i", &freq);
+ fclose(fp);
+
+ return smprintf("%d", (freq + 500) / 1000);
+}
+
+static char *
+cpu_perc(void)
+{
+ int perc;
+ long double a[4], b[4];
+ FILE *fp;
+
+ fp = fopen("/proc/stat", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/stat");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
+ fclose(fp);
+
+ delay++;
+ sleep(delay);
+
+ fp = fopen("/proc/stat", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/stat");
+ return smprintf("%s", 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 *fmt)
+{
+ time_t t;
+ char str[80];
+
+ t = time(NULL);
+ if (strftime(str, sizeof(str), fmt, localtime(&t)) == 0) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ return smprintf("%s", str);
+}
+
+static char *
+disk_free(const char *mnt)
+{
+ struct statvfs fs;
+
+ if (statvfs(mnt, &fs) < 0) {
+ warn("Failed to get filesystem info");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
+}
+
+static char *
+disk_perc(const char *mnt)
+{
+ int perc;
+ struct statvfs fs;
+
+ if (statvfs(mnt, &fs) < 0) {
+ warn("Failed to get filesystem info");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
+
+ return smprintf("%d", perc);
+}
+
+static char *
+disk_total(const char *mnt)
+{
+ struct statvfs fs;
+
+ if (statvfs(mnt, &fs) < 0) {
+ warn("Failed to get filesystem info");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
+}
+
+static char *
+disk_used(const char *mnt)
+{
+ struct statvfs fs;
+
+ if (statvfs(mnt, &fs) < 0) {
+ warn("Failed to get filesystem info");
+ return smprintf("%s", UNKNOWN_STR);
+ }