[libcxx] Implement temp_directory_path using GetTempPath on windows

This does roughly the same as the manual implementation, but checks
a slightly different set of environment variables and has a more
appropriate fallback if no environment variables are available
(/tmp isn't a very useful fallback on windows).

Differential Revision: https://reviews.llvm.org/D91175

GitOrigin-RevId: d4f4e723d0b4f09d72880f1679c02d586bf8abfa
diff --git a/src/filesystem/operations.cpp b/src/filesystem/operations.cpp
index dccec93..674f191 100644
--- a/src/filesystem/operations.cpp
+++ b/src/filesystem/operations.cpp
@@ -1347,6 +1347,19 @@
 path __temp_directory_path(error_code* ec) {
   ErrorHandler<path> err("temp_directory_path", ec);
 
+#if defined(_LIBCPP_WIN32API)
+  wchar_t buf[MAX_PATH];
+  DWORD retval = GetTempPathW(MAX_PATH, buf);
+  if (!retval)
+    return err.report(detail::make_windows_error(GetLastError()));
+  if (retval > MAX_PATH)
+    return err.report(errc::filename_too_long);
+  // GetTempPathW returns a path with a trailing slash, which we
+  // shouldn't include for consistency.
+  if (buf[retval-1] == L'\\')
+    buf[retval-1] = L'\0';
+  path p(buf);
+#else
   const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
   const char* ret = nullptr;
 
@@ -1357,6 +1370,7 @@
     ret = "/tmp";
 
   path p(ret);
+#endif
   error_code m_ec;
   file_status st = detail::posix_stat(p, &m_ec);
   if (!status_known(st))