RSS was not really disabled when it was supposed to be. Experimental wireless support.
[monky] / src / linux.c
index ec824e9..acb7f66 100644 (file)
@@ -16,6 +16,9 @@
 #include <sys/types.h>
 #include <sys/sysinfo.h>
 #include <sys/stat.h>
+#ifndef HAVE_CLOCK_GETTIME
+#include <sys/time.h>
+#endif
 #include <fcntl.h>
 #include <unistd.h>
 // #include <assert.h>
 #include <net/if.h>
 #include <math.h>
 
+#ifdef HAVE_IWLIB
+#include <iwlib.h>
+#endif
+
 #define SHORTSTAT_TEMPL "%*s %llu %llu %llu"
 #define LONGSTAT_TEMPL "%*s %llu %llu %llu "
 
-
-static struct sysinfo s_info;
-
 static int show_nice_processes;
 
+/* this flags tells the linux routines to use the /proc system
+ * where possible, even if other api's are available, e.g. sysinfo()
+ * or getloadavg(). the reason for this is to allow for /proc-based
+ * distributed monitoring. using a flag in this manner creates less
+ * confusing code.
+ */
+static int prefer_proc = 0;
+
 void prepare_update()
 {
 }
 
-static void update_sysinfo()
+void update_uptime()
 {
-       sysinfo(&s_info);
-
-       info.uptime = (double) s_info.uptime;
-
-       /* there was some problem with these */
-#if 0
-//      info.loadavg[0] = s_info.loads[0] / 100000.0f;
-       info.loadavg[1] = s_info.loads[1] / 100000.0f;
-       info.loadavg[2] = s_info.loads[2] / 100000.0f;
-       gkrelltop_process_find_top_three info.mask |= 1 << INFO_LOADAVG;
+#ifdef HAVE_SYSINFO
+  if (!prefer_proc)
+  {
+    struct sysinfo s_info;
+    sysinfo(&s_info);
+    info.uptime = (double) s_info.uptime;
+  }
+  else
 #endif
+  {
+         static int rep = 0;
+         FILE *fp;
 
-       info.procs = s_info.procs;
-
-       /* these aren't nice, no cache and should check kernel version for mem_unit */
-#if 0
-       info.memmax = s_info.totalram;
-       info.mem = s_info.totalram - s_info.freeram;
-       info.swapmax = s_info.totalswap;
-       info.swap = s_info.totalswap - s_info.swap;
-       info.mask |= 1 << INFO_MEM;
-#endif
-
-       info.mask |= (1 << INFO_UPTIME) | (1 << INFO_PROCS);
+    if (!(fp = open_file("/proc/uptime", &rep)))
+    {
+      info.uptime=0.0;
+      return;
+    }
+         fscanf(fp, "%lf", &info.uptime);
+         fclose(fp);
+  }
+  info.mask |= (1 << INFO_UPTIME);
 }
 
-void update_uptime()
+int check_mount(char *s)
 {
-       /* prefers sysinfo() for uptime, I don't really know which one is better
-        * (=faster?) */
-#ifdef USE_PROC_UPTIME
-       static int rep;
-       FILE *fp = open_file("/proc/uptime", &rep);
-       if (!fp)
-               return 0;
-       fscanf(fp, "%lf", &info.uptime);
-       fclose(fp);
-
-       info.mask |= (1 << INFO_UPTIME);
-#else
-       update_sysinfo();
-#endif
+       int ret = 0;
+       FILE *mtab = fopen("/etc/mtab", "r");
+       if (mtab) {
+               char buf1[256], buf2[128];
+               while (fgets(buf1, 256, mtab)) {
+                       sscanf(buf1, "%*s %128s", buf2);
+                       if (!strcmp(s, buf2)) {
+                               ret = 1;
+                               break;
+                       }
+               }
+               fclose(mtab);
+       } else {
+               ERR("Could not open mtab");
+       }
+       return ret;
 }
 
 /* these things are also in sysinfo except Buffers:, that's why I'm reading
 * them from proc */
 
-static FILE *meminfo_fp;
-
 void update_meminfo()
 {
-       static int rep;
+  FILE *meminfo_fp;
+       static int rep = 0;
        /*  unsigned int a; */
        char buf[256];
 
        info.mem = info.memmax = info.swap = info.swapmax = info.bufmem =
            info.buffers = info.cached = 0;
 
-       if (meminfo_fp == NULL)
-               meminfo_fp = open_file("/proc/meminfo", &rep);
-       else
-               fseek(meminfo_fp, 0, SEEK_SET);
-       if (meminfo_fp == NULL)
-               return;
+  if (!(meminfo_fp = open_file("/proc/meminfo", &rep)))
+      return;
 
        while (!feof(meminfo_fp)) {
                if (fgets(buf, 255, meminfo_fp) == NULL)
                        break;
 
                if (strncmp(buf, "MemTotal:", 9) == 0) {
-                       sscanf(buf, "%*s %lu", &info.memmax);
+                       sscanf(buf, "%*s %Lu", &info.memmax);
                } else if (strncmp(buf, "MemFree:", 8) == 0) {
-                       sscanf(buf, "%*s %lu", &info.mem);
+                       sscanf(buf, "%*s %Lu", &info.mem);
                } else if (strncmp(buf, "SwapTotal:", 10) == 0) {
-                       sscanf(buf, "%*s %lu", &info.swapmax);
+                       sscanf(buf, "%*s %Lu", &info.swapmax);
                } else if (strncmp(buf, "SwapFree:", 9) == 0) {
-                       sscanf(buf, "%*s %lu", &info.swap);
+                       sscanf(buf, "%*s %Lu", &info.swap);
                } else if (strncmp(buf, "Buffers:", 8) == 0) {
-                       sscanf(buf, "%*s %lu", &info.buffers);
+                       sscanf(buf, "%*s %Lu", &info.buffers);
                } else if (strncmp(buf, "Cached:", 7) == 0) {
-                       sscanf(buf, "%*s %lu", &info.cached);
+                       sscanf(buf, "%*s %Lu", &info.cached);
                }
        }
-       
+
        info.mem = info.memmax - info.mem;
        info.swap = info.swapmax - info.swap;
 
        info.bufmem = info.cached + info.buffers;
 
        info.mask |= (1 << INFO_MEM) | (1 << INFO_BUFFERS);
+
+  fclose (meminfo_fp);
 }
 
