diff --git a/std/file.d b/std/file.d index 14fd8e4f7..5f53e4ee1 100644 --- a/std/file.d +++ b/std/file.d @@ -5275,9 +5275,20 @@ slurp(Types...)(string filename, scope const(char)[] format) assert(slurp!(int)(deleteme, "%d") == [10, 20]); } - /** Returns the path to a directory for temporary files. +On POSIX platforms, it searches through the following list of directories +and returns the first one which is found to exist: +$(OL + $(LI The directory given by the `TMPDIR` environment variable.) + $(LI The directory given by the `TEMP` environment variable.) + $(LI The directory given by the `TMP` environment variable.) + $(LI `/tmp/`) + $(LI `/var/tmp/`) + $(LI `/usr/tmp/`) +) + +On all platforms, `tempDir` returns the current working directory on failure. The return value of the function is cached, so the procedures described below will only be performed the first time the function is called. All @@ -5321,13 +5332,14 @@ string tempDir() @trusted } else version (Posix) { + import std.path : dirSeparator; import std.process : environment; // This function looks through the list of alternative directories // and returns the first one which exists and is a directory. static string findExistingDir(T...)(lazy T alternatives) { foreach (dir; alternatives) - if (!dir.empty && exists(dir)) return dir; + if (!dir.empty && exists(dir)) return dir ~ dirSeparator; return null; } @@ -5340,7 +5352,10 @@ string tempDir() @trusted } else static assert(false, "Unsupported platform"); - if (cache is null) cache = getcwd(); + if (cache is null) + { + cache = getcwd() ~ dirSeparator; + } } return cache; } @@ -5363,6 +5378,13 @@ string tempDir() @trusted assert(myFile.readText == "hello"); } +@safe unittest +{ + import std.algorithm.searching : endsWith; + import std.path : dirSeparator; + assert(tempDir.endsWith(dirSeparator)); +} + /** Returns the available disk space based on a given path. On Windows, `path` must be a directory; on POSIX systems, it can be a file or directory.