summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Wolff <janw@mailbox.org>2019-06-19 17:51:59 +0200
committerJan Wolff <janw@mailbox.org>2019-06-19 17:51:59 +0200
commit85a60d03bbd4d09c54d4160e3fb2adbf1e6f505a (patch)
tree0f91ae66106972ffb6be517e0bae3234314b82be
parent8d9a38e0a90abaf2a1ef22db26cb5e4a79893948 (diff)
error handling and version tracking
-rw-r--r--Makefile4
-rw-r--r--dwmstatus.c31
2 files changed, 29 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index 87db407..092b5a6 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,7 @@
+VERSION = 0.1.1
+
CC = gcc
-CFLAGS = -std=c11 -Wall -Wextra
+CFLAGS = -std=c11 -Wall -Wextra -DVERSION=\"${VERSION}\"
X11INC = /usr/X11R6/include
X11LIB = /usr/X11R6/lib
LIBS = -L${X11LIB} -lX11
diff --git a/dwmstatus.c b/dwmstatus.c
index 1d16bb8..2bdaa62 100644
--- a/dwmstatus.c
+++ b/dwmstatus.c
@@ -25,6 +25,11 @@ void print() {
#endif
}
+void error(const char * err) {
+ fputs(err, stderr);
+ exit(EXIT_FAILURE);
+}
+
void update_tag(char * dst) {
strncpy(dst, TAG, BLOCK_SIZE);
}
@@ -34,15 +39,18 @@ void update_battery(char * dst) {
int cur, max;
FILE * fp;
- fp = fopen(BATTERY_NOW, "r");
+ if((fp = fopen(BATTERY_NOW, "r")) == NULL)
+ error("failed to read current battery state");
fread(buf_cur, 32, 1, fp);
fclose(fp);
- fp = fopen(BATTERY_FULL, "r");
+ if((fp = fopen(BATTERY_FULL, "r")) == NULL)
+ error("failed to read max battery state");
fread(buf_max, 32, 1, fp);
fclose(fp);
- fp = fopen(BATTERY_STATUS, "r");
+ if((fp = fopen(BATTERY_STATUS, "r")) == NULL)
+ error("failed to read battery status");
fread(buf_status, 1, 1, fp);
fclose(fp);
@@ -84,8 +92,21 @@ void update() {
}
}
-int main() {
- dpy = XOpenDisplay(NULL);
+void print_version() {
+ printf("dwmstatus %s\n", VERSION);
+ puts(" simple status bar helper for dwm");
+ puts(" (c) 2019 Jan Wolff");
+ exit(EXIT_SUCCESS);
+}
+
+int main(int argc, char * argv[]) {
+ for(int i = 0; i < argc; i++) {
+ if(strcmp(argv[i], "-v") == 0) print_version();
+ else if(strcmp(argv[i], "--version") == 0) print_version();
+ }
+
+ if((dpy = XOpenDisplay(NULL)) == NULL)
+ error("failed to open X display");
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);