Skip to content
Snippets Groups Projects
Commit 15226db1 authored by deian's avatar deian Committed by dterei
Browse files

mkstemps

parent 28a7a416
No related branches found
No related tags found
No related merge requests found
......@@ -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.
......
......@@ -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.
......
......@@ -135,6 +135,9 @@ int __hscore_mkstemp(char *filetemplate) {
char *__hscore_mkdtemp(char *filetemplate) {
return (mkdtemp(filetemplate));
}
int __hscore_mkstemps(char *filetemplate, int suffixlen) {
return (mkstemps(filetemplate, suffixlen));
}
#endif
#if !defined(__MINGW32__) && !defined(irix_HOST_OS)
......
......@@ -175,6 +175,7 @@ int __hsunix_push_module(int fd, const char *module);
#if !defined(__MINGW32__)
int __hscore_mkstemp(char *filetemplate);
int __hscore_mkstemps(char *filetemplate, int suffixlen);
char *__hscore_mkdtemp(char *filetemplate);
#endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment