* Replacing strdup() with strndup() throughout
[monky] / src / linux.c
index c4a00c8..c41841d 100644 (file)
@@ -8,7 +8,7 @@
  *
  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
  * Copyright (c) 2007 Toni Spets
- * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
  *     (see AUTHORS)
  * All rights reserved.
  *
@@ -73,11 +73,11 @@ static int show_nice_processes;
  * using a flag in this manner creates less confusing code. */
 static int prefer_proc = 0;
 
-void prepare_update()
+void prepare_update(void)
 {
 }
 
-void update_uptime()
+void update_uptime(void)
 {
 #ifdef HAVE_SYSINFO
        if (!prefer_proc) {
@@ -126,7 +126,7 @@ int check_mount(char *s)
 /* these things are also in sysinfo except Buffers:
  * (that's why I'm reading them from proc) */
 
-void update_meminfo()
+void update_meminfo(void)
 {
        FILE *meminfo_fp;
        static int rep = 0;
@@ -147,17 +147,17 @@ void update_meminfo()
                }
 
                if (strncmp(buf, "MemTotal:", 9) == 0) {
-                       sscanf(buf, "%*s %Lu", &info.memmax);
+                       sscanf(buf, "%*s %llu", &info.memmax);
                } else if (strncmp(buf, "MemFree:", 8) == 0) {
-                       sscanf(buf, "%*s %Lu", &info.mem);
+                       sscanf(buf, "%*s %llu", &info.mem);
                } else if (strncmp(buf, "SwapTotal:", 10) == 0) {
-                       sscanf(buf, "%*s %Lu", &info.swapmax);
+                       sscanf(buf, "%*s %llu", &info.swapmax);
                } else if (strncmp(buf, "SwapFree:", 9) == 0) {
-                       sscanf(buf, "%*s %Lu", &info.swap);
+                       sscanf(buf, "%*s %llu", &info.swap);
                } else if (strncmp(buf, "Buffers:", 8) == 0) {
-                       sscanf(buf, "%*s %Lu", &info.buffers);
+                       sscanf(buf, "%*s %llu", &info.buffers);
                } else if (strncmp(buf, "Cached:", 7) == 0) {
-                       sscanf(buf, "%*s %Lu", &info.cached);
+                       sscanf(buf, "%*s %llu", &info.cached);
                }
        }
 
@@ -171,7 +171,7 @@ void update_meminfo()
        fclose(meminfo_fp);
 }
 
-int get_laptop_mode()
+int get_laptop_mode(void)
 {
        FILE *fp;
        int val = -1;
@@ -192,22 +192,22 @@ char *get_ioscheduler(char *disk)
        char buf[128];
 
        if (!disk)
-               return "n/a";
+               return strndup("n/a", text_buffer_size);
 
        snprintf(buf, 127, "/sys/block/%s/queue/scheduler", disk);
        if ((fp = fopen(buf, "r")) == NULL) {
-               return strdup("n/a");
+               return strndup("n/a", text_buffer_size);
        }
        while (!feof(fp)) {
                fscanf(fp, "%127s", buf);
                if (buf[0] == '[') {
                        buf[strlen(buf) - 1] = '\0';
                        fclose(fp);
-                       return strdup(buf + 1);
+                       return strndup(buf + 1, text_buffer_size);
                }
        }
        fclose(fp);
-       return strdup("n/a");
+       return strndup("n/a", text_buffer_size);
 }
 
 int interface_up(const char *dev)
@@ -236,12 +236,12 @@ int interface_up(const char *dev)
 #define SAVE_SET_STRING(x, y) \
        if (x && strcmp((char *)x, (char *)y)) { \
                free(x); \
-               x = strdup("multiple"); \
+               x = strndup("multiple", text_buffer_size); \
        } else if (!x) { \
-               x = strdup(y); \
+               x = strndup(y, text_buffer_size); \
        }
 
-void update_gateway_info()
+void update_gateway_info(void)
 {
        FILE *fp;
        struct in_addr ina;
@@ -284,11 +284,11 @@ void update_gateway_info()
 CLOSE_FAIL:
        fclose(fp);
 FAIL:
-       info.gw_info.iface = info.gw_info.ip = strdup("failed");
+       info.gw_info.iface = info.gw_info.ip = strndup("failed", text_buffer_size);
        return;
 }
 
