Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use XDG_CACHE_HOME for xsel.log #15

Merged
merged 5 commits into from
Aug 12, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 13 additions & 34 deletions xsel.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,43 +335,22 @@ _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_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;
if ((cachedir = getenv ("XDG_CACHE_HOME")) == NULL) {
cachedir = strcat(getenv ("HOME"), "/.cache");
}

/* else ... go hunting for it */
uid = getuid ();
mkdir(cachedir, S_IRWXU|S_IRGRP|S_IXGRP);

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");
}

homedir = _xs_strdup (pw->pw_dir);

return homedir;
return cachedir;
}

/*
Expand Down Expand Up @@ -459,7 +438,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
Expand All @@ -468,14 +447,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 */
Expand Down Expand Up @@ -503,8 +482,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 /");
}
Expand Down