From 45db1d5d237364be1e600ba33cad2b04ee4cc1ed Mon Sep 17 00:00:00 2001
From: Weland Treebark <weland@blinkenshell.org>
Date: Wed, 28 May 2014 21:34:14 +0300
Subject: [PATCH] wmbutton: fail early on missing config

wmbutton will happily start and display what otherwise looks like a
functioning display even if none of the possible configuration files
exist. However, the application promptly exits as soon as it has to show
a tooltip. This isn't nice. It looks like a crash to an unsuspecting
user. Terminal output is shown, of course, leading to a decently quick
diagnostic, but the fail isn't early enough to be useable.

The trivial fix is to check if the local configuration file (specified
as a command line argument or defaulting to ~/.wmbutton) or the global
configuration file can be open. If neither can be open, we bail out
early.

This *still* has the problem of only really being functional in a
terminal. A graphical error box would definitely be preferable and is a
possible improvement.

Signed-off-by: Weland Treebark <weland@blinkenshell.org>
---
 wmbutton/wmb_libs.c | 17 +++++++++++++++++
 wmbutton/wmbutton.c |  8 ++++++++
 wmbutton/wmbutton.h |  1 +
 3 files changed, 26 insertions(+)

diff --git a/wmbutton/wmb_libs.c b/wmbutton/wmb_libs.c
index 55802ff..e778743 100644
--- a/wmbutton/wmb_libs.c
+++ b/wmbutton/wmb_libs.c
@@ -192,6 +192,23 @@ void RunAppN(int app)
 /***********************************************************************/
 
 /***********************************************************************
+ * canOpenFile(const char *path)
+ *
+ * Check if the file at a given path can be opened.
+ ***********************************************************************/
+int canOpenFile(const char *path)
+{
+	FILE *fp;
+
+	if ((fp = fopen(path, "r")) == NULL)
+		return 0;
+	else {
+		fclose(fp);
+		return 1;
+	}
+}
+
+/***********************************************************************
  * Parse(int app)
  *
  * Parses the file 'configfile' for command to execute.
diff --git a/wmbutton/wmbutton.c b/wmbutton/wmbutton.c
index 7b061b0..6d4dd26 100644
--- a/wmbutton/wmbutton.c
+++ b/wmbutton/wmbutton.c
@@ -111,6 +111,14 @@ int main(int argc, char **argv)
 	/* Parse Command Line Arguments */
 	parseargs(argc, argv);
 
+	/* Catch fire if no configuration file exists */
+	if (!canOpenFile(Config.configfile)) {
+		if(!canOpenFile(CONFIGGLOBAL)) {
+			err_mess(FAILCONF, Config.configfile);
+			return (1);
+		}
+	}
+
 	/* Open Display */
 	if ((display = XOpenDisplay(Config.Display_str)) == NULL)
 		err_mess(FAILDISP, Config.Display_str);
diff --git a/wmbutton/wmbutton.h b/wmbutton/wmbutton.h
index b653e7d..4cd3a68 100644
--- a/wmbutton/wmbutton.h
+++ b/wmbutton/wmbutton.h
@@ -84,6 +84,7 @@ void parseargs(int argc, char **argv);
 char *readln(FILE *fp);            /* read line from file, return pointer to it */
 void err_mess(int err, char *str); /* Error Handling Routine */
 void show_usage(void);             /* show usage message to stderr */
+int canOpenFile(const char *path);
 int flush_expose(Window w);
 
 
-- 
1.8.4

