summaryrefslogtreecommitdiff
path: root/lib/std/mktemp.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/mktemp.myr')
-rw-r--r--lib/std/mktemp.myr32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/std/mktemp.myr b/lib/std/mktemp.myr
index c5f06c8..bc1bf87 100644
--- a/lib/std/mktemp.myr
+++ b/lib/std/mktemp.myr
@@ -15,6 +15,8 @@ use "types"
pkg std =
const mktemp : (base : byte[:], opt : fdopt, mode : int64 -> std.result((fd, byte[:]), errno))
const mktempat : (dir : byte[:], base : byte[:], opt : fdopt, mode : int64 -> std.result((fd, byte[:]), errno))
+ const mkdtemp : (base : byte[:], mode : int64 -> std.result(byte[:], errno))
+ const mkdtempat : (dir : byte[:], base : byte[:], mode : int64 -> std.result(byte[:], errno))
const mktemppath : (base : byte[:] -> byte[:])
;;
@@ -51,6 +53,36 @@ const mktempat = {tmpdir, base, opt, mode
-> `Err Eexist
}
+const mkdtemp = {base, mode
+ var tmpdir
+
+ match std.getenv("TMPDIR")
+ | `std.Some d: tmpdir = d
+ | `std.None: tmpdir = "/tmp"
+ ;;
+
+ -> mkdtempat(tmpdir, base, mode)
+}
+
+const mkdtempat = {tmpdir, base, mode
+ var path
+
+ for var i = 0; i < Retries; i++
+ path = randpath(tmpdir, base)
+ match std.mkdir(path, mode)
+ | Enone:
+ -> `Ok path
+ | e:
+ if e != Eexist
+ std.slfree(path)
+ -> `Err e
+ ;;
+ ;;
+ std.slfree(path)
+ ;;
+ -> `Err Eexist
+}
+
const mktemppath = {base
var tmpdir, path