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

Sean Silva via llvm-dev llvm-dev at lists.llvm.org
Tue Apr 19 23:51:35 PDT 2016


We probably don't want to silently mutate the environment (which could lead
to surprises), but calling a proper system library function to get the home
directory makes sense.

-- Sean Silva

On Tue, Apr 19, 2016 at 11:18 AM, Greg Clayton via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> 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;
> }
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160419/e1ba512f/attachment.html>


More information about the llvm-dev mailing list