From 4aa6d8fff1e1e034c1a73d02d3515df36b93bbaf Mon Sep 17 00:00:00 2001 From: Miciah Masters Date: Sun, 15 Jan 2017 02:18:49 -0500 Subject: Print error if run dir is not a directory Print an error message if the run directory or the device-class subdirectory exists but is not a directory. Before: % rm -rf /tmp/brightnessctl % touch /tmp/brightnessctl % brightnessctl -s Device 'intel_backlight' of class 'backlight': Current brightness: 1000 (20%) Max brightness: 4794 % file /tmp/brightnessctl /tmp/brightnessctl: empty % After: % brightnessctl -s Error saving device data: Not a directory Could not save data for device 'intel_backlight'. Device 'intel_backlight' of class 'backlight': Current brightness: 1000 (20%) Max brightness: 4794 % --- brightnessctl.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'brightnessctl.c') diff --git a/brightnessctl.c b/brightnessctl.c index 55352d2..cbfe410 100644 --- a/brightnessctl.c +++ b/brightnessctl.c @@ -417,8 +417,10 @@ int save_device_data(struct device *dev) { if (stat(c_path, &sb)) goto fail; } - if (!S_ISDIR(sb.st_mode)) + if (!S_ISDIR(sb.st_mode)) { + errno = ENOTDIR; goto fail; + } if (!(fp = fopen(d_path, "w"))) goto fail; if (fwrite(c, 1, s, fp) < s) { @@ -472,7 +474,11 @@ static int ensure_run_dir() { if (stat(run_dir, &sb)) return 0; } - return S_ISDIR(sb.st_mode); + if (!S_ISDIR(sb.st_mode)) { + errno = ENOTDIR; + return 0; + } + return 1; } char *_cat_with(char c, ...) { -- cgit v1.2.3