~ubuntu-branches/ubuntu/precise/ghc/precise

« back to all changes in this revision

Viewing changes to libraries/unix/System/Posix/Temp.hsc

  • Committer: Bazaar Package Importer
  • Author(s): Joachim Breitner
  • Date: 2011-01-17 12:49:24 UTC
  • Revision ID: james.westby@ubuntu.com-20110117124924-do1pym1jlf5o636m
Tags: upstream-7.0.1
ImportĀ upstreamĀ versionĀ 7.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{-# LANGUAGE ForeignFunctionInterface #-}
 
2
-----------------------------------------------------------------------------
 
3
-- |
 
4
-- Module      :  System.Posix.Temp
 
5
-- Copyright   :  (c) Volker Stolz <vs@foldr.org>
 
6
-- License     :  BSD-style (see the file libraries/base/LICENSE)
 
7
--
 
8
-- Maintainer  :  vs@foldr.org
 
9
-- Stability   :  provisional
 
10
-- Portability :  non-portable (requires POSIX)
 
11
--
 
12
-- POSIX environment support
 
13
--
 
14
-----------------------------------------------------------------------------
 
15
 
 
16
module System.Posix.Temp (
 
17
 
 
18
    mkstemp
 
19
 
 
20
{- Not ported (yet?):
 
21
    tmpfile: can we handle FILE*?
 
22
    tmpnam: ISO C, should go in base?
 
23
    tempname: dito
 
24
-}
 
25
 
 
26
) where
 
27
 
 
28
#include "HsUnix.h"
 
29
 
 
30
import System.IO
 
31
import System.Posix.IO
 
32
import System.Posix.Types
 
33
import Foreign.C
 
34
 
 
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)
 
40
mkstemp template = do
 
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)
 
46
    return (name, h)
 
47
#else
 
48
  name <- mktemp (template ++ "XXXXXX")
 
49
  h <- openFile name ReadWriteMode
 
50
  return (name, h)
 
51
 
 
52
-- |'mktemp' - make a unique file name
 
53
-- This function should be considered deprecated
 
54
 
 
55
mktemp :: String -> IO String
 
56
mktemp template = do
 
57
  withCString template $ \ ptr -> do
 
58
    ptr <- throwErrnoIfNull "mktemp" (c_mktemp ptr)
 
59
    peekCString ptr
 
60
 
 
61
foreign import ccall unsafe "mktemp"
 
62
  c_mktemp :: CString -> IO CString
 
63
#endif
 
64
 
 
65
foreign import ccall unsafe "HsUnix.h __hscore_mkstemp"
 
66
  c_mkstemp :: CString -> IO CInt
 
67