Index: /trunk/src/VBox/Runtime/r3/path.cpp
===================================================================
--- /trunk/src/VBox/Runtime/r3/path.cpp	(revision 20100)
+++ /trunk/src/VBox/Runtime/r3/path.cpp	(revision 20101)
@@ -964,8 +964,10 @@
 }
 
+
 /**
  * Gets the temporary directory path.
  *
  * @returns iprt status code.
+ *
  * @param   pszPath     Buffer where to store the path.
  * @param   cchPath     Buffer size in bytes.
@@ -973,22 +975,36 @@
 RTDECL(int) RTPathTemp(char *pszPath, size_t cchPath)
 {
-    int rc;
-    const char *pszTmpEnv = RTEnvGet("TMP");
-    if (!pszTmpEnv)
-        pszTmpEnv = RTEnvGet("TEMP");
-    char *pszTmpDir;
-    /* Make a copy in any case. */
-    if (pszTmpEnv)
-        pszTmpDir = RTStrDup(pszTmpEnv);
-    else
-        pszTmpDir = RTStrDup("/tmp");
-
-    size_t cchTmpDir = strlen(pszTmpDir);
-    if (cchTmpDir < cchPath)
-        memcpy(pszPath, pszTmpDir, cchTmpDir + 1);
-    else
-        rc = VERR_BUFFER_OVERFLOW;
-    RTStrFree(pszTmpDir);
-    return rc;
+    /*
+     * Try get it from the environment first.
+     */
+    static const char * const s_apszVars[] =
+    {
+        "IPRT_TMPDIR"
+#if defined(RT_OS_WINDOWS)
+        , "TMP", "TEMP", "USERPROFILE"
+#elif defined(RT_OS_OS2)
+        , "TMP", "TEMP", "TMPDIR"
+#else
+        , "TMPDIR"
+#endif
+    };
+    for (size_t iVar = 0; iVar < RT_ELEMENTS(s_apszVars); iVar++)
+    {
+        int rc = RTEnvGetEx(RTENV_DEFAULT, s_apszVars[iVar], pszPath, cchPath, NULL);
+        if (rc != VERR_ENV_VAR_NOT_FOUND)
+            return rc;
+    }
+
+    /*
+     * Here we should use some sane system default, instead we just use
+     * the typical unix temp dir for now.
+     */
+    /** @todo Windows should default to the windows directory, see GetTempPath.
+     * Some unixes has path.h and _PATH_TMP. There is also a question about
+     * whether /var/tmp wouldn't be a better place...  */
+    if (cchPath < sizeof("/tmp") )
+        return VERR_BUFFER_OVERFLOW;
+    memcpy(pszPath, "/tmp", sizeof("/tmp"));
+    return VINF_SUCCESS;
 }
 
Index: /trunk/src/VBox/Runtime/testcase/tstPath.cpp
===================================================================
--- /trunk/src/VBox/Runtime/testcase/tstPath.cpp	(revision 20100)
+++ /trunk/src/VBox/Runtime/testcase/tstPath.cpp	(revision 20101)
@@ -78,4 +78,14 @@
     if (RT_SUCCESS(rc))
         RTTestIPrintf(RTTESTLVL_INFO, "UserHome={%s}\n", szPath);
+
+    RTTestSub(hTest, "RTPathTemp");
+    RTTESTI_CHECK_RC(RTPathTemp(szPath, sizeof(szPath)), VINF_SUCCESS);
+    if (RT_SUCCESS(rc))
+        RTTestIPrintf(RTTESTLVL_INFO, "PathTemp={%s}\n", szPath);
+    size_t cch = strlen(szPath);
+    RTTESTI_CHECK_RC(RTPathTemp(szPath, cch), VERR_BUFFER_OVERFLOW);
+    RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+1), VINF_SUCCESS);
+    RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+2), VINF_SUCCESS);
+
 
     /*
