aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnna (navi) Figueiredo Gomes <navi@vlhl.dev>2023-03-17 18:01:22 -0300
committerAnna (navi) Figueiredo Gomes <navi@vlhl.dev>2023-10-19 10:56:54 +0200
commitb7069282021aec7b486a1123c8521ac4e67250c0 (patch)
tree3c5a7d7e14ea5377183942342250385606a6cfce /src
parent9efa44579fd1f60a4c9ace264bb0b968ccb0f7ea (diff)
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 <navi@vlhl.dev>
Diffstat (limited to 'src')
-rw-r--r--src/librc/librc.c16
1 files 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 <fcntl.h>
#include <libgen.h>
#include <limits.h>
+#include <pwd.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
@@ -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;
}