-static FILE *net_dev_fp;
 static FILE *net_wireless_fp;
 
 inline void update_net_stats()
 {
-       static int rep;
+  FILE *net_dev_fp;
+       static int rep = 0;
        // FIXME: arbitrary size chosen to keep code simple.
        int i, i2;
        unsigned int curtmp1, curtmp2;
        unsigned int k;
        struct ifconf conf;
-
-
        char buf[256];
        double delta;
 
+#ifdef HAVE_IWLIB
+       // wireless info variables
+       int skfd, has_bitrate = 0, link_qual = 0, link_qual_max = 0;
+       struct wireless_info *winfo;
+       struct iwreq wrq;
+#endif
+
        /* get delta */
        delta = current_update_time - last_update_time;
        if (delta <= 0.0001)
                return;
 
        /* open file and ignore first two lines */
-       if (net_dev_fp == NULL) {
-               net_dev_fp = open_file("/proc/net/dev", &rep);
-       }
-       else
-               fseek(net_dev_fp, 0, SEEK_SET);
-       if (!net_dev_fp)
-               return;
+  if (!(net_dev_fp = open_file("/proc/net/dev", &rep)))
+  {
+    clear_net_stats ();
+    return;
+  }
 
        fgets(buf, 255, net_dev_fp);    /* garbage */
        fgets(buf, 255, net_dev_fp);    /* garbage (field names) */
@@ -204,17 +216,13 @@ inline void update_net_stats()
                /* if recv or trans is less than last time, an overflow happened */
 
                if (r < ns->last_read_recv)
-                       ns->recv +=
-                           ((long long) 4294967295U -
-                            ns->last_read_recv) + r;
+      last_recv = 0;
                else
                        ns->recv += (r - ns->last_read_recv);
                ns->last_read_recv = r;
 
                if (t < ns->last_read_trans)
-                       ns->trans +=
-                           ((long long) 4294967295U -
-                            ns->last_read_trans) + t;
+      last_trans = 0;
                else
                        ns->trans += (t - ns->last_read_trans);
                ns->last_read_trans = t;
@@ -255,6 +263,8 @@ inline void update_net_stats()
                        curtmp1 += ns->net_rec[i];
                        curtmp2 += ns->net_trans[i];
                }
+               if (curtmp1 == 0) curtmp1 = 1;
+               if (curtmp2 == 0) curtmp2 = 1;
                ns->recv_speed = curtmp1 / (double) info.net_avg_samples;
                ns->trans_speed = curtmp2 / (double) info.net_avg_samples;
                if (info.net_avg_samples > 1) {
@@ -265,17 +275,68 @@ inline void update_net_stats()
                        }
                }
 
+#ifdef HAVE_IWLIB
+               /* update wireless info */
+               winfo = malloc(sizeof(struct wireless_info));
+               memset(winfo, 0, sizeof(struct wireless_info));
+
+               skfd = iw_sockets_open();
+               if(iw_get_basic_config(skfd, s, &(winfo->b)) > -1) {
+
+                       // set present winfo variables
+                       if(iw_get_stats(skfd, s, &(winfo->stats), &winfo->range, winfo->has_range) >= 0)
+                               winfo->has_stats = 1;
+                       if(iw_get_range_info(skfd, s, &(winfo->range)) >= 0)
+                               winfo->has_range = 1;
+                       if(iw_get_ext(skfd, s, SIOCGIWAP, &wrq) >= 0) {
+                               winfo->has_ap_addr = 1;
+                               memcpy(&(winfo->ap_addr), &(wrq.u.ap_addr), sizeof (sockaddr));
+                       }
+
+                       // get bitrate
+                       if(iw_get_ext(skfd, s, SIOCGIWRATE, &wrq) >= 0) {
+                               memcpy(&(winfo->bitrate), &(wrq.u.bitrate), sizeof(iwparam));
+                               iw_print_bitrate(ns->bitrate, 16, winfo->bitrate.value);
+                               has_bitrate = 1;
+                       }
+
+                       // get link quality
+                       if(winfo->has_range && winfo->has_stats && ((winfo->stats.qual.level != 0) || (winfo->stats.qual.updated & IW_QUAL_DBM))) {
+                               if(!(winfo->stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
+                                       ns->link_qual = winfo->stats.qual.qual;
+                                       ns->link_qual_max = winfo->range.max_qual.qual;
+                               }
+                       }
+
+                       // get ap mac
+                       if(winfo->has_ap_addr) {
+                               iw_sawap_ntop(&winfo->ap_addr, ns->ap);
+                       }
 
+                       // get essid
+                       if(winfo->b.has_essid) {
+                               if(winfo->b.essid_on)
+                               snprintf(ns->essid, 32, "%s", winfo->b.essid);
+                               else
+                               snprintf(ns->essid, 32, "off/any");
+                       }
 
+                       snprintf(ns->mode, 16, "%s", iw_operation_mode[winfo->b.mode]);
+               }
+               iw_sockets_close(skfd);
+               free(winfo);
+#endif
        }
 
-       /* fclose(net_dev_fp); net_dev_fp = NULL; */
+       fclose(net_dev_fp);
+
+  info.mask |= (1 << INFO_NET);
 }
 
 inline void update_wifi_stats()
 {
        /** wireless stats patch by Bobby Beckmann **/
-       static int rep;
+       static int rep = 0;
        int i;
        char buf[256];
        /*open file and ignore first two lines       sorry, this code sucks ass right now, i'll clean it up later */
@@ -315,7 +376,6 @@ inline void update_wifi_stats()
                sscanf(p, "%*d   %d.  %d.  %d", &l, &m, &n);
 
                ns->linkstatus = (int) (log(MIN(MAX(l,1),92)) / log(92) * 100);
-
        }
 
        /*** end wireless patch ***/
@@ -325,7 +385,28 @@ int result;
 
 void update_total_processes()
 {
-       update_sysinfo();
+#ifdef HAVE_SYSINFO
+  if (!prefer_proc)
+  {
+    struct sysinfo s_info;
+    sysinfo(&s_info);
+    info.procs = s_info.procs;
+  }
+  else
+#endif
+  {
+    static int rep = 0;
+    FILE *fp;
+
+    if (!(fp = open_file("/proc/loadavg", &rep)))
+    {
+      info.procs=0;
+      return;
+    }
+    fscanf(fp, "%*f %*f %*f %*d/%hd", &info.procs );
+    fclose(fp);
+  }
+  info.mask |= (1 << INFO_PROCS);
 }
 
 #define CPU_SAMPLE_COUNT 15
@@ -345,35 +426,31 @@ struct cpu_info {
        double cpu_val[CPU_SAMPLE_COUNT];
 };
 static short cpu_setup = 0;
-static int rep;
-
 
-static FILE *stat_fp;
-
-/* 
-   determine if this kernel gives us "extended" statistics information in /proc/stat. 
-   Kernels around 2.5 and earlier only reported user, system, nice and idle values in proc stat. 
-   Kernels around 2.6 and greater report these PLUS iowait, irq, softirq, and steal 
+/*
+   determine if this kernel gives us "extended" statistics information in /proc/stat.
+   Kernels around 2.5 and earlier only reported user, system, nice and idle values in proc stat.
+   Kernels around 2.6 and greater report these PLUS iowait, irq, softirq, and steal
 */
-void determine_longstat(char * buf) { 
+void determine_longstat(char * buf) {
        unsigned long long iowait=0;
-       KFLAG_SETOFF(KFLAG_IS_LONGSTAT);        
+       KFLAG_SETOFF(KFLAG_IS_LONGSTAT);
        /* scanf will either return -1 or 1 because there is only 1 assignment  */
        if (sscanf(buf, "%*s %*d %*d %*d %*d %llu",&iowait)>0) KFLAG_SETON(KFLAG_IS_LONGSTAT);
 }
 
 void get_cpu_count()
 {
+  FILE *stat_fp;
+  static int rep = 0;
+
        if (info.cpu_usage) {
                return;
        }
        char buf[256];
-       if (stat_fp == NULL)
-               stat_fp = open_file("/proc/stat", &rep);
-       else
-               fseek(stat_fp, 0, SEEK_SET);
-       if (stat_fp == NULL)
-               return;
+
+  if (!(stat_fp = open_file("/proc/stat", &rep)))
+    return;
 
        info.cpu_count = 0;
 
@@ -389,6 +466,8 @@ void get_cpu_count()
                }
        }
        info.cpu_usage = malloc((info.cpu_count + 1) * sizeof(float));
