mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 19:45:12 +00:00
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.
This commit is contained in:
parent
8a703c0076
commit
7f25959fa2
Notes:
sideshowbarker
2024-07-19 12:55:50 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/7f25959fa20
4 changed files with 15 additions and 10 deletions
|
@ -24,7 +24,9 @@ void handle_sigchld(int)
|
|||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
chdir(get_current_user_home_path());
|
||||
if (chdir(get_current_user_home_path().characters()) < 0)
|
||||
perror("chdir");
|
||||
|
||||
GApplication app(argc, argv);
|
||||
|
||||
signal(SIGCHLD, handle_sigchld);
|
||||
|
|
|
@ -133,7 +133,8 @@ int main(int argc, char** argv)
|
|||
{
|
||||
GApplication app(argc, argv);
|
||||
|
||||
chdir(get_current_user_home_path());
|
||||
if (chdir(get_current_user_home_path().characters()) < 0)
|
||||
perror("chdir");
|
||||
|
||||
int ptm_fd = open("/dev/ptmx", O_RDWR);
|
||||
if (ptm_fd < 0) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -1 +1,5 @@
|
|||
const char* get_current_user_home_path();
|
||||
#pragma once
|
||||
|
||||
#include <AK/AKString.h>
|
||||
|
||||
String get_current_user_home_path();
|
||||
|
|
Loading…
Add table
Reference in a new issue