{-# LINE 1 "libraries/unix/System/Posix/Temp.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LINE 3 "libraries/unix/System/Posix/Temp.hsc" #-}
{-# LANGUAGE Safe #-}
{-# LINE 7 "libraries/unix/System/Posix/Temp.hsc" #-}
module System.Posix.Temp (
mkstemp, mkstemps, mkdtemp
) where
import Foreign.C
import System.IO
{-# LINE 33 "libraries/unix/System/Posix/Temp.hsc" #-}
import System.Posix.IO
import System.Posix.Types
import System.Posix.Internals (withFilePath, peekFilePath)
foreign import capi unsafe "HsUnix.h mkstemp"
c_mkstemp :: CString -> IO CInt
mkstemp :: String -> IO (FilePath, Handle)
mkstemp template' = do
let template = template' ++ "XXXXXX"
withFilePath template $ \ ptr -> do
fd <- throwErrnoIfMinus1 "mkstemp" (c_mkstemp ptr)
name <- peekFilePath ptr
h <- fdToHandle (Fd fd)
return (name, h)
{-# LINE 57 "libraries/unix/System/Posix/Temp.hsc" #-}
foreign import capi unsafe "HsUnix.h mkstemps"
c_mkstemps :: CString -> CInt -> IO CInt
{-# LINE 60 "libraries/unix/System/Posix/Temp.hsc" #-}
mkstemps :: String -> String -> IO (FilePath, Handle)
{-# LINE 72 "libraries/unix/System/Posix/Temp.hsc" #-}
mkstemps prefix suffix = do
let template = prefix ++ "XXXXXX" ++ suffix
lenOfsuf = (fromIntegral $ length suffix) :: CInt
withFilePath template $ \ ptr -> do
fd <- throwErrnoIfMinus1 "mkstemps" (c_mkstemps ptr lenOfsuf)
name <- peekFilePath ptr
h <- fdToHandle (Fd fd)
return (name, h)
{-# LINE 83 "libraries/unix/System/Posix/Temp.hsc" #-}
{-# LINE 85 "libraries/unix/System/Posix/Temp.hsc" #-}
foreign import capi unsafe "HsUnix.h mkdtemp"
c_mkdtemp :: CString -> IO CString
{-# LINE 88 "libraries/unix/System/Posix/Temp.hsc" #-}
mkdtemp :: String -> IO FilePath
mkdtemp template' = do
let template = template' ++ "XXXXXX"
{-# LINE 100 "libraries/unix/System/Posix/Temp.hsc" #-}
withFilePath template $ \ ptr -> do
_ <- throwErrnoIfNull "mkdtemp" (c_mkdtemp ptr)
name <- peekFilePath ptr
return name
{-# LINE 109 "libraries/unix/System/Posix/Temp.hsc" #-}
{-# LINE 124 "libraries/unix/System/Posix/Temp.hsc" #-}