From b2c44718b25d6d22bdbaf9d2f2e6ada122710e51 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Wed, 22 May 2013 15:15:48 +0000 Subject: [PATCH] beefed up the error handling and logging around the XAUTHORITY setup, and added the possibility of using the HOME env var as a fallback git-svn-id: svn+ssh://svn.code.sf.net/p/spacenav/code/trunk/spacenavd@153 ef983eb1-d774-4af8-acfd-baaf7b16a646 --- src/proto_x11.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/proto_x11.c b/src/proto_x11.c index 87d301c..a338ff8 100644 --- a/src/proto_x11.c +++ b/src/proto_x11.c @@ -22,6 +22,7 @@ along with this program. If not, see . #include #include #include +#include #include #ifdef HAVE_ALLOCA_H #include @@ -84,8 +85,21 @@ int init_x11(void) /* ... also there won't be an XAUTHORITY env var, so set one up */ if(!getenv("XAUTHORITY")) { struct passwd *p = getpwuid(getuid()); - char *home = p->pw_dir ? p->pw_dir : "/tmp"; - char *buf = alloca(strlen("XAUTHORITY=") + strlen(home) + strlen("/.Xauthority") + 1); + char *home, *buf; + if(!p || !p->pw_dir) { + if(!p) { + fprintf(stderr, "getpwuid failed: %s\n", strerror(errno)); + } + fprintf(stderr, "falling back to getting the home directory from the HOME env var...\n"); + if(!(home = getenv("HOME"))) { + fprintf(stderr, "HOME env var not found, using /tmp as a home directory...\n"); + home = "/tmp"; + } + } else { + home = p->pw_dir; + } + + buf = alloca(strlen("XAUTHORITY=") + strlen(home) + strlen("/.Xauthority") + 1); sprintf(buf, "XAUTHORITY=%s/.Xauthority", home); putenv(buf); }