- /*
- * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
- * [11:00] <@tokkee> present: yes
- * [11:00] <@tokkee> capacity state: ok
- * [11:00] <@tokkee> charging state: charging
- * [11:00] <@tokkee> present rate: 1724 mA
- * [11:00] <@tokkee> remaining capacity: 4136 mAh
- * [11:00] <@tokkee> present voltage: 12428 mV
- */
- syslog (LOG_DEBUG, "Found directory `/proc/acpi/battery'");
+ double current = INVALID_VALUE;
+ double voltage = INVALID_VALUE;
+ double charge = INVALID_VALUE;
+ double *valptr = NULL;
+ int charging = 0;
+
+ struct dirent *ent;
+ DIR *dh;
+
+ if ((dh = opendir ("/proc/acpi/battery")) == NULL)
+ {
+ syslog (LOG_ERR, "Cannot open `/proc/acpi/battery': %s", strerror (errno));
+ return;
+ }
+
+ while ((ent = readdir (dh)) != NULL)
+ {
+ len = snprintf (filename, BUFSIZE, "/proc/acpi/battery/%s/state", ent->d_name);
+ if ((len >= BUFSIZE) || (len < 0))
+ continue;
+
+ if ((fh = fopen (filename, "r")) == NULL)
+ {
+ syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
+ continue;
+ }
+
+ /*
+ * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
+ * [11:00] <@tokkee> present: yes
+ * [11:00] <@tokkee> capacity state: ok
+ * [11:00] <@tokkee> charging state: charging
+ * [11:00] <@tokkee> present rate: 1724 mA
+ * [11:00] <@tokkee> remaining capacity: 4136 mAh
+ * [11:00] <@tokkee> present voltage: 12428 mV
+ */
+ while (fgets (buffer, BUFSIZE, fh) == NULL)
+ {
+ numfields = strsplit (buffer, fields, 8);
+
+ if (numfields < 3)
+ continue;
+
+ if ((strcmp (fields[0], "present"))
+ && (strcmp (fields[1], "rate:")))
+ valptr = ¤t;
+ else if ((strcmp (fields[0], "remaining"))
+ && (strcmp (fields[1], "capacity:")))
+ valptr = &charge;
+ else if ((strcmp (fields[0], "present"))
+ && (strcmp (fields[1], "voltage:")))
+ valptr = &voltage;
+ else
+ valptr = NULL;
+
+ if ((strcmp (fields[0], "charging"))
+ && (strcmp (fields[1], "state:")))
+ {
+ if (strcmp (fields[2], "charging"))
+ charging = 1;
+ else
+ charging = 0;
+ }
+
+ if (valptr != NULL)
+ {
+ char *endptr;
+
+ endptr = NULL;
+ errno = 0;
+
+ *valptr = strtod (fields[2], &endptr) / 1000.0;
+
+ if ((fields[2] == endptr) || (errno != 0))
+ *valptr = INVALID_VALUE;
+ }
+ }
+
+ if ((current != INVALID_VALUE) && (charging == 0))
+ current *= -1;
+
+ if ((current != INVALID_VALUE)
+ || (voltage != INVALID_VALUE)
+ || (charge != INVALID_VALUE))
+ battery_submit (ent->d_name, current, voltage, charge);
+
+ fclose (fh);
+ }
+
+ closedir (dh);