aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Mykyta Holubakha <hilobakho@gmail.com>2017-01-18 12:28:52 +0200
committerGravatar Mykyta Holubakha <hilobakho@gmail.com>2017-01-18 12:28:52 +0200
commit976d4c669b44a51d80bb2eb62f711ad887ab7a81 (patch)
treebf1c882d95bdfc3bfae14579b0f37adcfc011d16
parenteebf093c9cff3c48b090a7ff67c8bd6ba1c64704 (diff)
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)
-rw-r--r--brightnessctl.c20
1 files changed, 11 insertions, 9 deletions
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)) {