From 7c07370d643696de3b354f11fe2e56131c1f2cb5 Mon Sep 17 00:00:00 2001 From: valeth Date: Sat, 21 Nov 2015 12:20:04 +0100 Subject: [PATCH 1/5] change get_homedir to get_xdg_cache_home don't write logfiles into the users home directory thats just rude :) --- xsel.c | 45 +++++++++++---------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/xsel.c b/xsel.c index 2eaa76a..b3e6506 100644 --- a/xsel.c +++ b/xsel.c @@ -340,38 +340,15 @@ _xs_strncpy (char * dest, const char * src, size_t n) * Get the user's home directory. */ static char * -get_homedir (void) +get_xdg_cache_home (void) { - uid_t uid; - char * username, * homedir; - struct passwd * pw; + char * cachedir; - if ((homedir = getenv ("HOME")) != NULL) { - return homedir; - } - - /* else ... go hunting for it */ - uid = getuid (); - - username = getenv ("LOGNAME"); - if (!username) username = getenv ("USER"); - - if (username) { - pw = getpwnam (username); - if (pw && pw->pw_uid == uid) goto gotpw; - } - - pw = getpwuid (uid); - -gotpw: - - if (!pw) { - exit_err ("error retrieving passwd entry"); + if ((cachedir = getenv ("XDG_CACHE_HOME")) != NULL) { + return cachedir; + } else { + return strcat(getenv ("HOME"), "/.cache") } - - homedir = _xs_strdup (pw->pw_dir); - - return homedir; } /* @@ -459,7 +436,7 @@ become_daemon (void) { pid_t pid; int null_r_fd, null_w_fd, log_fd; - char * homedir; + char * cachedir; if (no_daemon) { /* If the user has specified a timeout, enforce it even if we don't @@ -468,14 +445,14 @@ become_daemon (void) return; } - homedir = get_homedir (); + cachedir = get_xdg_cache_home(); /* Check that we can open a logfile before continuing */ /* If the user has specified a --logfile, use that ... */ if (logfile[0] == '\0') { /* ... otherwise use the default logfile */ - snprintf (logfile, MAXFNAME, "%s/.xsel.log", homedir); + snprintf (logfile, MAXFNAME, "%s/.xsel.log", cachedir); } /* Make sure to create the logfile with sane permissions */ @@ -503,8 +480,8 @@ become_daemon (void) umask (0); - if (chdir (homedir) == -1) { - print_debug (D_WARN, "Could not chdir to %s\n", homedir); + if (chdir (cachedir) == -1) { + print_debug (D_WARN, "Could not chdir to %s\n", cachedir); if (chdir ("/") == -1) { exit_err ("Error chdir to /"); } From a7f85c0f805957fef7f947223529a8641599d5be Mon Sep 17 00:00:00 2001 From: valeth Date: Sat, 21 Nov 2015 12:25:06 +0100 Subject: [PATCH 2/5] add missing semicolon --- xsel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xsel.c b/xsel.c index b3e6506..a355a3b 100644 --- a/xsel.c +++ b/xsel.c @@ -347,7 +347,7 @@ get_xdg_cache_home (void) if ((cachedir = getenv ("XDG_CACHE_HOME")) != NULL) { return cachedir; } else { - return strcat(getenv ("HOME"), "/.cache") + return strcat(getenv ("HOME"), "/.cache"); } } From 31e0a14bfcf3d23890241c9f09de1b0694a8d38c Mon Sep 17 00:00:00 2001 From: valeth Date: Sat, 21 Nov 2015 12:35:11 +0100 Subject: [PATCH 3/5] update comments for get_xdg_cache_home function --- xsel.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xsel.c b/xsel.c index a355a3b..846504f 100644 --- a/xsel.c +++ b/xsel.c @@ -335,9 +335,9 @@ _xs_strncpy (char * dest, const char * src, size_t n) } /* - * get_homedir () + * get_xdg_cache_home () * - * Get the user's home directory. + * Get the user's cache directory */ static char * get_xdg_cache_home (void) From a20077fef1e665dc342448bceee1d759c13c10bd Mon Sep 17 00:00:00 2001 From: valeth Date: Sat, 21 Nov 2015 12:35:48 +0100 Subject: [PATCH 4/5] do not use a dotfile for the log --- xsel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xsel.c b/xsel.c index 846504f..3bba52f 100644 --- a/xsel.c +++ b/xsel.c @@ -452,7 +452,7 @@ become_daemon (void) /* If the user has specified a --logfile, use that ... */ if (logfile[0] == '\0') { /* ... otherwise use the default logfile */ - snprintf (logfile, MAXFNAME, "%s/.xsel.log", cachedir); + snprintf (logfile, MAXFNAME, "%s/xsel.log", cachedir); } /* Make sure to create the logfile with sane permissions */ From 8856ad9694fdef09014fb9e0373be155284907e3 Mon Sep 17 00:00:00 2001 From: valeth Date: Sat, 21 Nov 2015 12:48:23 +0100 Subject: [PATCH 5/5] create the cache directory if it does not exist --- xsel.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/xsel.c b/xsel.c index 3bba52f..9e3afcf 100644 --- a/xsel.c +++ b/xsel.c @@ -344,11 +344,13 @@ get_xdg_cache_home (void) { char * cachedir; - if ((cachedir = getenv ("XDG_CACHE_HOME")) != NULL) { - return cachedir; - } else { - return strcat(getenv ("HOME"), "/.cache"); + if ((cachedir = getenv ("XDG_CACHE_HOME")) == NULL) { + cachedir = strcat(getenv ("HOME"), "/.cache"); } + + mkdir(cachedir, S_IRWXU|S_IRGRP|S_IXGRP); + + return cachedir; } /*