Files
event-handler/src/battery.c
Squibid 22fcfcdba9 fix: don't leak memory...
There's still memory being leaked by the wayland stuff
2025-09-26 16:45:31 -04:00

61 lines
960 B
C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include "log.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_status_path, "/capacity");
f = fopen(d, "r");
if (!f) {
log_fatal("battery directory does not exist");
free(d);
return NULL;
}
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';
}