summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--bin/bri.c51
1 files changed, 26 insertions, 25 deletions
diff --git a/bin/bri.c b/bin/bri.c
index 09852c5a..133673dd 100644
--- a/bin/bri.c
+++ b/bin/bri.c
@@ -18,6 +18,7 @@
 #include <err.h>
 #include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <sysexits.h>
 #include <unistd.h>
@@ -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;
 }