$NetBSD$

--- netbsd.c.orig	2004-08-25 19:55:57.000000000 +0200
+++ netbsd.c
@@ -9,6 +9,7 @@
 #include <err.h>
 #include <limits.h>
 #include <paths.h>
+#include <ctype.h>
 
 #include <kvm.h>
 #include <nlist.h>
@@ -221,6 +222,33 @@ void update_net_stats() 
     }
 }
 
+static char *freq = NULL;
+char * get_freq()
+{
+    size_t val_len;
+    u_int32_t val;
+
+    if (freq == NULL) {
+        freq = malloc(10);
+        if (freq == NULL) {
+            return NULL;
+        } else {
+            freq[0] = '\0';
+        }
+    }
+
+    /* Laptops with enhanced speed step. */
+    if (sysctlbyname("machdep.est.frequency.current",
+                     NULL, &val_len, NULL, 0) == 0) {
+        sysctlbyname("machdep.est.frequency.current", &val, &val_len, NULL, 0);
+        sprintf(freq, "%d", val);
+    } else {
+        /* XXX: Parse out the clockspeed from machdep.cpu_brand */
+    }
+
+    return freq;
+}
+
 void update_total_processes() 
 {
     /* It's easier to use kvm here than sysctl */
@@ -302,6 +330,7 @@ void update_cpu_usage() 
 }
 
 double get_i2c_info(int fd, int div) {
+    (void) fd; (void) div;
     return -1;
 }
 
@@ -315,24 +344,96 @@ void update_load_average() {
 }
 
 double get_acpi_temperature(int fd) {
+    (void) fd;
     return -1;
 }
 
-void get_battery_stuff(char *buf, unsigned int n, const char *bat) {
-}
-
 int open_i2c_sensor(const char *dev, const char *type, int n, int *div)
 {
+    (void) dev; (void) type; (void) n; (void) div;
     return -1;
 }
 
 int open_acpi_temperature(const char *name) {
+    (void) name;
     return -1;
 }
 
+static char acpi_ac_str[64] = "N/A";
 char * get_acpi_ac_adapter(void)
 {
-    return "N/A";
+    /* get_battery_stuff() actually populates this for us. */
+    return acpi_ac_str;
+}
+
+static char last_battery_str[64];
+static double last_battery_time;
+
+void get_battery_stuff(char *buf, unsigned int n, const char *bat) {
+    FILE *f;
+    char *foo;
+    char b[4096];
+    char b_name[32];
+    int bat_num;
+    int found_acpibat = 0;
+    int found_acpiacad = 0;
+    float bat_charge = 0.0;
+
+    /* Don't update battery too often. */
+    if (current_update_time - last_battery_time < 29.5) {
+        snprintf(buf, n, "%s", last_battery_str);
+        return;
+    }
+    last_battery_time = current_update_time;
+
+    sscanf(bat, "BAT%d", &bat_num);
+    sprintf(b_name, "acpibat%d", bat_num);
+
+    /*
+     * Using the envsys API is like pulling teeth without anesthetic.
+     * so just popen envstat and parse the output instead.
+     */
+    f = popen("/usr/sbin/envstat -r", "r");
+    if (f != NULL) {
+        while(!feof(f)) {
+            fgets(b, 4096, f);
+
+            /* AC adapter state. */
+            if (strstr(b, "acpiacad0")) {
+                if (strstr(b, "disconnected")) {
+                    sprintf(acpi_ac_str, "disconnected");
+                } else {
+                    sprintf(acpi_ac_str, "connected");
+                }
+                found_acpiacad = 1;
+            }
+
+            if (bat && strstr(b, b_name)) {
+                if (strstr(b, "charge:")) {
+                    foo = strstr(b, "%)");
+                    while (*foo != ' ' && *foo != '(') {
+                        foo--;
+                    }
+                    foo = foo + 1;
+                    sscanf(foo, "%f", &bat_charge);
+                    found_acpibat = 1;
+                }
+            }
+        }
+        pclose(f);
+    }
+
+    if (found_acpibat) {
+        snprintf(last_battery_str, 64, "%.2f%%", bat_charge);
+        snprintf(buf, n, "%s", last_battery_str);
+    } else {
+        /* XXX: If that failed, try to use APM. */
+    }
+
+    if (!found_acpiacad) {
+        sprintf(acpi_ac_str, "N/A");
+    }
+
 }
 
 char* get_acpi_fan() {
