Read $HOME first to determine the home directory (and when not present, fall back to getpwuid())

Also a minor compatibility fix in a sed invocation.

--
MOS_MIGRATED_REVID=106291639
diff --git a/src/main/cpp/blaze_util_linux.cc b/src/main/cpp/blaze_util_linux.cc
index 08bd075..867fbb2 100644
--- a/src/main/cpp/blaze_util_linux.cc
+++ b/src/main/cpp/blaze_util_linux.cc
@@ -36,15 +36,25 @@
 
 string GetOutputRoot() {
   char buf[2048];
-  struct passwd pwbuf;
-  struct passwd *pw = NULL;
-  int uid = getuid();
-  int r = getpwuid_r(uid, &pwbuf, buf, 2048, &pw);
-  if (r != -1 && pw != NULL) {
-    return blaze_util::JoinPath(pw->pw_dir, ".cache/bazel");
+  string base;
+  const char* home = getenv("HOME");
+  if (home != NULL) {
+    base = home;
   } else {
-    return "/tmp";
+    struct passwd pwbuf;
+    struct passwd *pw = NULL;
+    int uid = getuid();
+    int r = getpwuid_r(uid, &pwbuf, buf, 2048, &pw);
+    if (r != -1 && pw != NULL) {
+      base = pw->pw_dir;
+    }
   }
+
+  if (base != "") {
+    return blaze_util::JoinPath(base, ".cache/bazel");
+  }
+
+  return "/tmp";
 }
 
 void WarnFilesystemType(const string& output_base) {