From b74ec43e3c2ae9a1a36ede0874610c3b83c77ade Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Thu, 22 Feb 2018 23:41:15 -0500 Subject: Rewrite bri --- bin/bri.c | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/bin/bri.c b/bin/bri.c index edf1aeee..1cb62afd 100644 --- a/bin/bri.c +++ b/bin/bri.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -27,56 +28,56 @@ static const char *CLASS = "/sys/class/backlight"; int main(int argc, char *argv[]) { int error; - if (argc < 2) errx(EX_USAGE, "usage: bri N | +... | -..."); + const char *input = (argc > 1) ? argv[1] : NULL; error = chdir(CLASS); - if (error) err(EX_IOERR, "%s", CLASS); + if (error) err(EX_OSFILE, "%s", CLASS); DIR *dir = opendir("."); - if (!dir) err(EX_IOERR, "%s", CLASS); + if (!dir) err(EX_NOPERM, "%s", CLASS); struct dirent *entry; while (NULL != (errno = 0, entry = readdir(dir))) { if (entry->d_name[0] == '.') continue; error = chdir(entry->d_name); - if (error) err(EX_IOERR, "%s", entry->d_name); + if (error) err(EX_NOPERM, "%s/%s", CLASS, entry->d_name); break; } if (!entry) { if (errno) err(EX_IOERR, "%s", CLASS); - errx(EX_CONFIG, "empty %s", CLASS); + errx(EX_CONFIG, "%s: empty", CLASS); } - char *value = argv[1]; + FILE *actual = fopen("actual_brightness", "r"); + if (!actual) err(EX_NOPERM, "%s/actual_brightness", CLASS); - if (value[0] == '+' || value[0] == '-') { - FILE *actual = fopen("actual_brightness", "r"); - if (!actual) err(EX_IOERR, "actual_brightness"); + unsigned value; + int match = fscanf(actual, "%u", &value); + if (match == EOF) err(EX_IOERR, "%s/actual_brightness", CLASS); + if (match < 1) errx(EX_DATAERR, "%s/actual_brightness", CLASS); - unsigned int brightness; - int match = fscanf(actual, "%u", &brightness); - if (match == EOF) err(EX_IOERR, "actual_brightness"); - if (match < 1) err(EX_DATAERR, "actual_brightness"); + if (!input) { + printf("%u\n", value); + return EX_OK; + } - size_t count = strnlen(value, 15); - if (value[0] == '+') { - brightness += 16 * count; + if (input[0] == '+' || input[0] == '-') { + size_t count = strnlen(input, 16); + if (input[0] == '+') { + value += 16 * count; } else { - brightness -= 16 * count; + value -= 16 * count; } - - char buf[15]; - snprintf(buf, sizeof(buf), "%u", brightness); - - value = buf; + } else { + value = strtoul(input, NULL, 0); } FILE *brightness = fopen("brightness", "w"); - if (!brightness) err(EX_IOERR, "brightness"); + if (!brightness) err(EX_NOPERM, "%s/brightness", CLASS); - int count = fprintf(brightness, "%s", value); - if (count < 0) err(EX_IOERR, "brightness"); + int size = fprintf(brightness, "%u", value); + if (size < 0) err(EX_IOERR, "brightness"); return EX_OK; } -- cgit 1.4.1