@@ -424,18 +424,34 @@ function touch(path::AbstractString)
424424 path
425425end
426426
427- const temp_prefix = " jl_"
428-
429- if Sys. iswindows ()
427+ """
428+ tempdir()
430429
430+ Gets the path of the temporary directory. On Windows, `tempdir()` uses the first environment
431+ variable found in the ordered list `TMP`, `TEMP`, `USERPROFILE`. On all other operating
432+ systems, `tempdir()` uses the first environment variable found in the ordered list `TMPDIR`,
433+ `TMP`, `TEMP`, and `TEMPDIR`. If none of these are found, the path `"/tmp"` is used.
434+ """
431435function tempdir ()
432- temppath = Vector {UInt16} (undef, 32767 )
433- lentemppath = ccall (:GetTempPathW , stdcall, UInt32, (UInt32, Ptr{UInt16}), length (temppath), temppath)
434- windowserror (" GetTempPath" , lentemppath >= length (temppath) || lentemppath == 0 )
435- resize! (temppath, lentemppath)
436- return transcode (String, temppath)
436+ buf = Base. StringVector (AVG_PATH - 1 ) # space for null-terminator implied by StringVector
437+ sz = RefValue {Csize_t} (length (buf) + 1 ) # total buffer size including null
438+ while true
439+ rc = ccall (:uv_os_tmpdir , Cint, (Ptr{UInt8}, Ptr{Csize_t}), buf, sz)
440+ if rc == 0
441+ resize! (buf, sz[])
442+ return String (buf)
443+ elseif rc == Base. UV_ENOBUFS
444+ resize! (buf, sz[] - 1 ) # space for null-terminator implied by StringVector
445+ else
446+ uv_error (:tmpdir , rc)
447+ end
448+ end
437449end
438450
451+ const temp_prefix = " jl_"
452+
453+ if Sys. iswindows ()
454+
439455function _win_tempname (temppath:: AbstractString , uunique:: UInt32 )
440456 tempp = cwstring (temppath)
441457 temppfx = cwstring (temp_prefix)
@@ -481,9 +497,6 @@ function tempname()
481497 return s
482498end
483499
484- # Obtain a temporary directory's path.
485- tempdir () = dirname (tempname ())
486-
487500# Create and return the name of a temporary file along with an IOStream
488501function mktemp (parent= tempdir ())
489502 b = joinpath (parent, temp_prefix * " XXXXXX" )
496509end # os-test
497510
498511
499- """
500- tempdir()
501-
502- Obtain the path of a temporary directory (possibly shared with other processes).
503- """
504- tempdir ()
505-
506512"""
507513 tempname()
508514
0 commit comments