diff options
author | Tim Keller <tjkeller.xyz> | 2024-11-21 21:10:46 -0600 |
---|---|---|
committer | Tim Keller <tjkeller.xyz> | 2024-11-21 21:10:46 -0600 |
commit | 7f331782d9aa176e63fa1bddf88a0cccbe2096df (patch) | |
tree | bd74a34df71fec2ac6c791a475fef42a5bf4ffdf /lowbat.c | |
parent | 3ab85464ff92c57a33945a32a8a00df5a9abb684 (diff) | |
download | lowbat-master.tar.xz lowbat-master.zip |
Diffstat (limited to 'lowbat.c')
-rw-r--r-- | lowbat.c | 12 |
1 files changed, 8 insertions, 4 deletions
@@ -21,7 +21,7 @@ static char batteries[MAX_BATTERIES][MAX_PSU_NAME_LEN]; static unsigned int num_batteries; static FILE *fp; static char prop_path[MAX_PROP_PATH_LEN]; -static enum StatusSymbols { UNKNOWN = '?', CHARGING = '+', DISCHARGING = '-', NOT_CHARGING = '=', FULL = '=' }; +enum StatusSymbols { UNKNOWN = '?', CHARGING = '+', DISCHARGING = '-', NOT_CHARGING = '=', FULL = '=' }; // functions void load_installed_batteries() { @@ -60,7 +60,8 @@ void load_installed_batteries() { fprintf(stderr, "maximum number of batteries (8) exceeded\n"); exit(EXIT_FAILURE); } - strncpy(batteries[num_batteries], entry->d_name, MAX_PSU_NAME_LEN); + memcpy(batteries[num_batteries], entry->d_name, MAX_PSU_NAME_LEN); + batteries[num_batteries][MAX_PSU_NAME_LEN-1] = '\0'; num_batteries++; break; } @@ -86,11 +87,14 @@ void read_prop(char *psu, char *prop, char *fmt, void *out) { snprintf(prop_path, MAX_PROP_PATH_LEN, PSUPATH "/%s/%s", psu, prop); if ((fp = fopen(prop_path, "r")) != NULL) { - fscanf(fp, fmt, out); + if (fscanf(fp, fmt, out) != 1) { + fprintf(stderr, "failed to scan property %s with format '%s'\n", fmt, prop_path); + exit(EXIT_FAILURE); + } fclose(fp); } else { - fprintf(stderr, "failed to read fmt '%s' from property %s\n", fmt, prop_path); + fprintf(stderr, "failed to open %s\n", prop_path); exit(EXIT_FAILURE); } } |