1
{-# LANGUAGE ForeignFunctionInterface #-}
2
-----------------------------------------------------------------------------
4
-- Module : System.Posix.Temp
5
-- Copyright : (c) Volker Stolz <vs@foldr.org>
6
-- License : BSD-style (see the file libraries/base/LICENSE)
8
-- Maintainer : vs@foldr.org
9
-- Stability : provisional
10
-- Portability : non-portable (requires POSIX)
12
-- POSIX environment support
14
-----------------------------------------------------------------------------
16
module System.Posix.Temp (
21
tmpfile: can we handle FILE*?
22
tmpnam: ISO C, should go in base?
31
import System.Posix.IO
32
import System.Posix.Types
35
-- |'mkstemp' - make a unique filename and open it for
36
-- reading\/writing (only safe on GHC & Hugs).
37
-- The returned 'FilePath' is the (possibly relative) path of
38
-- the created file, which is padded with 6 random characters.
39
mkstemp :: String -> IO (FilePath, Handle)
41
#if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
42
withCString template $ \ ptr -> do
43
fd <- throwErrnoIfMinus1 "mkstemp" (c_mkstemp ptr)
44
name <- peekCString ptr
45
h <- fdToHandle (Fd fd)
48
name <- mktemp (template ++ "XXXXXX")
49
h <- openFile name ReadWriteMode
52
-- |'mktemp' - make a unique file name
53
-- This function should be considered deprecated
55
mktemp :: String -> IO String
57
withCString template $ \ ptr -> do
58
ptr <- throwErrnoIfNull "mktemp" (c_mktemp ptr)
61
foreign import ccall unsafe "mktemp"
62
c_mktemp :: CString -> IO CString
65
foreign import ccall unsafe "HsUnix.h __hscore_mkstemp"
66
c_mkstemp :: CString -> IO CInt