Skip to content

Commit

Permalink
Improve home retrieving home directory. Fixes #2149.
Browse files Browse the repository at this point in the history
  • Loading branch information
antonsviridenko committed Dec 10, 2023
1 parent e9021c4 commit f57b5e3
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/common/file-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "str-utils.h"
#include <algorithm>
#ifdef _WIN32
#include <Shlobj.h>
#include <random> // for rnp_mkstemp
#define CATCH_AND_RETURN(v) \
catch (...) \
Expand All @@ -51,6 +52,7 @@
}
#else
#include <string.h>
#include <pwd.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
Expand Down Expand Up @@ -329,10 +331,33 @@ empty(const std::string &path)
std::string
HOME(const std::string &sdir)
{
const char *home = getenv("HOME");
const char *home;
#ifdef _WIN32
wchar_t wcsidlprf[MAX_PATH];
wchar_t *wuserprf;

wuserprf = _wgetenv(L"USERPROFILE");

if (wuserprf != NULL) {
home = wstr_to_utf8(wuserprf).c_str();
} else if (SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, wcsidlprf) ==
S_OK) {
home = wstr_to_utf8(wcsidlprf).c_str();
} else {
home = "";
}
#else
home = getenv("HOME");
if (!home) {
return "";
struct passwd *pwd;
pwd = getpwuid(getuid());
if (pwd != NULL) {
home = pwd->pw_dir;
} else {
home = "";
}
}
#endif
return sdir.empty() ? home : append(home, sdir);
}

Expand Down

0 comments on commit f57b5e3

Please sign in to comment.