{-# LINE 1 "System/Directory/Internal/Posix.hsc" #-}
{-# LINE 2 "System/Directory/Internal/Posix.hsc" #-}
{-# LINE 4 "System/Directory/Internal/Posix.hsc" #-}
{-# LINE 5 "System/Directory/Internal/Posix.hsc" #-}
{-# LINE 6 "System/Directory/Internal/Posix.hsc" #-}
module System.Directory.Internal.Posix where
{-# LINE 9 "System/Directory/Internal/Posix.hsc" #-}
import Control.Monad ((>=>))
import Control.Exception (bracket)
import Foreign
import Foreign.C
foreign import ccall unsafe "free" c_free :: Ptr a -> IO ()
c_PATH_MAX :: Maybe Int
{-# LINE 20 "System/Directory/Internal/Posix.hsc" #-}
c_PATH_MAX | c_PATH_MAX' > toInteger maxValue = Nothing
| otherwise = Just (fromInteger c_PATH_MAX')
where c_PATH_MAX' = (4096)
{-# LINE 23 "System/Directory/Internal/Posix.hsc" #-}
maxValue = maxBound `asTypeOf` case c_PATH_MAX of ~(Just x) -> x
{-# LINE 27 "System/Directory/Internal/Posix.hsc" #-}
foreign import ccall "realpath" c_realpath
:: CString -> CString -> IO CString
withRealpath :: CString -> (CString -> IO a) -> IO a
withRealpath path action = case c_PATH_MAX of
Nothing ->
bracket (realpath nullPtr) c_free action
Just pathMax ->
allocaBytes (pathMax + 1) (realpath >=> action)
where realpath = throwErrnoIfNull "" . c_realpath path
{-# LINE 43 "System/Directory/Internal/Posix.hsc" #-}