From 9eee34e4f023732b9d17abddc03fa6f1faff6264 Mon Sep 17 00:00:00 2001 From: Mykyta Holubakha Date: Sun, 22 Sep 2019 20:06:10 +0300 Subject: Improve saving device data --- brightnessctl.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/brightnessctl.c b/brightnessctl.c index 8f73067..a210b87 100644 --- a/brightnessctl.c +++ b/brightnessctl.c @@ -503,8 +503,7 @@ int read_devices(struct device **devs) { bool save_device_data(struct device *dev) { char c[16]; size_t s = sprintf(c, "%u", dev->curr_brightness); - char *c_path = dir_child(run_dir, dev->class); - char *d_path = dir_child(c_path, dev->id); + char *d_path = cat_with('/', run_dir, dev->class, dev->id); FILE *fp; mode_t old = 0; int error = 0; @@ -514,9 +513,7 @@ bool save_device_data(struct device *dev) { error++; goto fail; } - if (!ensure_run_dir()) - goto fail; - if (!ensure_dir(c_path)) + if (!ensure_dev_dir(dev)) goto fail; old = umask(0); fp = fopen(d_path, "w"); @@ -529,7 +526,6 @@ bool save_device_data(struct device *dev) { } fclose(fp); fail: - free(c_path); free(d_path); if (errno) { perror("Error saving device data"); @@ -583,6 +579,17 @@ bool ensure_dir(char *dir) { return true; } +bool ensure_dev_dir(struct device *dev) { + char *cpath; + bool ret; + if (!ensure_run_dir()) + return false; + cpath = dir_child(run_dir, dev->class); + ret = ensure_dir(cpath); + free(cpath); + return ret; +} + char *_cat_with(char c, ...) { size_t size = 32; size_t length = 0; -- cgit v1.2.3