module Foreign.Handle (
fdopen, fflush, fclose,
handleToCFile
) where
import GHC.IO.Handle
import Foreign
import Foreign.C
import System.Posix.IO
import System.Posix.Types
foreign import ccall unsafe "stdio.h fdopen" fdopen :: Fd -> CString -> IO (Ptr CFile)
foreign import ccall unsafe "stdio.h fflush" fflush :: Ptr CFile -> IO ()
foreign import ccall unsafe "stdio.h fclose" fclose :: Ptr CFile -> IO ()
handleToCFile :: Handle -> String -> IO (Ptr CFile)
handleToCFile h m = withCAString m $ \iomode -> do
h' <- hDuplicate h
fd <- handleToFd h'
fdopen fd iomode