diff options
Diffstat (limited to 'System/Posix/Temp/ByteString.hsc')
-rw-r--r-- | System/Posix/Temp/ByteString.hsc | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/System/Posix/Temp/ByteString.hsc b/System/Posix/Temp/ByteString.hsc index a13e45a..02aca28 100644 --- a/System/Posix/Temp/ByteString.hsc +++ b/System/Posix/Temp/ByteString.hsc @@ -17,7 +17,7 @@ ----------------------------------------------------------------------------- module System.Posix.Temp.ByteString ( - mkstemp, mkdtemp + mkstemp, mkstemps, mkdtemp ) where #include "HsUnix.h" @@ -60,6 +60,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 'RawFilePath' is the (possibly relative) path of +-- the created file, which contains 6 random characters in between +-- the prefix and suffix. +mkstemps :: ByteString -> ByteString -> IO (RawFilePath, Handle) +mkstemps prefix suffix = do + let template = prefix `B.append` (BC.pack "XXXXXX") `B.append` suffix + lenOfsuf :: CInt + lenOfsuf = fromIntegral $ B.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 'RawFilePath' 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. |