summaryrefslogtreecommitdiff
path: root/Libraries/LibCore
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-08-03 08:32:07 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-08-03 08:32:07 +0200
commit7f25959fa2083ecd2a335a9809de509180c797ca (patch)
treebb19668a2fecb959c1ba0ddecda8044829724c5a /Libraries/LibCore
parent8a703c007647ece356f7e6b02757fbb19791d4d8 (diff)
downloadserenity-7f25959fa2083ecd2a335a9809de509180c797ca.zip
LibCore: Make get_current_user_home_path() return String & close passwd
This API was returning a "const char*" and it was unclear who took care of the underlying memory. Returning a String makes that obvious. Also make sure we close the /etc/passwd file when we're done with it.
Diffstat (limited to 'Libraries/LibCore')
-rw-r--r--Libraries/LibCore/CUserInfo.cpp12
-rw-r--r--Libraries/LibCore/CUserInfo.h6
2 files changed, 10 insertions, 8 deletions
diff --git a/Libraries/LibCore/CUserInfo.cpp b/Libraries/LibCore/CUserInfo.cpp
index d5308d454e..4a232c3752 100644
--- a/Libraries/LibCore/CUserInfo.cpp
+++ b/Libraries/LibCore/CUserInfo.cpp
@@ -3,15 +3,13 @@
#include <stdlib.h>
#include <unistd.h>
-const char* get_current_user_home_path()
+String get_current_user_home_path()
{
if (auto* home_env = getenv("HOME"))
return home_env;
- auto d = "/";
- uid_t uid = getuid();
- if (auto* pwd = getpwuid(uid))
- return pwd->pw_dir;
-
- return d;
+ auto* pwd = getpwuid(getuid());
+ String path = pwd ? pwd->pw_dir : "/";
+ endpwent();
+ return path;
}
diff --git a/Libraries/LibCore/CUserInfo.h b/Libraries/LibCore/CUserInfo.h
index b47cec4af1..2c172098e6 100644
--- a/Libraries/LibCore/CUserInfo.h
+++ b/Libraries/LibCore/CUserInfo.h
@@ -1 +1,5 @@
-const char* get_current_user_home_path();
+#pragma once
+
+#include <AK/AKString.h>
+
+String get_current_user_home_path();