From 782d023a32a047c3d19ec6a21318587c6446de48 Mon Sep 17 00:00:00 2001
From: Aaron Ball <nullspoon@oper.io>
Date: Sun, 24 Dec 2017 13:57:59 -0700
Subject: Added errno support for better error messages

We now detect permission denied errors when trying to write to the
brightness file. Any other problems still return a generic error
message.
---
 src/main.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/main.c b/src/main.c
index edf906a..be7bd29 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,6 +18,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include <errno.h>
 
 
 struct props {
@@ -78,8 +79,14 @@ int fadeup(struct props* props) {
     nanosleep(&tim, &tim2);
 
     FILE* fd = fopen(props->filebrightness, "w");
-    if(! fd)
+    if(! fd) {
+      if(errno == EACCES) {
+        fprintf(stderr, "Permission denied: %s\n", props->filebrightness);
+      } else {
+        fprintf(stderr, "Could not open file %s\n", props->filebrightness);
+      }
       return -1;
+    }
 
     // Write the new brightness to the brightness file
     fprintf(fd, "%d\n", props->cur);
@@ -117,8 +124,14 @@ int fadedown(struct props* props) {
     nanosleep(&tim, &tim2);
 
     FILE* fd = fopen(props->filebrightness, "w");
-    if(! fd)
+    if(! fd) {
+      if(errno == EACCES) {
+        fprintf(stderr, "Permission denied: %s\n", props->filebrightness);
+      } else {
+        fprintf(stderr, "Could not open file %s\n", props->filebrightness);
+      }
       return -1;
+    }
 
     // Write the new brightness to the brightness file
     fprintf(fd, "%d\n", props->cur);
-- 
cgit v1.2.3