about summary refs log tree commit diff
path: root/nixpkgs/pkgs/misc/drivers/spacenavd/configure-cfgfile-path.patch
blob: 268282e96eae666687f545438078e53c24a8ac98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
diff --git a/src/spnavd.c b/src/spnavd.c
index 2d4eca6..a5227ed 100644
--- a/src/spnavd.c
+++ b/src/spnavd.c
@@ -27,6 +27,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <sys/select.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <sys/types.h>
+#include <pwd.h>
 #include "spnavd.h"
 #include "logger.h"
 #include "dev.h"
@@ -47,13 +49,39 @@ static void handle_events(fd_set *rset);
 static void sig_handler(int s);
 static char *fix_path(char *str);
 
-static char *cfgfile = DEF_CFGFILE;
+static char* config_path;
+char* cfg_path()
+{
+	char* buf;
+	if((buf = getenv("XDG_CONFIG_HOME"))) {
+		if(config_path == NULL) {
+			config_path = malloc(strlen(buf) + strlen("/spnavrc") + 1);
+			if ( config_path != NULL) {
+				sprintf(config_path, "%s/spnavrc", buf);
+			}
+		};
+		return config_path;
+	} else {
+		if (!(buf = getenv("HOME"))) {
+			struct passwd *pw = getpwuid(getuid());
+			buf = pw->pw_dir;
+		}
+		config_path = malloc(strlen(buf) + strlen("/.config/spnavrc") + 1);
+		if ( config_path != NULL) {
+			sprintf(config_path, "%s/.config/spnavrc", buf);
+		}
+		return config_path;
+	}
+}
+
+static char *cfgfile = NULL;
 static char *logfile = DEF_LOGFILE;
 static char *pidpath = NULL;
 
 int main(int argc, char **argv)
 {
 	int i, pid, ret, become_daemon = 1;
+	cfgfile = cfg_path();
 
 	for(i=1; i<argc; i++) {
 		if(argv[i][0] == '-') {
@@ -247,7 +275,7 @@ static void print_usage(const char *argv0)
 	printf("usage: %s [options]\n", argv0);
 	printf("options:\n");
 	printf(" -d: do not daemonize\n");
-	printf(" -c <file>: config file path (default: " DEF_CFGFILE ")\n");
+	printf(" -c <file>: config file path (default: %s)\n", cfg_path());
 	printf(" -l <file>|syslog: log file path or log to syslog (default: " DEF_LOGFILE ")\n");
 	printf(" -v: verbose output\n");
 	printf(" -V,-version: print version number and exit\n");