Retrieve user's home from passwd entry as fallback

For weird reasons, the HOME-variable might not always be set. So we
should fallback on something more reliable.

Fixes #693
This commit is contained in:
Benedikt Heine 2020-03-10 16:11:34 +01:00
parent dfd6e76de5
commit 9591af02a8
2 changed files with 27 additions and 1 deletions

View File

@ -5,9 +5,11 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <glib.h> #include <glib.h>
#include <pwd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include "log.h" #include "log.h"
@ -138,7 +140,7 @@ char *string_to_path(char *string)
{ {
if (string && STRN_EQ(string, "~/", 2)) { if (string && STRN_EQ(string, "~/", 2)) {
char *home = g_strconcat(getenv("HOME"), "/", NULL); char *home = g_strconcat(user_get_home(), "/", NULL);
string = string_replace_at(string, 0, 2, home); string = string_replace_at(string, 0, 2, home);
@ -204,4 +206,21 @@ gint64 time_monotonic_now(void)
#endif #endif
return S2US(tv_now.tv_sec) + tv_now.tv_nsec / 1000; return S2US(tv_now.tv_sec) + tv_now.tv_nsec / 1000;
} }
/* see utils.h */
const char *user_get_home(void)
{
static const char *home_directory = NULL;
ASSERT_OR_RET(!home_directory, home_directory);
// Check the HOME variable for the user's home
home_directory = getenv("HOME");
ASSERT_OR_RET(!home_directory, home_directory);
// Check the /etc/passwd entry for the user's home
home_directory = getpwuid(getuid())->pw_dir;
return home_directory;
}
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */

View File

@ -110,5 +110,12 @@ gint64 string_to_time(const char *string);
*/ */
gint64 time_monotonic_now(void); gint64 time_monotonic_now(void);
/**
* Retrieve the HOME directory of the user running dunst
*
* @returns: A string of the current home directory
*/
const char *user_get_home(void);
#endif #endif
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */