Fix a lot of unimportant memleaks caused by a CRIT_ERR
[monky] / src / freebsd.c
index 63d25a7..e1c7701 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Please see COPYING for details
  *
- * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
  *     (see AUTHORS)
  * All rights reserved.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
- * $Id$ */
+ */
 
+#include <sys/ioctl.h>
 #include <sys/dkstat.h>
 #include <sys/param.h>
 #include <sys/resource.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <sys/sysctl.h>
 #include <sys/time.h>
 #include <sys/types.h>
-#include <sys/vmmeter.h>
 #include <sys/user.h>
-#include <sys/ioctl.h>
 
 #include <net/if.h>
 #include <net/if_mib.h>
 #include <net/if_media.h>
 #include <net/if_var.h>
-#include <netinet/in.h>
 
 #include <devstat.h>
-#include <fcntl.h>
 #include <ifaddrs.h>
 #include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #include <unistd.h>
 
 #include <dev/wi/if_wavelan_ieee.h>
+#include <dev/acpica/acpiio.h>
 
 #include "conky.h"
+#include "freebsd.h"
+#include "logging.h"
+#include "top.h"
+#include "diskio.h"
 
 #define        GETSYSCTL(name, var)    getsysctl(name, &(var), sizeof(var))
 #define        KELVTOC(x)                              ((x - 2732) / 10.0)
 #define        FREEBSD_DEBUG
 #endif
 
-inline void proc_find_top(struct process **cpu, struct process **mem);
+__attribute__((gnu_inline)) inline void
+proc_find_top(struct process **cpu, struct process **mem);
 
-u_int64_t diskio_prev = 0;
 static short cpu_setup = 0;
-static short diskio_setup = 0;
 
-static int getsysctl(char *name, void *ptr, size_t len)
+static int getsysctl(const char *name, void *ptr, size_t len)
 {
        size_t nlen = len;
 
@@ -76,7 +75,7 @@ static int getsysctl(char *name, void *ptr, size_t len)
                return -1;
        }
 
-       if (nlen != len) {
+       if (nlen != len && errno == ENOMEM) {
                return -1;
        }
 
@@ -111,11 +110,11 @@ static int swapmode(unsigned long *retavail, unsigned long *retfree)
        return n;
 }
 
-void prepare_update()
+void prepare_update(void)
 {
 }
 
-void update_uptime()
+void update_uptime(void)
 {
        int mib[2] = { CTL_KERN, KERN_BOOTTIME };
        struct timeval boottime;
@@ -147,38 +146,41 @@ int check_mount(char *s)
        return 0;
 }
 
-void update_meminfo()
+void update_meminfo(void)
 {
-       unsigned long total_pages, inactive_pages, free_pages;
+       u_int total_pages, inactive_pages, free_pages;
        unsigned long swap_avail, swap_free;
 
        int pagesize = getpagesize();
 
        if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages)) {
-               fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
+               fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_page_count\"\n");
        }
 
        if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages)) {
-               fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
+               fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_free_count\"\n");
        }
 
        if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages)) {
-               fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
+               fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"\n");
        }
 
        info.memmax = total_pages * (pagesize >> 10);
        info.mem = (total_pages - free_pages - inactive_pages) * (pagesize >> 10);
+       info.memeasyfree = info.memfree = info.memmax - info.mem;
 
        if ((swapmode(&swap_avail, &swap_free)) >= 0) {
                info.swapmax = swap_avail;
                info.swap = (swap_avail - swap_free);
+               info.swapfree = swap_free;
        } else {
                info.swapmax = 0;
                info.swap = 0;
+               info.swapfree = 0;
        }
 }
 
-void update_net_stats()
+void update_net_stats(void)
 {
        struct net_stat *ns;
        double delta;
@@ -197,7 +199,7 @@ void update_net_stats()
        }
 
        for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
