diff --git a/src/utils.c b/src/utils.c index 1b4713c..6a1d2fb 100644 --- a/src/utils.c +++ b/src/utils.c @@ -5,9 +5,11 @@ #include #include #include +#include #include #include #include +#include #include "log.h" @@ -138,7 +140,7 @@ char *string_to_path(char *string) { 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); @@ -204,4 +206,21 @@ gint64 time_monotonic_now(void) #endif 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: */ diff --git a/src/utils.h b/src/utils.h index 65f4cd8..2278f6d 100644 --- a/src/utils.h +++ b/src/utils.h @@ -110,5 +110,12 @@ gint64 string_to_time(const char *string); */ 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 /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */