diff options
Diffstat (limited to 'System/Posix/Temp.hsc')
-rw-r--r-- | System/Posix/Temp.hsc | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/System/Posix/Temp.hsc b/System/Posix/Temp.hsc index 9989ca0..8ab789c 100644 --- a/System/Posix/Temp.hsc +++ b/System/Posix/Temp.hsc @@ -17,7 +17,7 @@ ----------------------------------------------------------------------------- module System.Posix.Temp ( - mkstemp, mkdtemp + mkstemp, mkstemps, mkdtemp ) where #include "HsUnix.h" @@ -69,6 +69,27 @@ mkstemp template' = do return (name, h) #endif +#if defined(__GLASGOW_HASKELL__) || defined(__HUGS__) +-- |'mkstemps' - make a unique filename with a given prefix and suffix +-- and open it for reading\/writing (only safe on GHC & Hugs). +-- The returned 'FilePath' is the (possibly relative) path of +-- the created file, which contains 6 random characters in between +-- the prefix and suffix. +mkstemps :: String -> String -> IO (FilePath, Handle) +mkstemps prefix suffix = do + let template = prefix ++ "XXXXXX" ++ suffix + lenOfsuf :: CInt + lenOfsuf = fromIntegral $ length suffix + withFilePath template $ \ ptr -> do + fd <- throwErrnoIfMinus1 "mkstemps" (c_mkstemps ptr lenOfsuf) + name <- peekFilePath ptr + h <- fdToHandle (Fd fd) + return (name, h) + +foreign import ccall unsafe "HsUnix.h __hscore_mkstemps" + c_mkstemps :: CString -> CInt -> IO CInt +#endif + -- | Make a unique directory. The returned 'FilePath' is the path of the -- created directory, which is padded with 6 random characters. The argument is -- the desired prefix of the filepath of the temporary directory to be created. |