-inline void update_net_stats()
+inline void update_net_stats(void)
 {
        FILE *net_dev_fp;
        static int rep = 0;
@@ -327,7 +327,7 @@ inline void update_net_stats()
        for (i2 = 0; i2 < 16; i2++) {
                struct net_stat *ns;
                char *s, *p;
-        char temp_addr[17];
+               char temp_addr[17];
                long long r, t, last_recv, last_trans;
 
                if (fgets(buf, 255, net_dev_fp) == NULL) {
@@ -352,17 +352,17 @@ inline void update_net_stats()
                ns = get_net_stat(s);
                ns->up = 1;
                memset(&(ns->addr.sa_data), 0, 14);
-        if(NULL == ns->addrs)
-            ns->addrs = (char*) malloc(17 * 16);
-        if(NULL != ns->addrs)
-             memset(ns->addrs, 0, 17 * 16); /* Up to 17 chars per ip, max 16 interfaces. Nasty memory usage... */
+
+               if(NULL == ns->addrs)
+                       ns->addrs = (char*) malloc(17 * 16);
+               if(NULL != ns->addrs)
+                       memset(ns->addrs, 0, 17 * 16); /* Up to 17 chars per ip, max 16 interfaces. Nasty memory usage... */
+
                last_recv = ns->recv;
                last_trans = ns->trans;
 
                /* bytes packets errs drop fifo frame compressed multicast|bytes ... */
-               sscanf(p, "%Ld  %*d     %*d  %*d  %*d  %*d   %*d        %*d       %Ld",
+               sscanf(p, "%lld  %*d     %*d  %*d  %*d  %*d   %*d        %*d       %lld",
                        &r, &t);
 
                /* if recv or trans is less than last time, an overflow happened */
@@ -390,24 +390,23 @@ inline void update_net_stats()
                ioctl((long) i, SIOCGIFCONF, &conf);
 
                for (k = 0; k < conf.ifc_len / sizeof(struct ifreq); k++) {
-                       struct net_stat *ns;
+                       struct net_stat *ns2;
 
                        if (!(((struct ifreq *) conf.ifc_buf) + k))
                                break;
 
-                       ns = get_net_stat(
+                       ns2 = get_net_stat(
                                ((struct ifreq *) conf.ifc_buf)[k].ifr_ifrn.ifrn_name);
-                       ns->addr = ((struct ifreq *) conf.ifc_buf)[k].ifr_ifru.ifru_addr;
-           if(NULL != ns->addrs)
-           {
-               sprintf(temp_addr, "%u.%u.%u.%u, ",
-                   ns->addr.sa_data[2] & 255,
-                   ns->addr.sa_data[3] & 255,
-                   ns->addr.sa_data[4] & 255,
-                   ns->addr.sa_data[5] & 255);
-               if(NULL == strstr(ns->addrs, temp_addr))
-                   strncpy(ns->addrs + strlen(ns->addrs), temp_addr, 17);
-            }
+                       ns2->addr = ((struct ifreq *) conf.ifc_buf)[k].ifr_ifru.ifru_addr;
+                       if(NULL != ns2->addrs) {
+                               sprintf(temp_addr, "%u.%u.%u.%u, ",
+                                       ns2->addr.sa_data[2] & 255,
+                                       ns2->addr.sa_data[3] & 255,
+                                       ns2->addr.sa_data[4] & 255,
+                                       ns2->addr.sa_data[5] & 255);
+                               if(NULL == strstr(ns2->addrs, temp_addr))
+                                       strncpy(ns2->addrs + strlen(ns2->addrs), temp_addr, 17);
+                       }
                }
 
                close((long) i);
@@ -507,7 +506,7 @@ inline void update_net_stats()
 
 int result;
 
-void update_total_processes()
+void update_total_processes(void)
 {
 #ifdef HAVE_SYSINFO
        if (!prefer_proc) {
@@ -525,7 +524,7 @@ void update_total_processes()
                        info.procs = 0;
                        return;
                }
-               fscanf(fp, "%*f %*f %*f %*d/%hd", &info.procs);
+               fscanf(fp, "%*f %*f %*f %*d/%hu", &info.procs);
                fclose(fp);
        }
        info.mask |= (1 << INFO_PROCS);
@@ -566,15 +565,15 @@ void determine_longstat(char *buf)
        }
 }
 
-void get_cpu_count()
+void get_cpu_count(void)
 {
        FILE *stat_fp;
        static int rep = 0;
+       char buf[256];
 
        if (info.cpu_usage) {
                return;
        }
-       char buf[256];
 
        if (!(stat_fp = open_file("/proc/stat", &rep))) {
                return;
@@ -602,16 +601,16 @@ void get_cpu_count()
 #define TMPL_LONGSTAT "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
 #define TMPL_SHORTSTAT "%*s %llu %llu %llu %llu"
 
-inline static void update_stat()
+inline static void update_stat(void)
 {
        FILE *stat_fp;
        static int rep = 0;
        static struct cpu_info *cpu = NULL;
        char buf[256];
        unsigned int i;
-       unsigned int index;
+       unsigned int idx;
        double curtmp;
-       char *stat_template = NULL;
+       const char *stat_template = NULL;
        unsigned int malloc_cpu_size = 0;
 
        /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
@@ -639,7 +638,7 @@ inline static void update_stat()
                return;
        }
 
-       index = 0;
+       idx = 0;
        while (!feof(stat_fp)) {
                if (fgets(buf, 255, stat_fp) == NULL) {
                        break;
@@ -649,68 +648,69 @@ inline static void update_stat()
                        sscanf(buf, "%*s %hu", &info.run_procs);
                        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, &(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_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_active_total = cpu[index].cpu_total -
-                               (cpu[index].cpu_idle + cpu[index].cpu_iowait);
+                       double delta;
+                       idx = isdigit(buf[3]) ? ((int) buf[3]) - 0x2F : 0;
+                       sscanf(buf, stat_template, &(cpu[idx].cpu_user),
+                               &(cpu[idx].cpu_nice), &(cpu[idx].cpu_system),
+                               &(cpu[idx].cpu_idle), &(cpu[idx].cpu_iowait),
+                               &(cpu[idx].cpu_irq), &(cpu[idx].cpu_softirq),
+                               &(cpu[idx].cpu_steal));
+
+                       cpu[idx].cpu_total = cpu[idx].cpu_user + cpu[idx].cpu_nice +
+                               cpu[idx].cpu_system + cpu[idx].cpu_idle +
+                               cpu[idx].cpu_iowait + cpu[idx].cpu_irq +
+                               cpu[idx].cpu_softirq + cpu[idx].cpu_steal;
+
+                       cpu[idx].cpu_active_total = cpu[idx].cpu_total -
+                               (cpu[idx].cpu_idle + cpu[idx].cpu_iowait);
                        info.mask |= (1 << INFO_CPU);
 
-                       double delta = current_update_time - last_update_time;
+                       delta = current_update_time - last_update_time;
 
                        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[idx].cpu_val[0] = (cpu[idx].cpu_active_total -
+                               cpu[idx].cpu_last_active_total) /
+                               (float) (cpu[idx].cpu_total - cpu[idx].cpu_last_total);
                        curtmp = 0;
                        for (i = 0; i < info.cpu_avg_samples; i++) {
-                               curtmp += cpu[index].cpu_val[i];
+                               curtmp += cpu[idx].cpu_val[i];
                        }
                        /* 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 /
+                       /* if (idx == 0) {
+                               info.cpu_usage[idx] = curtmp / info.cpu_avg_samples /
                                        info.cpu_count;
                        } else {
-                               info.cpu_usage[index] = curtmp / info.cpu_avg_samples;
+                               info.cpu_usage[idx] = curtmp / info.cpu_avg_samples;
                        } */
                        /* TESTING -- this line replaces the prev. "suspect" if/else */
-                       info.cpu_usage[index] = curtmp / info.cpu_avg_samples;
+                       info.cpu_usage[idx] = curtmp / info.cpu_avg_samples;
 
-                       cpu[index].cpu_last_total = cpu[index].cpu_total;
-                       cpu[index].cpu_last_active_total = cpu[index].cpu_active_total;
+                       cpu[idx].cpu_last_total = cpu[idx].cpu_total;
+                       cpu[idx].cpu_last_active_total = cpu[idx].cpu_active_total;
                        for (i = info.cpu_avg_samples - 1; i > 0; i--) {
-                               cpu[index].cpu_val[i] = cpu[index].cpu_val[i - 1];
+                               cpu[idx].cpu_val[i] = cpu[idx].cpu_val[i - 1];
                        }
                }
        }
        fclose(stat_fp);
 }
 
-void update_running_processes()
+void update_running_processes(void)
 {
        update_stat();
 }
 
-void update_cpu_usage()
+void update_cpu_usage(void)
 {
        update_stat();
 }
 
-void update_load_average()
+void update_load_average(void)
 {
 #ifdef HAVE_GETLOADAVG
        if (!prefer_proc) {
@@ -740,7 +740,7 @@ void update_load_average()
 #define PROC_I8K "/proc/i8k"
 #define I8K_DELIM " "
 static char *i8k_procbuf = NULL;
-void update_i8k()
+void update_i8k(void)
 {
        FILE *fp;
 
@@ -815,7 +815,7 @@ static int get_first_file_in_a_directory(const char *dir, char *s, int *rep)
 }
 
 int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
-               int *div, char *devtype)
+               int *divisor, char *devtype)
 {
        char path[256];
        char buf[256];
@@ -868,9 +868,9 @@ int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
 
        if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0
                        || strcmp(type, "tempf") == 0) {
-               *div = 1;
+               *divisor = 1;
        } else {
-               *div = 0;
+               *divisor = 0;
        }
        /* fan does not use *_div as a read divisor */
        if (strcmp("fan", type) == 0) {
@@ -897,7 +897,7 @@ int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
                        ERR("open_sysfs_sensor(): can't read from sysfs");
                } else {
                        divbuf[divn] = '\0';
-                       *div = atoi(divbuf);
+                       *divisor = atoi(divbuf);
                }
        }
 
@@ -906,7 +906,7 @@ int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
        return fd;
 }
 
-double get_sysfs_info(int *fd, int div, char *devtype, char *type)
+double get_sysfs_info(int *fd, int divisor, char *devtype, char *type)
 {
        int val = 0;
 
@@ -947,17 +947,17 @@ double get_sysfs_info(int *fd, int div, char *devtype, char *type)
        /* divide voltage and temperature by 1000 */
        /* or if any other divisor is given, use that */
        if (strcmp(type, "tempf") == 0) {
-               if (div > 1) {
-                       return ((val / div + 40) * 9.0 / 5) - 40;
-               } else if (div) {
+               if (divisor > 1) {
+                       return ((val / divisor + 40) * 9.0 / 5) - 40;
+               } else if (divisor) {
                        return ((val / 1000.0 + 40) * 9.0 / 5) - 40;
                } else {
                        return ((val + 40) * 9.0 / 5) - 40;
                }
        } else {
-               if (div > 1) {
-                       return val / div;
-               } else if (div) {
+               if (divisor > 1) {
+                       return val / divisor;
+               } else if (divisor) {
                        return val / 1000.0;
                } else {
                        return val;
@@ -1032,7 +1032,7 @@ void get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
  * Small changes by David Sterba <sterd9am@ss1000.ms.mff.cuni.cz> */
 
 #if  defined(__i386) || defined(__x86_64)
-__inline__ unsigned long long int rdtsc()
+__inline__ unsigned long long int rdtsc(void)
 {
        unsigned long long int x;
 
@@ -1043,7 +1043,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;
@@ -1087,8 +1087,8 @@ void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
 #define CPUFREQ_POSTFIX "cpufreq/scaling_cur_freq"
 
 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
-char get_freq(char *p_client_buffer, size_t client_buffer_size, char *p_format,
-               int divisor, unsigned int cpu)
+char get_freq(char *p_client_buffer, size_t client_buffer_size,
+               const char *p_format, int divisor, unsigned int cpu)
 {
        FILE *f;
        static int rep = 0;
@@ -1186,7 +1186,7 @@ char get_freq(char *p_client_buffer, size_t client_buffer_size, char *p_format,
 
 /* 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)
+               const char *p_format, int divisor, unsigned int cpu)
 {
        FILE *f;
        char s[256];
@@ -1469,9 +1469,9 @@ present voltage:         16608 mV
 #define APM_PATH "/proc/apm"
 #define MAX_BATTERY_COUNT 4
 
-static FILE *sysfs_bat_fp[MAX_BATTERY_COUNT];
-static FILE *acpi_bat_fp[MAX_BATTERY_COUNT];
-static FILE *apm_bat_fp[MAX_BATTERY_COUNT];
+static FILE *sysfs_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
+static FILE *acpi_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
+static FILE *apm_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
 
 static int batteries_initialized = 0;
 static char batteries[MAX_BATTERY_COUNT][32];
@@ -1520,13 +1520,13 @@ int get_battery_idx(const char *bat)
        return idx;
 }
 
-void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
+void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
 {
        static int idx, rep = 0, rep2 = 0;
        char acpi_path[128];
+       char sysfs_path[128];
 
        snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
-       char sysfs_path[128];
        snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat);
 
        init_batteries();
@@ -1545,11 +1545,14 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
 
        /* first try SYSFS if that fails try ACPI */
  
-       if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL)
+       if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
                sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
+               rep = 0;
+       }
   
-       if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL)
+       if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
                acpi_bat_fp[idx] = open_file(acpi_path, &rep);
+       }
   
        if (sysfs_bat_fp[idx] != NULL) {
                /* SYSFS */
@@ -1558,7 +1561,7 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
                char charging_state[64];
                char present[4];
  
-               strcpy(charging_state, "Unknown");
+               strcpy(charging_state, "unknown");
  
                while (!feof(sysfs_bat_fp[idx])) {
                        char buf[256];
@@ -1567,9 +1570,9 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
  
                        /* let's just hope units are ok */
                        if (strncmp (buf, "POWER_SUPPLY_PRESENT=1", 22) == 0)
-                               strcpy(present, "Yes");
+                               strcpy(present, "yes");
                        else if (strncmp (buf, "POWER_SUPPLY_PRESENT=0", 22) == 0)
-                               strcpy(present, "No");
+                               strcpy(present, "no");
                        else if (strncmp (buf, "POWER_SUPPLY_STATUS=", 20) == 0)
                                sscanf(buf, "POWER_SUPPLY_STATUS=%63s", charging_state);
                        /* present_rate is not the same as the
@@ -1603,33 +1606,41 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
                else if (strcmp(charging_state, "Charging") == 0) {
                        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%%",
+                               snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%",
                                        (int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 ));
                                /* e.g. 2h 37m */
                                format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1,
                                              (long) (((float)(acpi_last_full[idx] - remaining_capacity) / present_rate) * 3600));
                        } else if (acpi_last_full[idx] != 0 && present_rate <= 0) {
-                               snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "Charging %d%%",
+                               snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %d%%",
                                        (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
+                               snprintf(last_battery_time_str[idx],
+                                       sizeof(last_battery_time_str[idx]) - 1, "unknown");
                        } else {
-                               strncpy(last_battery_str[idx], "Charging", sizeof(last_battery_str[idx])-1);
+                               strncpy(last_battery_str[idx], "charging", sizeof(last_battery_str[idx])-1);
+                               snprintf(last_battery_time_str[idx],
+                                       sizeof(last_battery_time_str[idx]) - 1, "unknown");
                        }
                }
                /* discharging */
                else if (strncmp(charging_state, "Discharging", 64) == 0) {
                        if (present_rate > 0) {
                                /* e.g. discharging 35% */
-                               snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "Discharging %i%%",
+                               snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%",
                                        (int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 ));
                                /* e.g. 1h 12m */
                                format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1,
                                              (long) (((float)(acpi_last_full[idx] - remaining_capacity) / present_rate) * 3600));
                        } else if (present_rate == 0) { /* Thanks to Nexox for this one */
-                               snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "Full");
+                               snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "full");
+                               snprintf(last_battery_time_str[idx],
+                                       sizeof(last_battery_time_str[idx]) - 1, "unknown");
                        } else {
                                snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1,
-                                       "Discharging %d%%",
+                                       "discharging %d%%",
                                        (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
+                               snprintf(last_battery_time_str[idx],
+                                       sizeof(last_battery_time_str[idx]) - 1, "unknown");
                        }
                }
                /* charged */
@@ -1639,15 +1650,15 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
                                 * when the second one is empty and the first one
                                 * being charged. */
                                if (remaining_capacity == 0)
-                                       strcpy(last_battery_str[idx], "Empty");
+                                       strcpy(last_battery_str[idx], "empty");
                                else
-                                       strcpy(last_battery_str[idx], "Charged");
+                                       strcpy(last_battery_str[idx], "charged");
                }
                /* unknown, probably full / AC */
                else {
                        if (acpi_last_full[idx] != 0
                            && remaining_capacity != acpi_last_full[idx])
-                               snprintf(last_battery_str[idx], 64, "Unknown %d%%",
+                               snprintf(last_battery_str[idx], 64, "unknown %d%%",
                                        (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
                        else
                                strncpy(last_battery_str[idx], "AC", 64);
@@ -1661,12 +1672,12 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
 
                /* read last full capacity if it's zero */
                if (acpi_last_full[idx] == 0) {
-                       static int rep = 0;
+                       static int rep3 = 0;
                        char path[128];
                        FILE *fp;
 
                        snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
-                       fp = open_file(path, &rep);
+                       fp = open_file(path, &rep3);
                        if (fp != NULL) {
                                while (!feof(fp)) {
                                        char b[256];
@@ -1731,9 +1742,13 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
                                snprintf(last_battery_str[idx],
                                        sizeof(last_battery_str[idx]) - 1, "charging %d%%",
                                        (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
+                               snprintf(last_battery_time_str[idx],
+                                       sizeof(last_battery_time_str[idx]) - 1, "unknown");
                        } else {
                                strncpy(last_battery_str[idx], "charging",
                                        sizeof(last_battery_str[idx]) - 1);
+                               snprintf(last_battery_time_str[idx],
+                                       sizeof(last_battery_time_str[idx]) - 1, "unknown");
                        }
                /* discharging */
                } else if (strncmp(charging_state, "discharging", 64) == 0) {
@@ -1749,10 +1764,14 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
                        } else if (present_rate == 0) { /* Thanks to Nexox for this one */
                                snprintf(last_battery_str[idx],
                                        sizeof(last_battery_str[idx]) - 1, "full");
+                               snprintf(last_battery_time_str[idx],
+                                       sizeof(last_battery_time_str[idx]) - 1, "unknown");
                        } else {
                                snprintf(last_battery_str[idx],
                                        sizeof(last_battery_str[idx]) - 1, "discharging %d%%",
                                        (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
+                               snprintf(last_battery_time_str[idx],
+                                       sizeof(last_battery_time_str[idx]) - 1, "unknown");
                        }
                /* charged */
                } else if (strncmp(charging_state, "charged", 64) == 0) {
@@ -1781,7 +1800,8 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
                }
 
                if (apm_bat_fp[idx] != NULL) {
-                       int ac, status, flag, life;
+                       unsigned int ac, status, flag;
+                       int life;
 
                        fscanf(apm_bat_fp[idx], "%*s %*s %*x %x   %x       %x     %d%%",
                                &ac, &status, &flag, &life);
@@ -1807,10 +1827,10 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
 set_return_value:
        switch (item) {
                case BATTERY_STATUS:
-                       snprintf(buf, n, "%s", last_battery_str[idx]);
+                       snprintf(buffer, n, "%s", last_battery_str[idx]);
                        break;
                case BATTERY_TIME:
-                       snprintf(buf, n, "%s", last_battery_time_str[idx]);
+                       snprintf(buffer, n, "%s", last_battery_time_str[idx]);
                        break;
                default:
                        break;
@@ -1819,12 +1839,13 @@ set_return_value:
 
 int get_battery_perct(const char *bat)
 {
-       static int rep;
+       static int rep = 0;
        int idx;
        char acpi_path[128];
+       char sysfs_path[128];
+       int remaining_capacity = -1;
 
        snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
-       char sysfs_path[128];
        snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat);
 
        init_batteries();
@@ -1839,13 +1860,14 @@ int get_battery_perct(const char *bat)
 
        /* Only check for SYSFS or ACPI */
 
-       if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL)
+       if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
                sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
+               rep = 0;
+       }
 
-       if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL)
+       if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
                acpi_bat_fp[idx] = open_file(acpi_path, &rep);
-
-       int remaining_capacity = -1;
+       }
 
        if (sysfs_bat_fp[idx] != NULL) {
                /* SYSFS */
@@ -1872,12 +1894,12 @@ int get_battery_perct(const char *bat)
                /* ACPI */
                /* read last full capacity if it's zero */
                if (acpi_design_capacity[idx] == 0) {
-                       static int rep;
+                       static int rep2;
                        char path[128];
                        FILE *fp;
 
                        snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
-                       fp = open_file(path, &rep);
+                       fp = open_file(path, &rep2);
                        if (fp != NULL) {
                                while (!feof(fp)) {
                                        char b[256];
@@ -1951,17 +1973,18 @@ 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)
+void get_powerbook_batt_info(char *buffer, size_t n, int i)
 {
        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;
-       long time = -1;
+       unsigned int flags;
+       int charge, max_charge, ac = -1;
+       long timeval = -1;
 
        /* don't update battery too often */
        if (current_update_time - pb_battery_info_update < 29.5) {
-               snprintf(buf, n, "%s", pb_battery_info[i]);
+               snprintf(buffer, n, "%s", pb_battery_info[i]);
                return;
        }
        pb_battery_info_update = current_update_time;
@@ -1986,7 +2009,7 @@ void get_powerbook_batt_info(char *buf, size_t n, int i)
                        } else if (buf[0] == 'm') {
                                sscanf(buf, "max_charge : %d", &max_charge);
                        } else if (buf[0] == 't') {
-                               sscanf(buf, "time rem.  : %ld", &time);
+                               sscanf(buf, "time rem.  : %ld", &timeval);
                        }
                }
        }
@@ -2020,7 +2043,7 @@ void get_powerbook_batt_info(char *buf, size_t n, int i)
        }
 
        /* update percentage string */
-       if (time == 0) {
+       if (timeval == 0) {
                pb_battery_info[PB_BATT_PERCENT][0] = 0;
        } else {
                snprintf(pb_battery_info[PB_BATT_PERCENT],
@@ -2029,20 +2052,20 @@ void get_powerbook_batt_info(char *buf, size_t n, int i)
        }
 
        /* update time string */
-       if (time == 0) {                        /* fully charged or battery not present */
+       if (timeval == 0) {                     /* fully charged or battery not present */
                pb_battery_info[PB_BATT_TIME][0] = 0;
-       } else if (time < 60 * 60) {    /* don't show secs */
+       } else if (timeval < 60 * 60) { /* don't show secs */
                format_seconds_short(pb_battery_info[PB_BATT_TIME],
-                       sizeof(pb_battery_info[PB_BATT_TIME]), time);
+                       sizeof(pb_battery_info[PB_BATT_TIME]), timeval);
        } else {
                format_seconds(pb_battery_info[PB_BATT_TIME],
-                       sizeof(pb_battery_info[PB_BATT_TIME]), time);
+                       sizeof(pb_battery_info[PB_BATT_TIME]), timeval);
        }
 
-       snprintf(buf, n, "%s", pb_battery_info[i]);
+       snprintf(buffer, n, "%s", pb_battery_info[i]);
 }
 
-void update_top()
+void update_top(void)
 {
        show_nice_processes = 1;
        process_find_top(info.cpu, info.memu);
@@ -2064,7 +2087,7 @@ void update_top()
 #define NBD_MAJOR 43
 #endif
 
-void update_diskio()
+void update_diskio(void)
 {
        static unsigned int last = UINT_MAX;
        static unsigned int last_read = UINT_MAX;
@@ -2073,12 +2096,14 @@ void update_diskio()
        static int rep = 0;
 
        char buf[512], devbuf[64];
-       int major, minor, i;
+       int i;
+       unsigned 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;
+       int tot, tot_read, tot_write;
 
        if (!(fp = open_file("/proc/diskstats", &rep))) {
                diskio_value = 0;
@@ -2109,7 +2134,7 @@ void update_diskio()
                }
                for (i = 0; i < MAX_DISKIO_STATS; i++) {
                        if (diskio_stats[i].dev &&
-                                       strcmp(devbuf, diskio_stats[i].dev) == 0) {
+                                       strncmp(devbuf, diskio_stats[i].dev, text_buffer_size) == 0) {
                                diskio_stats[i].current =
                                        (reads + writes - diskio_stats[i].last) / 2;
                                diskio_stats[i].current_read =
@@ -2137,9 +2162,9 @@ void update_diskio()
        /* since the values in /proc/diststats are absolute, we have to substract
         * our last reading. The numbers stand for "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);
+       tot = ((double) (current - last) / 2);
+       tot_read = ((double) (current_read - last_read) / 2);
+       tot_write = ((double) (current_write - last_write) / 2);
 
        if (last_read > current_read) {
                tot_read = 0;
@@ -2198,14 +2223,14 @@ commands:       enable, disable
 
 void get_ibm_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
 {
-       if (!p_client_buffer || client_buffer_size <= 0) {
-               return;
-       }
-
        FILE *fp;
        unsigned int speed = 0;
        char fan[128];
 
+       if (!p_client_buffer || client_buffer_size <= 0) {
+               return;
+       }
+
        snprintf(fan, 127, "%s/fan", IBM_ACPI_DIR);
 
        fp = fopen(fan, "r");
@@ -2216,7 +2241,7 @@ void get_ibm_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
                        if (fgets(line, 255, fp) == NULL) {
                                break;
                        }
-                       if (sscanf(line, "speed: %d", &speed)) {
+                       if (sscanf(line, "speed: %u", &speed)) {
                                break;
                        }
                }
@@ -2251,9 +2276,12 @@ temperatures:   41 43 31 46 33 -128 29 -128
  * Peter Tarjan (ptarjan@citromail.hu) */
 
 static double last_ibm_acpi_temp_time;
-void get_ibm_acpi_temps()
+void get_ibm_acpi_temps(void)
 {
 
+       FILE *fp;
+       char thermal[128];
+
        /* don't update too often */
        if (current_update_time - last_ibm_acpi_temp_time < 10.00) {
                return;
@@ -2264,10 +2292,6 @@ void get_ibm_acpi_temps()
                return;
        } */
 
-       FILE *fp;
-
-       char thermal[128];
-
        snprintf(thermal, 127, "%s/thermal", IBM_ACPI_DIR);
        fp = fopen(thermal, "r");
 
@@ -2305,17 +2329,16 @@ commands:       level <level> (<level> is 0-15)
 
 void get_ibm_acpi_volume(char *p_client_buffer, size_t client_buffer_size)
 {
+       FILE *fp;
+       char volume[128];
+       unsigned int vol = -1;
+       char mute[3] = "";
+
        if (!p_client_buffer || client_buffer_size <= 0) {
                return;
        }
 
-       FILE *fp;
-
-       char volume[128];
-
        snprintf(volume, 127, "%s/volume", IBM_ACPI_DIR);
-       unsigned int vol = -1;
-       char mute[3] = "";
 
        fp = fopen(volume, "r");
        if (fp != NULL) {
@@ -2326,7 +2349,7 @@ void get_ibm_acpi_volume(char *p_client_buffer, size_t client_buffer_size)
                        if (fgets(line, 255, fp) == NULL) {
                                break;
                        }
-                       if (sscanf(line, "level: %d", &read_vol)) {
+                       if (sscanf(line, "level: %u", &read_vol)) {
                                vol = read_vol;
                                continue;
                        }
@@ -2361,14 +2384,14 @@ commands:       level <level> (<level> is 0-7)
 
 void get_ibm_acpi_brightness(char *p_client_buffer, size_t client_buffer_size)
 {
-       if (!p_client_buffer || client_buffer_size <= 0) {
-               return;
-       }
-
        FILE *fp;
        unsigned int brightness = 0;
        char filename[128];
 
+       if (!p_client_buffer || client_buffer_size <= 0) {
+               return;
+       }
+
        snprintf(filename, 127, "%s/brightness", IBM_ACPI_DIR);
 
        fp = fopen(filename, "r");
@@ -2379,7 +2402,7 @@ void get_ibm_acpi_brightness(char *p_client_buffer, size_t client_buffer_size)
                        if (fgets(line, 255, fp) == NULL) {
                                break;
                        }
-                       if (sscanf(line, "level: %d", &brightness)) {
+                       if (sscanf(line, "level: %u", &brightness)) {
                                break;
                        }
                }
@@ -2421,20 +2444,20 @@ void update_entropy(void)
        info.mask |= (1 << INFO_ENTROPY);
 }
 
-char *get_disk_protect_queue(char *disk)
+const char *get_disk_protect_queue(const char *disk)
 {
-  FILE *fp;
-  char path[128];
-  int state;
-
-  snprintf(path, 127, "/sys/block/%s/queue/protect", disk);
-  if ((fp = fopen(path, "r")) == NULL)
-     return "n/a   ";
-  if (fscanf(fp, "%d\n", &state) != 1) {
-     fclose(fp);
-     return "failed";
-  }
-  fclose(fp);
-  return state ? "frozen" : "free  ";
+       FILE *fp;
+       char path[128];
+       int state;
+
+       snprintf(path, 127, "/sys/block/%s/queue/protect", disk);
+       if ((fp = fopen(path, "r")) == NULL)
+               return "n/a   ";
+       if (fscanf(fp, "%d\n", &state) != 1) {
+               fclose(fp);
+               return "failed";
+       }
+       fclose(fp);
+       return state ? "frozen" : "free  ";
 }