+static char *parse_line(char *line)
+{
+ char *t = strchr(line, '\n');
+ if (t)
+ *t = 0;
+ char *val = strchr(line, ':');
+ if (!val)
+ return NULL;
+ *val++ = 0;
+ while (*val == ' ' || *val == '\t')
+ *val++ = 0;
+ return val;
+}
+
+static void scan_ac(void)
+{
+ ac_online = 0;
+
+ const char dir[] = "/proc/acpi/ac_adapter";
+ DIR *d = opendir(dir);
+ if (!d)
+ return;
+
+ struct dirent *e;
+ while (e = readdir(d))
+ {
+ if (e->d_name[0] == '.')
+ continue;
+ char n[sizeof(dir) + 1 + strlen(e->d_name) + 6];
+ sprintf(n, "%s/%s/state", dir, e->d_name);
+ FILE *f = fopen(n, "r");
+ if (!f)
+ continue;
+ char line[1024];
+ while (fgets(line, sizeof(line)-1, f))
+ {
+ char *val = parse_line(line);
+ if (!val)
+ continue;
+ if (!strcmp(line, "state") && !strcmp(val, "on-line"))
+ ac_online = 1;
+ }
+ fclose(f);
+ }
+ closedir(d);
+}
+