39 lines
790 B
C
39 lines
790 B
C
/* see LICENSE file for details on license */
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
|
|
#include "module.h"
|
|
#include "battery.h"
|
|
|
|
void populate_buffer(char *buffer, char pattern) {
|
|
/* XXX: we assume the buffer is at least four bytes long */
|
|
|
|
int i;
|
|
for(i = 0; i < 3; i++) *(buffer + i) = pattern;
|
|
*(buffer + i + 1) = '\0';
|
|
}
|
|
|
|
|
|
int battery_update(struct module *module) {
|
|
char *filepath = BATTERY_PRE BATTERY_DIR BATTERY_CAP;
|
|
int i;
|
|
|
|
if (!access(filepath, F_OK) == 0) {
|
|
populate_buffer((char *)&module->buffer, '?');
|
|
return 1;
|
|
}
|
|
|
|
FILE *file = fopen(filepath, "r");
|
|
i = fread(&module->buffer, 1, MODULE_BUFFER_LEN, file);
|
|
module->buffer[i - 1] = '\0';
|
|
|
|
if(ferror(file)) {
|
|
populate_buffer((char *)&module->buffer, '!');
|
|
return 1;
|
|
}
|
|
|
|
module->buffer[i] = '\0';
|
|
|
|
return 0;
|
|
}
|