Improve behaviour of $pb_battery.
authorBrenden Matthews <brenden@rty.ca>
Sun, 14 Dec 2008 03:55:45 +0000 (20:55 -0700)
committerBrenden Matthews <brenden@rty.ca>
Sun, 14 Dec 2008 03:55:45 +0000 (20:55 -0700)
ChangeLog
src/linux.c

index 0c32368..6790909 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,6 @@
 2008-12-14
        * Fix disk_protect for linux-2.6.27 (and hopefully above)
+       * Improve behaviour of $pb_battery
 
 2008-12-09
        * Add an option to timed_thread_test() to not wait for the interval period
index ab4d709..cd9599e 100644 (file)
@@ -1996,6 +1996,9 @@ void get_powerbook_batt_info(char *buffer, size_t n, int i)
 
        if (pmu_battery_fp == NULL) {
                pmu_battery_fp = open_file(batt_path, &rep);
+               if (pmu_battery_fp == NULL) {
+                       return;
+               }
        }
 
        if (pmu_battery_fp != NULL) {
@@ -2020,6 +2023,9 @@ void get_powerbook_batt_info(char *buffer, size_t n, int i)
        }
        if (pmu_info_fp == NULL) {
                pmu_info_fp = open_file(info_path, &rep);
+               if (pmu_info_fp == NULL) {
+                       return;
+               }
        }
 
        if (pmu_info_fp != NULL) {
@@ -2037,19 +2043,24 @@ void get_powerbook_batt_info(char *buffer, size_t n, int i)
        }
        /* update status string */
        if ((ac && !(flags & PMU_BATT_PRESENT))) {
-               strcpy(pb_battery_info[PB_BATT_STATUS], "AC");
+               strncpy(pb_battery_info[PB_BATT_STATUS], "AC", sizeof(pb_battery_info[PB_BATT_STATUS]));
        } else if (ac && (flags & PMU_BATT_PRESENT)
                        && !(flags & PMU_BATT_CHARGING)) {
-               strcpy(pb_battery_info[PB_BATT_STATUS], "charged");
+               strncpy(pb_battery_info[PB_BATT_STATUS], "charged", sizeof(pb_battery_info[PB_BATT_STATUS]));
        } else if ((flags & PMU_BATT_PRESENT) && (flags & PMU_BATT_CHARGING)) {
-               strcpy(pb_battery_info[PB_BATT_STATUS], "charging");
+               strncpy(pb_battery_info[PB_BATT_STATUS], "charging", sizeof(pb_battery_info[PB_BATT_STATUS]));
        } else {
-               strcpy(pb_battery_info[PB_BATT_STATUS], "discharging");
+               strncpy(pb_battery_info[PB_BATT_STATUS], "discharging", sizeof(pb_battery_info[PB_BATT_STATUS]));
        }
 
        /* update percentage string */
-       if (timeval == 0) {
-               pb_battery_info[PB_BATT_PERCENT][0] = 0;
+       if (timeval == 0 && ac && (flags & PMU_BATT_PRESENT)
+                       && !(flags & PMU_BATT_CHARGING)) {
+               snprintf(pb_battery_info[PB_BATT_PERCENT],
+                       sizeof(pb_battery_info[PB_BATT_PERCENT]), "100%%");
+       } else if (timeval == 0) {
+               snprintf(pb_battery_info[PB_BATT_PERCENT],
+                       sizeof(pb_battery_info[PB_BATT_PERCENT]), "unknown");
        } else {
                snprintf(pb_battery_info[PB_BATT_PERCENT],
                        sizeof(pb_battery_info[PB_BATT_PERCENT]), "%d%%",
@@ -2058,7 +2069,8 @@ void get_powerbook_batt_info(char *buffer, size_t n, int i)
 
        /* update time string */
        if (timeval == 0) {                     /* fully charged or battery not present */
-               pb_battery_info[PB_BATT_TIME][0] = 0;
+               snprintf(pb_battery_info[PB_BATT_TIME],
+                       sizeof(pb_battery_info[PB_BATT_TIME]), "unknown");
        } 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]), timeval);