-               ns = get_net_stat((const char *) ifa->ifa_name);
+               ns = get_net_stat((const char *) ifa->ifa_name, NULL, NULL);
 
                if (ifa->ifa_flags & IFF_UP) {
                        struct ifaddrs *iftmp;
@@ -251,7 +253,7 @@ void update_net_stats()
        freeifaddrs(ifap);
 }
 
-void update_total_processes()
+void update_total_processes(void)
 {
        int n_processes;
 
@@ -260,7 +262,7 @@ void update_total_processes()
        info.procs = n_processes;
 }
 
-void update_running_processes()
+void update_running_processes(void)
 {
        struct kinfo_proc *p;
        int n_processes;
@@ -287,7 +289,7 @@ struct cpu_load_struct {
 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
 long cpu_used, oldtotal, oldused;
 
-void get_cpu_count()
+void get_cpu_count(void)
 {
        /* int cpu_count = 0; */
 
@@ -309,11 +311,11 @@ void get_cpu_count()
 }
 
 /* XXX: SMP support */
-void update_cpu_usage()
+void update_cpu_usage(void)
 {
        long used, total;
        long cp_time[CPUSTATES];
-       size_t len = sizeof(cp_time);
+       size_t cp_len = sizeof(cp_time);
 
        /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
        if ((cpu_setup == 0) || (!info.cpu_usage)) {
@@ -321,7 +323,7 @@ void update_cpu_usage()
                cpu_setup = 1;
        }
 
-       if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
+       if (sysctlbyname("kern.cp_time", &cp_time, &cp_len, NULL, 0) < 0) {
                fprintf(stderr, "Cannot get kern.cp_time");
        }
 
@@ -345,12 +347,7 @@ void update_cpu_usage()
        oldtotal = total;
 }
 
-double get_sysfs_info(int *fd, int arg, char *devtype, char *type)
-{
-       return 0.0;
-}
-
-void update_load_average()
+void update_load_average(void)
 {
        double v[3];
 
@@ -364,6 +361,7 @@ void update_load_average()
 double get_acpi_temperature(int fd)
 {
        int temp;
+       (void)fd;
 
        if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
                fprintf(stderr,
@@ -374,90 +372,107 @@ double get_acpi_temperature(int fd)
        return KELVTOC(temp);
 }
 
-void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
-{
-       int battime, batcapacity, batstate, ac;
-       char battery_status[64];
-       char battery_time[64];
-
-       if (GETSYSCTL("hw.acpi.battery.time", battime)) {
+static void get_battery_stats(int *battime, int *batcapacity, int *batstate, int *ac) {
+       if (battime && GETSYSCTL("hw.acpi.battery.time", *battime)) {
                fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.time\"\n");
        }
-       if (GETSYSCTL("hw.acpi.battery.life", batcapacity)) {
+       if (batcapacity && GETSYSCTL("hw.acpi.battery.life", *batcapacity)) {
                fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.life\"\n");
        }
-
-       if (GETSYSCTL("hw.acpi.battery.state", batstate)) {
+       if (batstate && GETSYSCTL("hw.acpi.battery.state", *batstate)) {
                fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.state\"\n");
        }
-
-       if (GETSYSCTL("hw.acpi.acline", ac)) {
+       if (ac && GETSYSCTL("hw.acpi.acline", *ac)) {
                fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
        }
+}
 
-       if (batstate == 1) {
-               if (battime != -1) {
-                       snprintf(battery_status, sizeof(battery_status) - 1,
-                               "remaining %d%%", batcapacity);
-                       snprintf(battery_time, sizeof(battery_time) - 1, "%d:%2.2d",
-                               battime / 60, battime % 60);
-                       /* snprintf(buf, n, "remaining %d%% (%d:%2.2d)", batcapacity,
-                               battime / 60, battime % 60); */
-               } else {
-                       /* no time estimate available yet */
-                       snprintf(battery_status, sizeof(battery_status) - 1,
-                               "remaining %d%%", batcapacity);
-               }
-               /* snprintf(buf, n, "remaining %d%%", batcapacity); */
-               if (ac == 1) {
-                       fprintf(stderr, "Discharging while on AC!\n");
-               }
-       } else {
-               snprintf(battery_status, sizeof(battery_status) - 1,
-                       batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
-               /* snprintf(buf, n,
-                       batstate == 2 ? "charging (%d%%)" : "charged (%d%%)",
-                       batcapacity); */
-               if (batstate != 2 && batstate != 0) {
-                       fprintf(stderr, "Unknown battery state %d!\n", batstate);
-               }
-               if (ac == 0) {
-                       fprintf(stderr, "Charging while not on AC!\n");
-               }
-       }
+void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
+{
+       int battime, batcapacity, batstate, ac;
+       (void)bat;
+
+       get_battery_stats(&battime, &batcapacity, &batstate, &ac);
+
+       if (batstate != 1 && batstate != 2 && batstate != 0 && batstate != 7)
+               fprintf(stderr, "Unknown battery state %d!\n", batstate);
+       else if (batstate != 1 && ac == 0)
+               fprintf(stderr, "Battery charging while not on AC!\n");
+       else if (batstate == 1 && ac == 1)
+               fprintf(stderr, "Battery discharing while on AC!\n");
 
        switch (item) {
-               case BATTERY_STATUS:
-                       snprintf(buf, n, "%s", battery_status);
-                       break;
                case BATTERY_TIME:
-                       snprintf(buf, n, "%s", battery_time);
+                       if (batstate == 1 && battime != -1)
+                               snprintf(buf, n, "%d:%2.2d", battime / 60, battime % 60);
                        break;
-               default:
+               case BATTERY_STATUS:
+                       if (batstate == 1) // Discharging
+                               snprintf(buf, n, "remaining %d%%", batcapacity);
+                       else
+                               snprintf(buf, n, batstate == 2 ? "charging (%d%%)" :
+                                               (batstate == 7 ? "absent/on AC" : "charged (%d%%)"),
+                                               batcapacity);
                        break;
+               default:
+                       fprintf(stderr, "Unknown requested battery stat %d\n", item);
        }
 }
 
-int get_battery_perct(const char *bat)
+static int check_bat(const char *bat)
 {
-       /* not implemented */
-       return 0;
+       int batnum, numbatts;
+       char *endptr;
+       if (GETSYSCTL("hw.acpi.battery.units", numbatts)) {
+               fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.units\"\n");
+               return -1;
+       }
+       if (numbatts <= 0) {
+               fprintf(stderr, "No battery unit detected\n");
+               return -1;
+       }
+       if (!bat || (batnum = strtol(bat, &endptr, 10)) < 0 ||
+                       bat == endptr || batnum > numbatts) {
+               fprintf(stderr, "Wrong battery unit %s requested\n", bat ? bat : "");
+               return -1;
+       }
+       return batnum;
 }
 
-int get_battery_perct_bar(const char *bar)
+int get_battery_perct(const char *bat)
 {
-       /* not implemented */
-       return 0;
+       union acpi_battery_ioctl_arg battio;
+       int batnum, acpifd;
+       int designcap, lastfulcap, batperct;
+
+       if ((battio.unit = batnum = check_bat(bat)) < 0)
+               return 0;
+       if ((acpifd = open("/dev/acpi", O_RDONLY)) < 0) {
+               fprintf(stderr, "Can't open ACPI device\n");
+               return 0;
+       }
+       if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1) {
+               fprintf(stderr, "Unable to get info for battery unit %d\n", batnum);
+               return 0;
+       }
+       close(acpifd);
+       designcap = battio.bif.dcap;
+       lastfulcap = battio.bif.lfcap;
+       batperct = (designcap > 0 && lastfulcap > 0) ?
+               (int) (((float) lastfulcap / designcap) * 100) : 0;
+       return batperct > 100 ? 100 : batperct;
 }
 
-int open_sysfs_sensor(const char *dir, const char *dev, const char *type,
-               int n, int *div, char *devtype)
+int get_battery_perct_bar(const char *bar)
 {
-       return 0;
+       int batperct = get_battery_perct(bar);
+       return (int)(batperct * 2.56 - 1);
 }
 
 int open_acpi_temperature(const char *name)
 {
+       (void)name;
+       /* Not applicable for FreeBSD. */
        return 0;
 }
 
@@ -508,7 +523,7 @@ void get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
 /* rdtsc() and get_freq_dynamic() copied from linux.c */
 
 #if  defined(__i386) || defined(__x86_64)
-__inline__ unsigned long long int rdtsc()
+__attribute__((gnu_inline)) inline unsigned long long int rdtsc(void)
 {
        unsigned long long int x;
 
@@ -519,7 +534,7 @@ __inline__ unsigned long long int rdtsc()
 
 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
-               char *p_format, int divisor)
+               const char *p_format, int divisor)
 {
 #if  defined(__i386) || defined(__x86_64)
        struct timezone tz;
@@ -549,7 +564,7 @@ void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
 }
 
 /* void */
-char get_freq(char *p_client_buffer, size_t client_buffer_size, char *p_format,
+char get_freq(char *p_client_buffer, size_t client_buffer_size, const char *p_format,
                int divisor, unsigned int cpu)
 {
        int freq;
@@ -578,13 +593,13 @@ char get_freq(char *p_client_buffer, size_t client_buffer_size, char *p_format,
        return 1;
 }
 
-void update_top()
+void update_top(void)
 {
        proc_find_top(info.cpu, info.memu);
 }
 
 #if 0
-void update_wifi_stats()
+void update_wifi_stats(void)
 {
        struct ifreq ifr;               /* interface stats */
        struct wi_req wireq;
@@ -599,7 +614,7 @@ void update_wifi_stats()
        }
 
        for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
-               ns = get_net_stat((const char *) ifa->ifa_name);
+               ns = get_net_stat((const char *) ifa->ifa_name, NULL, NULL);
 
                s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 
@@ -638,21 +653,24 @@ cleanup:
 }
 #endif
 
-void update_diskio()
+void update_diskio(void)
 {
-       int devs_count, num_selected, num_selections, i;
+       int devs_count, num_selected, num_selections, dn;
        struct device_selection *dev_select = NULL;
        long select_generation;
-       int dn;
        static struct statinfo statinfo_cur;
-       u_int64_t diskio_current = 0;
-       u_int64_t writes = 0;
+       char device_name[text_buffer_size];
+       struct diskio_stat *cur;
+       unsigned int reads, writes;
+       unsigned int total_reads = 0, total_writes = 0;
+
 
-       bzero(&statinfo_cur, sizeof(statinfo_cur));
-       statinfo_cur.dinfo = (struct devinfo *) malloc(sizeof(struct devinfo));
-       bzero(statinfo_cur.dinfo, sizeof(struct devinfo));
+       memset(&statinfo_cur, 0, sizeof(statinfo_cur));
+       statinfo_cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
+       stats.current = stats.current_read = stats.current_write = 0;
 
        if (devstat_getdevs(NULL, &statinfo_cur) < 0) {
+               free(statinfo_cur.dinfo);
                return;
        }
 
@@ -661,59 +679,29 @@ void update_diskio()
                        &select_generation, statinfo_cur.dinfo->generation,
                        statinfo_cur.dinfo->devices, devs_count, NULL, 0, NULL, 0,
                        DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
-               for (dn = 0; dn < devs_count; ++dn) {
+               for (dn = 0; dn < devs_count; dn++) {
                        int di;
                        struct devstat *dev;
 
                        di = dev_select[dn].position;
                        dev = &statinfo_cur.dinfo->devices[di];
-
-                       diskio_current += dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE];
-
-                       for (i = 0; i < MAX_DISKIO_STATS; i++) {
-                               if (diskio_stats[i].dev && strcmp(dev_select[dn].device_name,
-                                               diskio_stats[i].dev) == 0) {
-                                       diskio_stats[i].current = (dev->bytes[DEVSTAT_READ] +
-                                               dev->bytes[DEVSTAT_WRITE] - diskio_stats[i].last) / 1024;
-                                       diskio_stats[i].current_read = (dev->bytes[DEVSTAT_READ] -
-                                               diskio_stats[i].last_read) / 1024;
-                                       diskio_stats[i].current_write = (dev->bytes[DEVSTAT_WRITE] -
-                                               diskio_stats[i].last_write) / 1024;
-                                       if (dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE]
-                                                       < diskio_stats[i].last) {
-                                               diskio_stats[i].current = 0;
-                                       }
-                                       if (dev->bytes[DEVSTAT_READ] < diskio_stats[i].last_read) {
-                                               diskio_stats[i].current_read = 0;
-                                               diskio_stats[i].current = diskio_stats[i].current_write;
-                                       }
-                                       if (dev->bytes[DEVSTAT_WRITE] < diskio_stats[i].last_write) {
-                                               diskio_stats[i].current_write = 0;
-                                               diskio_stats[i].current = diskio_stats[i].current_read;
-                                       }
-                                       diskio_stats[i].last = dev->bytes[DEVSTAT_READ] +
-                                               dev->bytes[DEVSTAT_WRITE];
-                                       diskio_stats[i].last_read = dev->bytes[DEVSTAT_READ];
-                                       diskio_stats[i].last_write = dev->bytes[DEVSTAT_WRITE];
+                       snprintf(device_name, text_buffer_size, "%s%d",
+                                       dev_select[dn].device_name, dev_select[dn].unit_number);
+
+                       total_reads += (reads = dev->bytes[DEVSTAT_READ] / 512);
+                       total_writes += (writes = dev->bytes[DEVSTAT_WRITE] / 512);
+                       for (cur = stats.next; cur; cur = cur->next) {
+                               if (cur->dev && !strcmp(device_name, cur->dev)) {
+                                       update_diskio_values(cur, reads, writes);
+                                       break;
                                }
                        }
                }
+               update_diskio_values(&stats, total_reads, total_writes);
 
                free(dev_select);
        }
 
-       /* Since we return (diskio_total_current - diskio_total_old),
-        * the first frame will be way too high
-        * (it will be equal to diskio_total_current, i.e. all disk I/O since boot).
-        *  That's why it is better to return 0 first time; */
-       if (diskio_setup == 0) {
-               diskio_setup = 1;
-               diskio_value = 0;
-       } else {
-               diskio_value = (unsigned int) ((diskio_current - diskio_prev) / 1024);
-       }
-       diskio_prev = diskio_current;
-
        free(statinfo_cur.dinfo);
 }
 
@@ -721,9 +709,9 @@ void update_diskio()
 
 int comparecpu(const void *a, const void *b)
 {
-       if (((struct process *)a)->amount > ((struct process *)b)->amount) {
+       if (((const struct process *)a)->amount > ((const struct process *)b)->amount) {
                return -1;
-       } else if (((struct process *)a)->amount < ((struct process *)b)->amount) {
+       } else if (((const struct process *)a)->amount < ((const struct process *)b)->amount) {
                return 1;
        } else {
                return 0;
@@ -732,16 +720,17 @@ int comparecpu(const void *a, const void *b)
 
 int comparemem(const void *a, const void *b)
 {
-       if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem) {
+       if (((const struct process *)a)->totalmem > ((const struct process *)b)->totalmem) {
                return -1;
-       } else if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem) {
+       } else if (((const struct process *)a)->totalmem < ((const struct process *)b)->totalmem) {
                return 1;
        } else {
                return 0;
        }
 }
 
-inline void proc_find_top(struct process **cpu, struct process **mem)
+__attribute__((gnu_inline)) inline void
+proc_find_top(struct process **cpu, struct process **mem)
 {
        struct kinfo_proc *p;
        int n_processes;
@@ -838,10 +827,10 @@ int apm_getinfo(int fd, apm_info_t aip)
        return 0;
 }
 
-char *get_apm_adapter()
+char *get_apm_adapter(void)
 {
        int fd;
-       struct apm_info info;
+       struct apm_info a_info;
        char *out;
 
        out = (char *) calloc(16, sizeof(char));
@@ -852,20 +841,20 @@ char *get_apm_adapter()
                return out;
        }
 
-       if (apm_getinfo(fd, &info) != 0) {
+       if (apm_getinfo(fd, &a_info) != 0) {
                close(fd);
                strncpy(out, "ERR", 16);
                return out;
        }
        close(fd);
 
-       switch (info.ai_acline) {
+       switch (a_info.ai_acline) {
                case 0:
                        strncpy(out, "off-line", 16);
                        return out;
                        break;
                case 1:
-                       if (info.ai_batt_stat == 3) {
+                       if (a_info.ai_batt_stat == 3) {
                                strncpy(out, "charging", 16);
                                return out;
                        } else {
@@ -880,11 +869,11 @@ char *get_apm_adapter()
        }
 }
 
-char *get_apm_battery_life()
+char *get_apm_battery_life(void)
 {
        int fd;
        u_int batt_life;
-       struct apm_info info;
+       struct apm_info a_info;
        char *out;
 
        out = (char *) calloc(16, sizeof(char));
@@ -895,14 +884,14 @@ char *get_apm_battery_life()
                return out;
        }
 
-       if (apm_getinfo(fd, &info) != 0) {
+       if (apm_getinfo(fd, &a_info) != 0) {
                close(fd);
                strncpy(out, "ERR", 16);
                return out;
        }
        close(fd);
 
-       batt_life = info.ai_batt_life;
+       batt_life = a_info.ai_batt_life;
        if (batt_life == APM_UNKNOWN) {
                strncpy(out, "unknown", 16);
        } else if (batt_life <= 100) {
@@ -915,12 +904,12 @@ char *get_apm_battery_life()
        return out;
 }
 
-char *get_apm_battery_time()
+char *get_apm_battery_time(void)
 {
        int fd;
        int batt_time;
        int h, m, s;
-       struct apm_info info;
+       struct apm_info a_info;
        char *out;
 
        out = (char *) calloc(16, sizeof(char));
@@ -931,14 +920,14 @@ char *get_apm_battery_time()
                return out;
        }
 
-       if (apm_getinfo(fd, &info) != 0) {
+       if (apm_getinfo(fd, &a_info) != 0) {
                close(fd);
                strncpy(out, "ERR", 16);
                return out;
        }
        close(fd);
 
-       batt_time = info.ai_batt_time;
+       batt_time = a_info.ai_batt_time;
 
        if (batt_time == -1) {
                strncpy(out, "unknown", 16);
@@ -956,9 +945,24 @@ char *get_apm_battery_time()
 
 #endif
 
+void get_battery_short_status(char *buffer, unsigned int n, const char *bat)
+{
+       get_battery_stuff(buffer, n, bat, BATTERY_STATUS);
+       if (0 == strncmp("charging", buffer, 8)) {
+               buffer[0] = 'C';
+               memmove(buffer + 1, buffer + 8, n - 8);
+       } else if (0 == strncmp("discharging", buffer, 11)) {
+               buffer[0] = 'D';
+               memmove(buffer + 1, buffer + 11, n - 11);
+       } else if (0 == strncmp("absent/on AC", buffer, 12)) {
+               buffer[0] = 'A';
+               memmove(buffer + 1, buffer + 12, n - 12);
+       }
+}
+
 void update_entropy(void)
 {
-       /* mirrorbox: can you do anything equivalent in freebsd? -drphibes. */
+       /* Not applicable for FreeBSD as it uses the yarrow prng. */
 }
 
 /* empty stub so conky links */