seperate out the battery funcs

This commit is contained in:
2025-09-23 02:20:07 -04:00
parent bfe23c60fd
commit b4715770db
3 changed files with 61 additions and 47 deletions

54
src/battery.c Normal file
View File

@@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include "utils.h"
#include "config.h"
char
*battery_percent(void)
{
char *r, c, *d;
int i;
FILE *f;
/* open up the battery capacity for reading */
d = concat(battery_dir, "/capacity");
f = fopen(d, "r");
free(d);
/* create enough space for the battery percentage */
r = calloc(4, sizeof(char));
/* read in the battery percentage */
i = 0;
while ((c = getc(f)) && i < 4) {
/* make sure to replace the newline with a string terminator */
if (c == '\n') {
r[i] = '\0';
break;
}
r[i] = c;
i++;
}
fclose(f);
return r;
}
bool
plugged_in(char *fn)
{
FILE *f;
char buf[1];
f = fopen(fn, "r");
if (read(fileno(f), buf, 1) < 0) {
return -1;
}
fclose(f);
return buf[0] - '0';
}