diff options
author | Simon Marlow <marlowsd@gmail.com> | 2011-11-11 16:18:48 +0000 |
---|---|---|
committer | Simon Marlow <marlowsd@gmail.com> | 2011-11-22 12:36:48 +0000 |
commit | 34c7bf896f19b182cf6fa104e057f1df9df1254a (patch) | |
tree | abdb8264ae52c62263fc0fb4b395906a64acb104 /System/Posix/Temp | |
parent | c213ae2ec6d9c71266aebc8e5b2326a9625fba7a (diff) |
Provide a raw ByteString version of FilePath and environment APIs
The new module System.Posix.ByteString provides exactly the same API
as System.Posix, except that:
- There is a new type: RawFilePath = ByteString
- All functions mentioning FilePath in the System.Posix API
use RawFilePath in the System.Posix.ByteString API
- RawFilePaths are not subject to Unicode locale encoding and
decoding, unlike FilePaths. They are the exact bytes passed to and
returned from the underlying POSIX API.
- Similarly for functions that deal in environment
strings (System.Posix.Env): these use untranslated ByteStrings
in System.Posix.Environment
- There is a new function
System.Posix.ByteString.getArgs :: [ByteString]
returning the raw untranslated arguments as passed to exec() when
the program was started.
Diffstat (limited to 'System/Posix/Temp')
-rw-r--r-- | System/Posix/Temp/ByteString.hsc | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/System/Posix/Temp/ByteString.hsc b/System/Posix/Temp/ByteString.hsc new file mode 100644 index 0000000..c5f8906 --- /dev/null +++ b/System/Posix/Temp/ByteString.hsc @@ -0,0 +1,82 @@ +{-# LANGUAGE ForeignFunctionInterface #-} +#if __GLASGOW_HASKELL__ >= 701 +{-# LANGUAGE Trustworthy #-} +#endif +----------------------------------------------------------------------------- +-- | +-- Module : System.Posix.Temp.ByteString +-- Copyright : (c) Volker Stolz <vs@foldr.org> +-- License : BSD-style (see the file libraries/base/LICENSE) +-- +-- Maintainer : vs@foldr.org +-- Stability : provisional +-- Portability : non-portable (requires POSIX) +-- +-- POSIX environment support +-- +----------------------------------------------------------------------------- + +module System.Posix.Temp.ByteString ( + + mkstemp + +{- Not ported (yet?): + tmpfile: can we handle FILE*? + tmpnam: ISO C, should go in base? + tempname: dito +-} + +) where + +#include "HsUnix.h" + +import System.IO (Handle) +import System.Posix.IO +import System.Posix.Types + +import Foreign.C hiding ( + throwErrnoPath, + throwErrnoPathIf, + throwErrnoPathIf_, + throwErrnoPathIfNull, + throwErrnoPathIfMinus1, + throwErrnoPathIfMinus1_ ) + +import System.Posix.ByteString.FilePath + +import Data.ByteString (ByteString) + + +-- |'mkstemp' - make a unique filename and open it for +-- reading\/writing (only safe on GHC & Hugs). +-- The returned 'RawFilePath' is the (possibly relative) path of +-- the created file, which is padded with 6 random characters. +mkstemp :: ByteString -> IO (RawFilePath, Handle) +mkstemp template = do +#if defined(__GLASGOW_HASKELL__) || defined(__HUGS__) + withFilePath template $ \ ptr -> do + fd <- throwErrnoIfMinus1 "mkstemp" (c_mkstemp ptr) + name <- peekFilePath ptr + h <- fdToHandle (Fd fd) + return (name, h) +#else + name <- mktemp (template ++ "XXXXXX") + h <- openFile name ReadWriteMode + return (name, h) + +-- |'mktemp' - make a unique file name +-- This function should be considered deprecated + +mktemp :: ByteString -> IO RawFilePath +mktemp template = do + withFilePath template $ \ ptr -> do + ptr <- throwErrnoIfNull "mktemp" (c_mktemp ptr) + peekFilePath ptr + +foreign import ccall unsafe "mktemp" + c_mktemp :: CString -> IO CString +#endif + +foreign import ccall unsafe "HsUnix.h __hscore_mkstemp" + c_mkstemp :: CString -> IO CInt + |