llvm.org GIT mirror llvm / 2811bb8
Make home_directory look in the password database in addition to $HOME. This is something of an edge case, but when the $HOME environment variable is not set, we can still look in the password database to get the current user's home directory. Added a test for this by getting the value of $HOME, then unsetting it, then calling home_directory() and verifying that it succeeds and that the value is the same as what we originally read from the environment. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@298513 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 3 years ago
2 changed file(s) with 32 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
919919 namespace path {
920920
921921 bool home_directory(SmallVectorImpl &result) {
922 if (char *RequestedDir = getenv("HOME")) {
923 result.clear();
924 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
925 return true;
926 }
927 return false;
922 char *RequestedDir = getenv("HOME");
923 if (!RequestedDir) {
924 struct passwd *pw = getpwuid(getuid());
925 if (pw && pw->pw_dir)
926 RequestedDir = pw->pw_dir;
927 }
928 if (!RequestedDir)
929 return false;
930
931 result.clear();
932 result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
933 return true;
928934 }
929935
930936 static bool getDarwinConfDir(bool TempDir, SmallVectorImpl &Result) {
327327 }
328328 }
329329
330 #ifndef LLVM_ON_WIN32
331 TEST(Support, HomeDirectoryWithNoEnv) {
332 std::string Original;
333 char const *path = ::getenv("HOME");
334 // Don't try to test if we don't have something to compare against.
335 if (!path)
336 return;
337 Original = path;
338 ::unsetenv("HOME");
339
340 SmallString<128> HomeDir;
341 auto status = path::home_directory(HomeDir);
342 EXPECT_TRUE(status);
343 EXPECT_EQ(Original, HomeDir);
344
345 // Now put the original environment variable back
346 ::setenv("HOME", Original.c_str(), 1);
347 }
348 #endif
349
330350 TEST(Support, UserCacheDirectory) {
331351 SmallString<13> CacheDir;
332352 SmallString<20> CacheDir2;