getenv(3) can return NULL if the variable isn't set. Some Standard
Library implementations (like Clang's libc++) do not like a NULL as
the argument to std::string's constructor, which is why the code is so
convoluted as it is. Instead, simplify the code by having a getenv
wrapper that has a default value.

And handle the case of the empty $HOME: currently, if it isn't set,
the application would crash. Instead, use getpwuid as a fallback.