+ 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);