summary refs log tree commit diff stats
path: root/lib/posix
diff options
context:
space:
mode:
authorMax Grender-Jones <maxgrenderjones@users.noreply.github.com>2020-05-25 11:18:35 +0100
committerGitHub <noreply@github.com>2020-05-25 12:18:35 +0200
commitcbfe9325c5c1851630ce10cf780d1af27c57d19a (patch)
treebeb6acc332404c7bb173c325c57f2748bc040fd4 /lib/posix
parent79e85cb0b55437d8a5990f66c760fb98998de372 (diff)
downloadNim-cbfe9325c5c1851630ce10cf780d1af27c57d19a.tar.gz
Add support for mktemps (#14347)
Diffstat (limited to 'lib/posix')
-rw-r--r--lib/posix/posix.nim13
-rw-r--r--lib/posix/posix_utils.nim19
2 files changed, 24 insertions, 8 deletions
diff --git a/lib/posix/posix.nim b/lib/posix/posix.nim
index 96203b736..d3d9728ab 100644
--- a/lib/posix/posix.nim
+++ b/lib/posix/posix.nim
@@ -1036,12 +1036,21 @@ proc mkstemp*(tmpl: cstring): cint {.importc, header: "<stdlib.h>", sideEffect.}
   ## Creates a unique temporary file.
   ##
   ## **Warning**: The `tmpl` argument is written to by `mkstemp` and thus
-  ## can't be a string literal. If in doubt copy the string before passing it.
+  ## can't be a string literal. If in doubt make a copy of the cstring before
+  ## passing it in.
+
+proc mkstemps*(tmpl: cstring, suffixlen: int): cint {.importc, header: "<stdlib.h>", sideEffect.}
+  ## Creates a unique temporary file.
+  ##
+  ## **Warning**: The `tmpl` argument is written to by `mkstemps` and thus
+  ## can't be a string literal. If in doubt make a copy of the cstring before
+  ## passing it in.
 
 proc mkdtemp*(tmpl: cstring): pointer {.importc, header: "<stdlib.h>", sideEffect.}
 
-when defined(linux) or defined(bsd):
+when defined(linux) or defined(bsd) or defined(osx):
   proc mkostemp*(tmpl: cstring, oflags: cint): cint {.importc, header: "<stdlib.h>", sideEffect.}
+  proc mkostemps*(tmpl: cstring, suffixlen: cint, oflags: cint): cint {.importc, header: "<stdlib.h>", sideEffect.}
 
   proc posix_memalign*(memptr: pointer, alignment: csize_t, size: csize_t): cint {.importc, header: "<stdlib.h>".}
 
diff --git a/lib/posix/posix_utils.nim b/lib/posix/posix_utils.nim
index 2d0288187..d083e20b9 100644
--- a/lib/posix/posix_utils.nim
+++ b/lib/posix/posix_utils.nim
@@ -78,16 +78,23 @@ proc sendSignal*(pid: Pid, signal: int) =
   if kill(pid, signal.cint) != 0:
     raise newException(OSError, $strerror(errno))
 
-proc mkstemp*(prefix: string): (string, File) =
-  ## Creates a unique temporary file from a prefix string. Adds a six chars suffix.
+proc mkstemp*(prefix: string, suffix=""): (string, File) =
+  ## Creates a unique temporary file from a prefix string. A six-character string
+  ## will be added. If suffix is provided it will be added to the string
   ## The file is created with perms 0600.
   ## Returns the filename and a file opened in r/w mode.
-  var tmpl = cstring(prefix & "XXXXXX")
+  var tmpl = cstring(prefix & "XXXXXX" & suffix)
   let fd =
-    when declared(mkostemp):
-      mkostemp(tmpl, O_CLOEXEC)
+    if len(suffix)==0:
+      when declared(mkostemp):
+        mkostemp(tmpl, O_CLOEXEC)
+      else:
+        mkstemp(tmpl)
     else:
-      mkstemp(tmpl)
+      when declared(mkostemps):
+        mkostemps(tmpl, cint(len(suffix)), O_CLOEXEC)
+      else:
+        mkstemps(tmpl, cint(len(suffix)))
   var f: File
   if open(f, fd, fmReadWrite):
     return ($tmpl, f)