From b7069282021aec7b486a1123c8521ac4e67250c0 Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Fri, 17 Mar 2023 18:01:22 -0300 Subject: librc: Use getpwuid instead of ${HOME} to get user's home directory. Using ${HOME} works when the user runs any librc program from the shell, but trying to invoke them from pam or similar, the variable is not set. Signed-off-by: Anna (navi) Figueiredo Gomes --- src/librc/librc.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/librc/librc.c b/src/librc/librc.c index 9063b934..b05483d9 100644 --- a/src/librc/librc.c +++ b/src/librc/librc.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -412,12 +413,17 @@ char * rc_user_sysconfdir(void) { char *env, *path = NULL; + struct passwd *user_passwd = NULL; + if ((env = getenv("XDG_CONFIG_HOME"))) { xasprintf(&path, "%s", env); - } else if ((env = getenv("HOME"))) { - xasprintf(&path, "%s/.config/", env); } else { - eerrorx("Both XDG_CONFIG_HOME and HOME unset."); + errno = 0; + user_passwd = getpwuid(getuid()); + if (!user_passwd) { + eerrorx("getpwuid: Failed to get user's passwd: %s", strerror(errno)); + } + xasprintf(&path, "%s/.config/", user_passwd->pw_dir); } return path; } @@ -426,12 +432,10 @@ char * rc_user_svcdir(void) { char *env, *path = NULL; - uid_t id; if ((env = getenv("XDG_RUNTIME_DIR"))) { xasprintf(&path, "%s%s", env, RC_USER_RUNTIME_FOLDER); } else { - id = getuid(); - xasprintf(&path, "/tmp%s/%d/", RC_USER_RUNTIME_FOLDER, id); + xasprintf(&path, "/tmp%s/%d/", RC_USER_RUNTIME_FOLDER, getuid()); } return path; } -- cgit v1.2.3