+
+  fclose (stat_fp);
 }
 
 #define TMPL_LONGSTAT "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
@@ -396,40 +475,43 @@ void get_cpu_count()
 
 inline static void update_stat()
 {
+  FILE *stat_fp;
+  static int rep = 0;
        static struct cpu_info *cpu = NULL;
        char buf[256];
        unsigned int i;
        unsigned int index;
        double curtmp;
-       char * stat_template=NULL; 
+       char * stat_template=NULL;
        unsigned int malloc_cpu_size=0;
-       
 
-       /* added check for !info.cpu_usage since that mem is freed on a SIGUSR1
-        * and linux.c was assuming it ws still allocated, leading to a sigsegv */
+
+       /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
        if (!cpu_setup || !info.cpu_usage) {
                get_cpu_count();
                cpu_setup = 1;
        }
 
-       if (stat_template == NULL) {
+       if (!stat_template) {
                stat_template = KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGSTAT : TMPL_SHORTSTAT ;
-       }       
+       }
 
-       if (cpu == NULL) {
-               malloc_cpu_size = (info.cpu_count + 1) *  sizeof(struct cpu_info);
+       if (!cpu) {
+               malloc_cpu_size = (info.cpu_count + 1) * sizeof(struct cpu_info);
                cpu = malloc(malloc_cpu_size);
                memset(cpu, 0, malloc_cpu_size);
        }
 
-       if (stat_fp == NULL) {
-               stat_fp = open_file("/proc/stat", &rep);
-       } else {
-               fseek(stat_fp, 0, SEEK_SET);
-       }
-       if (stat_fp == NULL) {
-               return;
-       }
+  if (!(stat_fp = open_file("/proc/stat", &rep)))
+  {
+    info.run_procs=0;
+    if (info.cpu_usage)
+    {
+       memset(info.cpu_usage, 0, info.cpu_count * sizeof (float));
+    }
+    return;
+  }
+
        index = 0;
        while (!feof(stat_fp)) {
                if (fgets(buf, 255, stat_fp) == NULL)
@@ -440,7 +522,7 @@ inline static void update_stat()
                        info.mask |= (1 << INFO_RUN_PROCS);
                } else if (strncmp(buf, "cpu", 3) == 0) {
                        index = isdigit(buf[3]) ? ((int)buf[3]) - 0x2F : 0;
-                       sscanf(buf, stat_template 
+                       sscanf(buf, stat_template
                                , &(cpu[index].cpu_user)
                                , &(cpu[index].cpu_nice)
                                , &(cpu[index].cpu_system)
@@ -451,32 +533,32 @@ inline static void update_stat()
                                , &(cpu[index].cpu_steal)
                                );
 
-                       cpu[index].cpu_total = cpu[index].cpu_user 
-                                        + cpu[index].cpu_nice 
-                                        + cpu[index].cpu_system 
-                                        + cpu[index].cpu_idle 
-                                        + cpu[index].cpu_iowait 
+                       cpu[index].cpu_total = cpu[index].cpu_user
+                                        + cpu[index].cpu_nice
+                                        + cpu[index].cpu_system
+                                        + cpu[index].cpu_idle
+                                        + cpu[index].cpu_iowait
                                         + cpu[index].cpu_irq
                                         + cpu[index].cpu_softirq
-                                        + cpu[index].cpu_steal 
-                                        ; 
+                                        + cpu[index].cpu_steal
+                                        ;
 
                        cpu[index].cpu_active_total = cpu[index].cpu_total - (cpu[index].cpu_idle + cpu[index].cpu_iowait);
                        info.mask |= (1 << INFO_CPU);
 
                        double delta = current_update_time - last_update_time;
-                       if (delta <= 0.001) return;     
+                       if (delta <= 0.001) break;
 
-                       cpu[index].cpu_val[0] = (cpu[index].cpu_active_total -  cpu[index].cpu_last_active_total) / 
-                                               (float )(cpu[index].cpu_total - cpu[index].cpu_last_total); 
+                       cpu[index].cpu_val[0] = (cpu[index].cpu_active_total -  cpu[index].cpu_last_active_total) /
+                                               (float )(cpu[index].cpu_total - cpu[index].cpu_last_total);
                        curtmp = 0;
                        for (i=0; i < info.cpu_avg_samples; i++ ) {
                                curtmp += cpu[index].cpu_val[i];
                        }
-                       /* TESTING -- I've removed this, because I don't think it is right. You shouldn't divide 
+                       /* TESTING -- I've removed this, because I don't think it is right. You shouldn't divide
                                      by the cpu count here ... removing for testing */
                        /* if (index == 0) {
-                               info.cpu_usage[index] = curtmp / info.cpu_avg_samples / info.cpu_count; 
+                               info.cpu_usage[index] = curtmp / info.cpu_avg_samples / info.cpu_count;
                        } else {
                                info.cpu_usage[index] = curtmp / info.cpu_avg_samples;
                        }  */
@@ -491,6 +573,7 @@ inline static void update_stat()
                }
 
        }
+  fclose (stat_fp);
 }
 
 void update_running_processes()
@@ -506,26 +589,29 @@ void update_cpu_usage()
 void update_load_average()
 {
 #ifdef HAVE_GETLOADAVG
-       double v[3];
-       getloadavg(v, 3);
-       info.loadavg[0] = (float) v[0];
-       info.loadavg[1] = (float) v[1];
-       info.loadavg[2] = (float) v[2];
-#else
-       static int rep;
-       FILE *fp;
-
-       fp = open_file("/proc/loadavg", &rep);
-       if (!fp) {
-               v[0] = v[1] = v[2] = 0.0;
-               return;
-       }
-
-       fscanf(fp, "%f %f %f", &info.loadavg[0], &info.loadavg[1],
-              &info.loadavg[2]);
-
-       fclose(fp);
+  if (!prefer_proc)
+  {
+         double v[3];
+         getloadavg(v, 3);
+         info.loadavg[0] = (float) v[0];
+         info.loadavg[1] = (float) v[1];
+         info.loadavg[2] = (float) v[2];
+  }
+  else
 #endif
+  {
+    static int rep = 0;
+    FILE *fp;
+
+    if (!(fp = open_file("/proc/loadavg", &rep)))
+    {
+      info.loadavg[0] = info.loadavg[1] = info.loadavg[2] = 0.0;
+      return;
+    }
+    fscanf(fp, "%f %f %f", &info.loadavg[0], &info.loadavg[1], &info.loadavg[2]);
+    fclose(fp);
+  }
+  info.mask |= (1 << INFO_LOADAVG);
 }
 
 #define PROC_I8K "/proc/i8k"
@@ -552,8 +638,8 @@ void update_i8k()
        i8k.bios = strtok(NULL,I8K_DELIM);
        i8k.serial = strtok(NULL,I8K_DELIM);
        i8k.cpu_temp = strtok(NULL,I8K_DELIM);
-       i8k.left_fan_status = strtok(NULL,I8K_DELIM);   
-       i8k.right_fan_status = strtok(NULL,I8K_DELIM);  
+       i8k.left_fan_status = strtok(NULL,I8K_DELIM);
+       i8k.right_fan_status = strtok(NULL,I8K_DELIM);
        i8k.left_fan_rpm = strtok(NULL,I8K_DELIM);
        i8k.right_fan_rpm = strtok(NULL,I8K_DELIM);
        i8k.ac_status = strtok(NULL,I8K_DELIM);
@@ -601,12 +687,14 @@ get_first_file_in_a_directory(const char *dir, char *s, int *rep)
        }
 }
 
-#define I2C_DIR "/sys/bus/i2c/devices/"
-
-int
-open_i2c_sensor(const char *dev, const char *type, int n, int *div,
-               char *devtype)
+int open_i2c_sensor(const char *dev, const char *type, int n, int *div, char *devtype)
 {
+       char i2c_dir[64];
+       if (post_21_kernel) {
+               strncpy(i2c_dir, "/sys/bus/platform/devices/", 64);
+       } else {
+               strncpy(i2c_dir, "/sys/bus/i2c/devices/", 64);
+       }
        char path[256];
        char buf[256];
        int fd;
@@ -614,8 +702,8 @@ open_i2c_sensor(const char *dev, const char *type, int n, int *div,
 
        /* if i2c device is NULL or *, get first */
        if (dev == NULL || strcmp(dev, "*") == 0) {
-               static int rep;
-               if (!get_first_file_in_a_directory(I2C_DIR, buf, &rep))
+               static int rep = 0;
+               if (!get_first_file_in_a_directory(i2c_dir, buf, &rep))
                        return -1;
                dev = buf;
        }
@@ -625,9 +713,9 @@ open_i2c_sensor(const char *dev, const char *type, int n, int *div,
                type = "in";
 
        if (strcmp(type, "tempf") == 0) {
-               snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, "temp", n);
+               snprintf(path, 255, "%s%s/%s%d_input", i2c_dir, dev, "temp", n);
        } else {
-               snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, type, n);
+               snprintf(path, 255, "%s%s/%s%d_input", i2c_dir, dev, type, n);
        }
        strncpy(devtype, path, 255);
 
@@ -648,10 +736,10 @@ open_i2c_sensor(const char *dev, const char *type, int n, int *div,
 
        /* test if *_div file exist, open it and use it as divisor */
        if (strcmp(type, "tempf") == 0) {
-               snprintf(path, 255, I2C_DIR "%s/%s%d_div", "one", "two",
+               snprintf(path, 255, "%s%s/%s%d_div", i2c_dir, "one", "two",
                         n);
        } else {
-               snprintf(path, 255, I2C_DIR "%s/%s%d_div", dev, type, n);
+               snprintf(path, 255, "%s%s/%s%d_div", i2c_dir, dev, type, n);
        }
 
        divfd = open(path, O_RDONLY);
@@ -697,7 +785,7 @@ double get_i2c_info(int *fd, int div, char *devtype, char *type)
        if (*fd < 0)
                ERR("can't open '%s': %s", devtype, strerror(errno));
 
-       /* My dirty hack for computing CPU value 
+       /* My dirty hack for computing CPU value
         * Filedil, from forums.gentoo.org
         */
 /*     if (strstr(devtype, "temp1_input") != NULL)
@@ -732,7 +820,7 @@ double get_i2c_info(int *fd, int div, char *devtype, char *type)
 
 void get_adt746x_fan( char * p_client_buffer, size_t client_buffer_size )
 {
-       static int rep;
+       static int rep = 0;
        char adt746x_fan_state[64];
        FILE *fp;
 
@@ -765,13 +853,13 @@ void get_adt746x_fan( char * p_client_buffer, size_t client_buffer_size )
 
 void get_adt746x_cpu( char * p_client_buffer, size_t client_buffer_size )
 {
-       static int rep;
+       static int rep = 0;
        char adt746x_cpu_state[64];
        FILE *fp;
 
        if ( !p_client_buffer || client_buffer_size <= 0 )
                return;
-       
+
        if ((fp = open_file(ADT746X_CPU, &rep)) == NULL
                 && (fp = open_file(ADT746X_CPU_OLD, &rep)) == NULL)
        {
@@ -783,7 +871,7 @@ void get_adt746x_cpu( char * p_client_buffer, size_t client_buffer_size )
                fclose(fp);
        }
 
-       snprintf( p_client_buffer, client_buffer_size, "%s", adt746x_cpu_state ); 
+       snprintf( p_client_buffer, client_buffer_size, "%s", adt746x_cpu_state );
        return;
 }
 
@@ -840,8 +928,8 @@ void get_freq_dynamic( char * p_client_buffer, size_t client_buffer_size, char *
 #else
 /* FIXME: hardwired: get freq for first cpu!
    this whole function needs to be rethought and redone for
-   multi-cpu/multi-core/multi-threaded environments and 
-   arbitrary combinations thereof 
+   multi-cpu/multi-core/multi-threaded environments and
+   arbitrary combinations thereof
 */
        get_freq( p_client_buffer, client_buffer_size, p_format, divisor, 1 );
        return;
@@ -856,33 +944,34 @@ void get_freq_dynamic( char * p_client_buffer, size_t client_buffer_size, char *
 char get_freq( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor, unsigned int cpu )
 {
        FILE *f;
+  static int rep = 0;
        char frequency[32];
        char s[256];
        double freq = 0;
-       char current_freq_file[128];
-       
-       cpu--;
-       snprintf(current_freq_file, 127, "%s/cpu%d/%s",
-                CPUFREQ_PREFIX, cpu, CPUFREQ_POSTFIX);
 
        if ( !p_client_buffer || client_buffer_size <= 0 || !p_format || divisor <= 0 )
                return 0;
-       
-       f = fopen(current_freq_file, "r");
-       if (f) {
-               /* if there's a cpufreq /sys node, read the current frequency from this node;
-                * divide by 1000 to get Mhz. */
-               if (fgets(s, sizeof(s), f)) {
-                       s[strlen(s)-1] = '\0';
-                       freq = strtod(s, NULL);
-               }
-               fclose(f);
-               snprintf( p_client_buffer, client_buffer_size, p_format, (freq/1000)/divisor );
-               return 1;
+
+  if (!prefer_proc)
+  {
+    char current_freq_file[128];
+    snprintf(current_freq_file, 127, "%s/cpu%d/%s",CPUFREQ_PREFIX, cpu-1, CPUFREQ_POSTFIX);
+         f = fopen(current_freq_file, "r");
+         if (f)
+    {
+                 /* if there's a cpufreq /sys node, read the current frequency from this node;
+                  * divide by 1000 to get Mhz. */
+                 if (fgets(s, sizeof(s), f)) {
+                           s[strlen(s)-1] = '\0';
+                           freq = strtod(s, NULL);
+                 }
+                 fclose(f);
+                 snprintf( p_client_buffer, client_buffer_size, p_format, (freq/1000)/divisor );
+                 return 1;
+    }
        }
-       
-       cpu++;
-       f = fopen("/proc/cpuinfo", "r");                //open the CPU information file
+
+       f = open_file("/proc/cpuinfo", &rep);           //open the CPU information file
        if (!f) {
                perror("Conky: Failed to access '/proc/cpuinfo' at get_freq()");
                return 0;
@@ -903,7 +992,7 @@ char get_freq( char * p_client_buffer, size_t client_buffer_size, char * p_forma
                strcpy(frequency, strchr(s, ':') + 2);  //copy just the number
 #if defined(__alpha)
                frequency[strlen(frequency) - 6] = '\0';// strip " est.\n"
-               freq = strtod(frequency, NULL)/1000000; // kernel reports in Hz 
+               freq = strtod(frequency, NULL)/1000000; // kernel reports in Hz
 #else
                frequency[strlen(frequency) - 1] = '\0'; // strip \n
                freq = strtod(frequency, NULL);
@@ -911,12 +1000,12 @@ char get_freq( char * p_client_buffer, size_t client_buffer_size, char * p_forma
                break;
                }
                if (strncmp(s, "processor", 9) == 0) {
-                   cpu--; 
+                   cpu--;
                    continue;
                }
-               
+
        }
-       
+
        fclose(f);
        snprintf( p_client_buffer, client_buffer_size, p_format, (float)freq/divisor );
        return 1;
@@ -927,7 +1016,7 @@ char get_freq( char * p_client_buffer, size_t client_buffer_size, char * p_forma
 /* return cpu voltage in mV (use divisor=1) or V (use divisor=1000) */
 char get_voltage( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor, unsigned int cpu )
 {
-/* /sys/devices/system/cpu/cpu0/cpufreq/scaling_voltages looks 
+/* /sys/devices/system/cpu/cpu0/cpufreq/scaling_voltages looks
    something like this:
 # frequency voltage
 1800000 1340
@@ -946,7 +1035,7 @@ char get_voltage( char * p_client_buffer, size_t client_buffer_size, char * p_fo
        int voltage = 0;
        char current_freq_file[128];
        int freq_comp = 0;
-       
+
 
 /* build the voltage file name */
        cpu--;
@@ -1004,7 +1093,7 @@ char get_voltage( char * p_client_buffer, size_t client_buffer_size, char * p_fo
 
 void get_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
 {
-       static int rep;
+       static int rep = 0;
        char buf[256];
        char buf2[256];
        FILE *fp;
@@ -1039,7 +1128,7 @@ void get_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
 
 void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
 {
-       static int rep;
+       static int rep = 0;
        char buf[256];
        char buf2[256];
        FILE *fp;
@@ -1051,11 +1140,11 @@ void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
        if (!get_first_file_in_a_directory(ACPI_AC_ADAPTER_DIR, buf, &rep))
        {
                snprintf( p_client_buffer, client_buffer_size, "no ac_adapters?" );
-               return; 
+               return;
        }
 
        snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_AC_ADAPTER_DIR, buf );
-        
+
 
        fp = open_file(buf2, &rep);
        if (!fp) {
@@ -1095,7 +1184,7 @@ int open_acpi_temperature(const char *name)
        int fd;
 
        if (name == NULL || strcmp(name, "*") == 0) {
-               static int rep;
+               static int rep = 0;
                if (!get_first_file_in_a_directory
                    (ACPI_THERMAL_DIR, buf, &rep))
                        return -1;
@@ -1145,7 +1234,7 @@ double get_acpi_temperature(int fd)
 }
 
 /*
-hipo@lepakko hipo $ cat /proc/acpi/battery/BAT1/info 
+hipo@lepakko hipo $ cat /proc/acpi/battery/BAT1/info
 present:                 yes
 design capacity:         4400 mAh
 last full capacity:      4064 mAh
@@ -1172,10 +1261,10 @@ present voltage:         16608 mV
 */
 
 /*
-2213<@jupet kellari ö> jupet@lagi-unstable:~$ cat /proc/apm 
+2213<@jupet kellari ö> jupet@lagi-unstable:~$ cat /proc/apm
 2213<@jupet kellari ö> 1.16 1.2 0x03 0x01 0xff 0x10 -1% -1 ?
 2213<@jupet kellari ö> (-1 ollee ei akkua kiinni, koska akku on pöydällä)
-2214<@jupet kellari ö> jupet@lagi-unstable:~$ cat /proc/apm 
+2214<@jupet kellari ö> jupet@lagi-unstable:~$ cat /proc/apm
 2214<@jupet kellari ö> 1.16 1.2 0x03 0x01 0x03 0x09 98% -1 ?
 
 2238<@jupet kellari ö> 1.16 1.2 0x03 0x00 0x00 0x01 100% -1 ? ilman verkkovirtaa
@@ -1187,43 +1276,83 @@ present voltage:         16608 mV
 
 #define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery"
 #define APM_PATH "/proc/apm"
+#define MAX_BATTERY_COUNT 4
 
-static FILE *acpi_bat_fp;
-static FILE *apm_bat_fp;
+static FILE *acpi_bat_fp[MAX_BATTERY_COUNT];
+static FILE *apm_bat_fp[MAX_BATTERY_COUNT];
 
-static int acpi_last_full;
+static int batteries_initialized = 0;
+static char batteries[MAX_BATTERY_COUNT][32];
 
-static char last_battery_str[64];
+static int acpi_last_full[MAX_BATTERY_COUNT];
+static int acpi_design_capacity[MAX_BATTERY_COUNT];
 
-static double last_battery_time;
+static char last_battery_str[MAX_BATTERY_COUNT][64];   /* e.g. "charging 75%" */
+static char last_battery_time_str[MAX_BATTERY_COUNT][64];      /* e.g. "3h 15m" */
+
+static double last_battery_time[MAX_BATTERY_COUNT];
+
+static int last_battery_perct[MAX_BATTERY_COUNT];
+static double last_battery_perct_time[MAX_BATTERY_COUNT];
+
+
+void init_batteries(void)
+{
+       int idx;
+       if(batteries_initialized)
+               return;
+       for(idx = 0; idx < MAX_BATTERY_COUNT; idx++)
+               batteries[idx][0] = '\0';
+       batteries_initialized = 1;
+}
 
-void get_battery_stuff(char *buf, unsigned int n, const char *bat)
+int get_battery_idx(const char *bat)
 {
-       static int rep, rep2;
+       int idx;
+       for(idx = 0; idx < MAX_BATTERY_COUNT; idx++)
+               if(!strlen(batteries[idx]) || !strcmp(batteries[idx], bat))
+                       break;
+
+       /* if not found, enter a new entry */
+       if(!strlen(batteries[idx]))
+               snprintf(batteries[idx], 31, "%s", bat);
+
+       return idx;
+}
+
+void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
+{
+       static int idx, rep = 0, rep2 = 0;
        char acpi_path[128];
        snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
 
+       init_batteries();
+
+       idx = get_battery_idx(bat);
+
        /* 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;
+       if (current_update_time - last_battery_time[idx] < 29.5)
+               goto set_return_value;
+
+       last_battery_time[idx] = current_update_time;
+
+       memset (last_battery_str[idx], 0, sizeof (last_battery_str[idx]));
+       memset (last_battery_time_str[idx], 0, sizeof (last_battery_time_str[idx]));
 
        /* first try ACPI */
 
-       if (acpi_bat_fp == NULL && apm_bat_fp == NULL)
-               acpi_bat_fp = open_file(acpi_path, &rep);
+       if (acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL)
+               acpi_bat_fp[idx] = open_file(acpi_path, &rep);
 
-       if (acpi_bat_fp != NULL) {
+       if (acpi_bat_fp[idx] != NULL) {
                int present_rate = -1;
                int remaining_capacity = -1;
                char charging_state[64];
                char present[4];
 
                /* read last full capacity if it's zero */
-               if (acpi_last_full == 0) {
-                       static int rep;
+               if (acpi_last_full[idx] == 0) {
+                       static int rep = 0;
                        char path[128];
                        FILE *fp;
                        snprintf(path, 127,
@@ -1234,7 +1363,7 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat)
                                        char b[256];
                                        if (fgets(b, 256, fp) == NULL)
                                                break;
-                                       if (sscanf(b, "last full capacity: %d", &acpi_last_full) != 0) {
+                                       if (sscanf(b, "last full capacity: %d", &acpi_last_full[idx]) != 0) {
                                                break;
                                        }
                                }
@@ -1243,115 +1372,211 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat)
                        }
                }
 
-               fseek(acpi_bat_fp, 0, SEEK_SET);
+               fseek(acpi_bat_fp[idx], 0, SEEK_SET);
 
                strcpy(charging_state, "unknown");
 
-               while (!feof(acpi_bat_fp)) {
+               while (!feof(acpi_bat_fp[idx])) {
                        char buf[256];
-                       if (fgets(buf, 256, acpi_bat_fp) == NULL)
+                       if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL)
                                break;
 
                        /* let's just hope units are ok */
-                       if (buf[0] == 'p') {
+                       if (strncmp (buf, "present:", 8) == 0)
                                sscanf(buf, "present: %4s", present);
-                       } else if (buf[0] == 'c')
-                               sscanf(buf, "charging state: %63s",
-                                      charging_state);
-                       else if (buf[0] == 'p')
-                               sscanf(buf, "present rate: %d",
-                                      &present_rate);
-                       else if (buf[0] == 'r')
-                               sscanf(buf, "remaining capacity: %d",
-                                      &remaining_capacity);
+                       else if (strncmp (buf, "charging state:", 15) == 0)
+                               sscanf(buf, "charging state: %63s", charging_state);
+                       else if (strncmp (buf, "present rate:", 13) == 0)
+                               sscanf(buf, "present rate: %d", &present_rate);
+                       else if (strncmp(buf, "remaining capacity:", 19) == 0)
+                               sscanf(buf, "remaining capacity: %d", &remaining_capacity);
                }
-               
+
+               /* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */
+               if (remaining_capacity > acpi_last_full[idx])
+                       acpi_last_full[idx] = remaining_capacity;  /* normalize to 100% */
+
                /* not present */
                if (strcmp(present, "no") == 0) {
-                       strncpy(last_battery_str, "not present", 64);
+                       strncpy(last_battery_str[idx], "not present", 64);
                }
                /* charging */
                else if (strcmp(charging_state, "charging") == 0) {
-                       if (acpi_last_full != 0 && present_rate > 0) {
-                               snprintf(last_battery_str, 63, "charging %i%% ", (int) (remaining_capacity / acpi_last_full) * 100);
-                               format_seconds(last_battery_str + 14,
-                                              63 - 14,
-                                              (acpi_last_full -
-                                               remaining_capacity) * 60 *
-                                              60 / present_rate);
-                       } else if (acpi_last_full != 0
-                                  && present_rate <= 0) {
-                               snprintf(last_battery_str, 64, "charging %d%%",
-                                       remaining_capacity * 100 /
-                                       acpi_last_full);
+                       if (acpi_last_full[idx] != 0 && present_rate > 0) {
+                               /* e.g. charging 75% */
+                               snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%",
+                                       (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
+                               /* e.g. 2h 37m */
+                               format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1,
+                                             (long) (((acpi_last_full[idx] - remaining_capacity) * 3600) /
+                                                     present_rate));
+                       } else if (acpi_last_full[idx] != 0 && present_rate <= 0) {
+                               snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %d%%",
+                                       (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
                        } else {
-                               strncpy(last_battery_str, "charging", 63);
+                               strncpy(last_battery_str[idx], "charging", sizeof(last_battery_str[idx])-1);
                        }
                }
                /* discharging */
                else if (strncmp(charging_state, "discharging", 64) == 0) {
                        if (present_rate > 0) {
-                               snprintf(last_battery_str, 63, "discharging %i%% ", (int)(remaining_capacity / acpi_last_full) * 100);
-                               format_seconds(last_battery_str + 17, 63 - 17,
-                                              (remaining_capacity * 60 *
-                                               60) / present_rate);
+                               /* e.g. discharging 35% */
+                               snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%",
+                                       (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
+                               /* e.g. 1h 12m */
+                               format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1,
+                                             (long) ((remaining_capacity * 3600) / present_rate));
                        } else if (present_rate == 0) { /* Thanks to Nexox for this one */
-                               snprintf(last_battery_str, 64, "full");
+                               snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "full");
                        } else {
-                               snprintf(last_battery_str, 64,
+                               snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1,
                                        "discharging %d%%",
-                                       remaining_capacity * 100 /
-                                       acpi_last_full);
+                                       (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
                        }
                }
                /* charged */
                /* thanks to Lukas Zapletal <lzap@seznam.cz> */
                else if (strncmp(charging_state, "charged", 64) == 0) {
-                               strcpy(last_battery_str, "charged");
-               } 
+                               /* Below happens with the second battery on my X40,
+                                * when the second one is empty and the first one
+                                * being charged. */
+                               if (remaining_capacity == 0)
+                                       strcpy(last_battery_str[idx], "empty");
+                               else
+                                       strcpy(last_battery_str[idx], "charged");
+               }
                /* unknown, probably full / AC */
                else {
-                       if (acpi_last_full != 0
-                           && remaining_capacity != acpi_last_full)
-                               snprintf(last_battery_str, 64, "unknown %d%%",
-                                       remaining_capacity * 100 /
-                                       acpi_last_full);
+                       if (acpi_last_full[idx] != 0
+                           && remaining_capacity != acpi_last_full[idx])
+                               snprintf(last_battery_str[idx], 64, "unknown %d%%",
+                                       (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
                        else
-                               strncpy(last_battery_str, "AC", 64);
+                               strncpy(last_battery_str[idx], "AC", 64);
                }
        } else {
                /* APM */
-               if (apm_bat_fp == NULL)
-                       apm_bat_fp = open_file(APM_PATH, &rep2);
+               if (apm_bat_fp[idx] == NULL)
+                       apm_bat_fp[idx] = open_file(APM_PATH, &rep2);
 
-               if (apm_bat_fp != NULL) {
+               if (apm_bat_fp[idx] != NULL) {
                        int ac, status, flag, life;
 
-                       fscanf(apm_bat_fp,
+                       fscanf(apm_bat_fp[idx],
                               "%*s %*s %*x %x   %x       %x     %d%%",
                               &ac, &status, &flag, &life);
 
                        if (life == -1) {
                                /* could check now that there is ac */
-                               snprintf(last_battery_str, 64, "AC");
+                               snprintf(last_battery_str[idx], 64, "AC");
                        } else if (ac && life != 100) { /* could check that status==3 here? */
-                               snprintf(last_battery_str, 64,
+                               snprintf(last_battery_str[idx], 64,
                                         "charging %d%%", life);
                        } else {
-                               snprintf(last_battery_str, 64, "%d%%",
+                               snprintf(last_battery_str[idx], 64, "%d%%",
                                         life);
                        }
 
                        /* it seemed to buffer it so file must be closed (or could use syscalls
                         * directly but I don't feel like coding it now) */
-                       fclose(apm_bat_fp);
-                       apm_bat_fp = NULL;
+                       fclose(apm_bat_fp[idx]);
+                       apm_bat_fp[idx] = NULL;
+               }
+       }
+
+set_return_value:
+       switch (item) {
+        case BATTERY_STATUS:
+               {
+                       snprintf(buf, n, "%s", last_battery_str[idx]);
+                       break;
+               }
+        case BATTERY_TIME:
+               {
+                       snprintf(buf, n, "%s", last_battery_time_str[idx]);
+                       break;
+               }
+       default:
+                       break;
+        }
+       return;
+}
+
+int get_battery_perct(const char *bat)
+{
+       static int rep;
+       int idx;
+       char acpi_path[128];
+       snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
+
+       init_batteries();
+
+       idx = get_battery_idx(bat);
+
+       /* don't update battery too often */
+       if (current_update_time - last_battery_perct_time[idx] < 30) {
+               return last_battery_perct[idx];
+       }
+       last_battery_perct_time[idx] = current_update_time;
+
+       /* Only check for ACPI */
+
+       if (acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL)
+               acpi_bat_fp[idx] = open_file(acpi_path, &rep);
+
+       int remaining_capacity = -1;
+       if (acpi_bat_fp[idx] != NULL) {
+               /* read last full capacity if it's zero */
+               if (acpi_design_capacity[idx] == 0) {
+                       static int rep;
+                       char path[128];
+                       FILE *fp;
+                       snprintf(path, 127,
+                                ACPI_BATTERY_BASE_PATH "/%s/info", bat);
+                       fp = open_file(path, &rep);
+                       if (fp != NULL) {
+                               while (!feof(fp)) {
+                                       char b[256];
+                                       if (fgets(b, 256, fp) == NULL)
+                                               break;
+                                       if (sscanf(b, "last full capacity: %d", &acpi_design_capacity[idx]) != 0) {
+                                               break;
+                                       }
+                               }
+                               fclose(fp);
+                       }
+               }
+
+               fseek(acpi_bat_fp[idx], 0, SEEK_SET);
+
+               while (!feof(acpi_bat_fp[idx])) {
+                       char buf[256];
+                       if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL)
+                               break;
+
+                       if (buf[0] == 'r')
+                               sscanf(buf, "remaining capacity: %d",
+                                      &remaining_capacity);
                }
        }
+       if(remaining_capacity < 0)
+               return 0;
+       /* compute the battery percentage */
+       last_battery_perct[idx] =
+               (int) (((float)remaining_capacity/acpi_design_capacity[idx]) * 100);
+       return last_battery_perct[idx];
+}
 
-       snprintf(buf, n, "%s", last_battery_str);
+int get_battery_perct_bar(const char *bar)
+{
+       int idx;
+       get_battery_perct(bar);
+       idx = get_battery_idx(bar);
+       return (int) (last_battery_perct[idx] * 2.56 - 1);
 }
 
+
+
 /* On Apple powerbook and ibook:
 $ cat /proc/pmu/battery_0
 flags      : 00000013
@@ -1375,11 +1600,11 @@ static FILE* pmu_battery_fp;
 static FILE* pmu_info_fp;
 static char pb_battery_info[3][32];
 static double pb_battery_info_update;
+
 #define PMU_PATH "/proc/pmu"
 void get_powerbook_batt_info(char *buf, size_t n, int i)
 {
-        static int rep;
+        static int rep = 0;
         const char* batt_path = PMU_PATH "/battery_0";
         const char* info_path = PMU_PATH "/info";
         int flags, charge, max_charge, ac = -1;
@@ -1439,7 +1664,7 @@ void get_powerbook_batt_info(char *buf, size_t n, int i)
 
         /* update percentage string */
         if (time == 0)
-                pb_battery_info[PB_BATT_PERCENT][0] = 0; 
+                pb_battery_info[PB_BATT_PERCENT][0] = 0;
         else
                 snprintf(pb_battery_info[PB_BATT_PERCENT],
                         sizeof(pb_battery_info[PB_BATT_PERCENT]),
@@ -1447,7 +1672,7 @@ void get_powerbook_batt_info(char *buf, size_t n, int i)
 
         /* update time string */
         if (time == 0) /* fully charged or battery not present */
-                pb_battery_info[PB_BATT_TIME][0] = 0; 
+                pb_battery_info[PB_BATT_TIME][0] = 0;
         else if (time < 60*60) /* don't show secs */
                 format_seconds_short(pb_battery_info[PB_BATT_TIME],
                         sizeof(pb_battery_info[PB_BATT_TIME]), time);
@@ -1486,24 +1711,27 @@ void update_top()
 void update_diskio()
 {
        static unsigned int last = UINT_MAX;
-       static FILE* fp;
+       static unsigned int last_read = UINT_MAX;
+       static unsigned int last_write = UINT_MAX;
+       FILE* fp;
+       static int rep=0;
 
        char buf[512];
        int major, minor;
        unsigned int current = 0;
+       unsigned int current_read = 0;
+       unsigned int current_write = 0;
        unsigned int reads, writes = 0;
        int col_count = 0;
 
-       if (!fp) {
-               fp = fopen("/proc/diskstats", "r");
-       } else {
-               fseek(fp, 0, SEEK_SET);
+       if (!(fp =open_file("/proc/diskstats", &rep))) {
+               diskio_value=0;
+               return;
        }
 
        /* read reads and writes from all disks (minor = 0), including
         * cd-roms and floppies, and summ them up
         */
-       current = 0;
        while (!feof(fp)) {
                fgets(buf, 512, fp);
                col_count = sscanf(buf, "%u %u %*s %*u %*u %u %*u %*u %*u %u",
@@ -1517,6 +1745,8 @@ void update_diskio()
                    major != LVM_BLK_MAJOR && major != NBD_MAJOR &&
                    major != RAMDISK_MAJOR && major != LOOP_MAJOR) {
                        current += reads + writes;
+                       current_read += reads;
+                       current_write += writes;
                }
        }
 
@@ -1525,6 +1755,14 @@ void update_diskio()
         * "sectors read", and we therefore have to divide by two to
         * get KB */
        int tot = ((double)(current-last)/2);
+       int tot_read = ((double)(current_read-last_read)/2);
+       int tot_write = ((double)(current_write-last_write)/2);
+
+       if (last_read > current_read)
+           tot_read = 0;
+       if (last_write > current_write)
+           tot_write = 0;
+
        if (last > current) {
                /* we hit this either if it's the very first time we
                  * run this, or when /proc/diskstats overflows; while
@@ -1532,8 +1770,14 @@ void update_diskio()
                tot = 0;
        }
        last = current;
+       last_read = current_read;
+       last_write = current_write;
 
        diskio_value = tot;
+       diskio_read_value = tot_read;
+       diskio_write_value = tot_write;
+
+       fclose(fp);
 }
 
 /* Here come the IBM ACPI-specific things. For reference, see
@@ -1573,11 +1817,11 @@ Peter Tarjan (ptarjan@citromail.hu)
 
     if ( !p_client_buffer || client_buffer_size <= 0 )
        return;
-    
+
     FILE *fp;
     unsigned int speed=0;
     char fan[128];
-    snprintf(fan, 127, "%s/fan",IBM_ACPI_DIR);    
+    snprintf(fan, 127, "%s/fan",IBM_ACPI_DIR);
 
     fp = fopen(fan, "r");
     if (fp != NULL)
@@ -1586,10 +1830,10 @@ Peter Tarjan (ptarjan@citromail.hu)
        {
            char line[256];
            if (fgets(line, 255, fp) == NULL) break;
-           if (sscanf(line, "speed: %d", &speed)) break;       
+           if (sscanf(line, "speed: %d", &speed)) break;
        }
     }
-    else 
+    else
     {
        CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove ibm* from your Conky config file.", fan, strerror(errno));
     }
@@ -1597,15 +1841,15 @@ Peter Tarjan (ptarjan@citromail.hu)
     fclose(fp);
     snprintf( p_client_buffer, client_buffer_size, "%d", speed );
     return;
-    
-}    
+
+}
 
 static double last_ibm_acpi_temp_time;
 void get_ibm_acpi_temps()
 {
-/* get the measured temperatures from the temperature sensors 
+/* get the measured temperatures from the temperature sensors
    on IBM/Lenovo laptops running the ibm acpi.
-   There are 8 values in /proc/acpi/ibm/thermal, and according to 
+   There are 8 values in /proc/acpi/ibm/thermal, and according to
    http://ibm-acpi.sourceforge.net/README
    these mean the following (at least on an IBM R51...)
 0:  CPU (also on the T series laptops)
@@ -1615,30 +1859,30 @@ void get_ibm_acpi_temps()
 4:  Battery (?)
 5:  N/A
 6:  Battery (?)
-7:  N/A 
-   I'm not too sure about those with the question mark, but the values I'm 
-   reading from *my* thermal file (on a T42p) look realistic for the 
-   hdd and the battery. 
-   #5 and #7 are always -128. 
+7:  N/A
+   I'm not too sure about those with the question mark, but the values I'm
+   reading from *my* thermal file (on a T42p) look realistic for the
+   hdd and the battery.
+   #5 and #7 are always -128.
    /proc/acpi/ibm/thermal looks like this (1 line):
 temperatures:   41 43 31 46 33 -128 29 -128
 Peter Tarjan (ptarjan@citromail.hu)
 */
 
 /*    don't update too often */
-    if (current_update_time - last_ibm_acpi_temp_time < 10.00) 
+    if (current_update_time - last_ibm_acpi_temp_time < 10.00)
     {
        return;
     }
-    last_ibm_acpi_temp_time = current_update_time; 
-    
+    last_ibm_acpi_temp_time = current_update_time;
+
 /*    if ( !p_client_buffer || client_buffer_size <= 0 )
       return; */
 
     FILE *fp;
 
     char thermal[128];
-    snprintf(thermal, 127, "%s/thermal",IBM_ACPI_DIR);    
+    snprintf(thermal, 127, "%s/thermal",IBM_ACPI_DIR);
     fp = fopen(thermal, "r");
 
     if (fp != NULL)
@@ -1650,18 +1894,18 @@ Peter Tarjan (ptarjan@citromail.hu)
            if (sscanf(line, "temperatures: %d %d %d %d %d %d %d %d",
                       &ibm_acpi.temps[0], &ibm_acpi.temps[1],
                       &ibm_acpi.temps[2], &ibm_acpi.temps[3],
-                      &ibm_acpi.temps[4], &ibm_acpi.temps[5], 
+                      &ibm_acpi.temps[4], &ibm_acpi.temps[5],
                       &ibm_acpi.temps[6], &ibm_acpi.temps[7])) break;
        }
     }
-    else 
+    else
     {
        CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove ibm* from your Conky config file.", thermal, strerror(errno));
     }
 
     fclose(fp);
 
-}             
+}
 
 
 void get_ibm_acpi_volume( char * p_client_buffer, size_t client_buffer_size )
@@ -1677,14 +1921,14 @@ commands:       up, down, mute
 commands:       level <level> (<level> is 0-15)
 Peter Tarjan (ptarjan@citromail.hu)
 */
-    
+
     if ( !p_client_buffer || client_buffer_size <= 0 )
        return;
-    
+
     FILE *fp;
 
     char volume[128];
-    snprintf(volume, 127, "%s/volume",IBM_ACPI_DIR);    
+    snprintf(volume, 127, "%s/volume",IBM_ACPI_DIR);
     unsigned int vol=-1;
     char mute[3]="";
 
@@ -1699,7 +1943,7 @@ Peter Tarjan (ptarjan@citromail.hu)
            if (sscanf(line, "mute: %s", mute)) break;
        }
     }
-    else 
+    else
     {
        CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove ibm* from your Conky config file.", volume, strerror(errno));
     }
@@ -1737,7 +1981,7 @@ Peter Tarjan (ptarjan@citromail.hu)
     FILE *fp;
     unsigned int brightness=0;
     char filename[128];
-    snprintf(filename, 127, "%s/brightness",IBM_ACPI_DIR);    
+    snprintf(filename, 127, "%s/brightness",IBM_ACPI_DIR);
 
     fp = fopen(filename, "r");
     if (fp != NULL)
@@ -1746,10 +1990,10 @@ Peter Tarjan (ptarjan@citromail.hu)
        {
            char line[256];
            if (fgets(line, 255, fp) == NULL) break;
-           if (sscanf(line, "level: %d", &brightness)) break;  
+           if (sscanf(line, "level: %d", &brightness)) break;
        }
     }
-    else 
+    else
     {
        CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove ibm* from your Conky config file.", filename, strerror(errno));
     }
@@ -1758,5 +2002,33 @@ Peter Tarjan (ptarjan@citromail.hu)
 
     snprintf( p_client_buffer, client_buffer_size, "%d", brightness );
     return;
-    
-}    
+
+}
+
+void update_entropy (void)
+{
+  static int rep = 0;
+  const char *entropy_avail = "/proc/sys/kernel/random/entropy_avail";
+  const char *entropy_poolsize = "/proc/sys/kernel/random/poolsize";
+  FILE *fp1, *fp2;
+
+  info.entropy.entropy_avail=0;
+  info.entropy.poolsize=0;
+
+  if ((fp1 = open_file (entropy_avail, &rep))==NULL)
+    return;
+
+  if ((fp2 = open_file (entropy_poolsize, &rep))==NULL)
+  {
+    fclose (fp1);
+    return;
+  }
+
+  fscanf (fp1, "%u", &info.entropy.entropy_avail);
+  fscanf (fp2, "%u", &info.entropy.poolsize);
+
+  fclose (fp1);
+  fclose (fp2);
+
+  info.mask |= (1 << INFO_ENTROPY);
+}