[llvm-dev] llvm::sys::path::home_directory() implementation for unix

Greg Clayton via llvm-dev llvm-dev at lists.llvm.org
Tue Apr 19 11:18:20 PDT 2016


I recently was using llvm code from a process that I manually spawned as a child process and noticed that llvm::sys::path::home_directory() only works if "HOME" is set in the process environment:

bool home_directory(SmallVectorImpl<char> &result) {
  if (char *RequestedDir = getenv("HOME")) {
    result.clear();
    result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
    return true;
  }

  return false;
}

This is in lib/Support/Unix/Path.inc. Do we want to try a little bit harder? Maybe something like:

bool home_directory(SmallVectorImpl<char> &result) {
  if (char *RequestedDir = getenv("HOME")) {
    result.clear();
    result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
    return true;
  } else {
    struct passwd *pw = getpwuid(getuid());
    if (pw && pw->pw_dir && pw->pw_dir[0])
    {
      setenv("HOME", pw->pw_dir, 0);
      result.assign(llvm::StringRef(pw->pw_dir));
      return true;
    }
  }
  return false;
}


More information about the llvm-dev mailing list