[PATCH] Fix llvm::sys::path::home_directory on unix to get path from passwd.pw_dir when the $HOME variable isn't set

Ilia K ki.stfu at gmail.com
Mon Feb 9 11:59:01 PST 2015


Use thread-safe version of getpwuid.


http://reviews.llvm.org/D7515

Files:
  lib/Support/Unix/Path.inc

Index: lib/Support/Unix/Path.inc
===================================================================
--- lib/Support/Unix/Path.inc
+++ lib/Support/Unix/Path.inc
@@ -44,6 +44,7 @@
 #  include <ndir.h>
 # endif
 #endif
+#include <pwd.h>
 
 #ifdef __APPLE__
 #include <mach-o/dyld.h>
@@ -546,7 +547,19 @@
 namespace path {
 
 bool home_directory(SmallVectorImpl<char> &result) {
-  if (char *RequestedDir = getenv("HOME")) {
+  char *RequestedDir = getenv("HOME");
+  if (!RequestedDir)
+  {
+    struct passwd Pwd, *PwdResult = NULL;
+    long BufSize = sysconf(_SC_GETPW_R_SIZE_MAX);
+    if (BufSize == -1)
+        BufSize = 1024;
+    char Buf[BufSize];
+    getpwuid_r(getuid(), &Pwd, Buf, BufSize, &PwdResult);
+    if (PwdResult)
+        RequestedDir = PwdResult->pw_dir;
+  }
+  if (RequestedDir) {
     result.clear();
     result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
     return true;

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7515.19603.patch
Type: text/x-patch
Size: 916 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150209/7f93355c/attachment.bin>


More information about the llvm-commits mailing list