From 976d4c669b44a51d80bb2eb62f711ad887ab7a81 Mon Sep 17 00:00:00 2001 From: Mykyta Holubakha Date: Wed, 18 Jan 2017 12:28:52 +0200 Subject: Fix logical errors Restore device data only after successful read Do not set curr_brightness to max after an unsuccessful restore Fix ensure_dir Do not ignore ignore value of fread (fixes #6) --- brightnessctl.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'brightnessctl.c') diff --git a/brightnessctl.c b/brightnessctl.c index 49834f7..cd65d25 100644 --- a/brightnessctl.c +++ b/brightnessctl.c @@ -187,8 +187,8 @@ int main(int argc, char **argv) { if (save_device_data(dev)) fprintf(stderr, "Could not save data for device '%s'.\n", dev_name); if (p.restore) { - restore_device_data(dev); - write_device(dev); + if (restore_device_data(dev)) + write_device(dev); } return apply_operation(dev, p.operation, &p.val); } @@ -449,18 +449,20 @@ int restore_device_data(struct device *dev) { errno = 0; if (!(fp = fopen(filename, "r"))) goto fail; - fread(buf, 15, 1, fp); - fclose(fp); + if (!fread(buf, 1, 15, fp)) + goto rfail; dev->curr_brightness = strtol(buf, &end, 10); if (end == buf) errno = EINVAL; +rfail: + fclose(fp); fail: + free(filename); if (errno) { perror("Error restoring device data"); - dev->curr_brightness = dev->max_brightness; + return 0; } - free(filename); - return errno; + return 1; } @@ -470,10 +472,10 @@ int ensure_dir(char *dir) { if (errno != ENOENT) return 0; errno = 0; - if (mkdir(run_dir, 0777)) { + if (mkdir(dir, 0777)) { return 0; } - if (stat(run_dir, &sb)) + if (stat(dir, &sb)) return 0; } if (!S_ISDIR(sb.st_mode)) { -- cgit v1.2.3