Add simple haddock docs for throwErrnoPath* functions
[ghc-base.git] / Foreign / C / Error.hs
index 927924c..c3cd68c 100644 (file)
@@ -1,4 +1,4 @@
-{-# OPTIONS -fno-implicit-prelude -#include "HsBase.h" #-}
+{-# OPTIONS_GHC -fno-implicit-prelude -#include "HsBase.h" #-}
 -----------------------------------------------------------------------------
 -- |
 -- Module      :  Foreign.C.Error
@@ -91,7 +91,7 @@ module Foreign.C.Error (
 -- calculated for us
 --
 #ifndef __NHC__
-#include "ghcconfig.h"
+#include "HsBaseConfig.h"
 #endif
 
 -- system dependent imports
@@ -118,13 +118,17 @@ import Data.Maybe
 import GHC.IOBase
 import GHC.Num
 import GHC.Base
+#elif __HUGS__
+import Hugs.Prelude            ( Handle, IOError, ioError )
+import System.IO.Unsafe                ( unsafePerformIO )
 #else
-import System.IO               ( IOError, Handle, ioError )
+import System.IO               ( Handle )
+import System.IO.Error         ( IOError, ioError )
 import System.IO.Unsafe                ( unsafePerformIO )
 #endif
 
 #ifdef __HUGS__
-{-# CBITS PrelIOUtils.c #-}
+{-# CFILES cbits/PrelIOUtils.c #-}
 #endif
 
 
@@ -444,6 +448,47 @@ throwErrnoIfNullRetry  = throwErrnoIfRetry (== nullPtr)
 throwErrnoIfNullRetryMayBlock :: String -> IO (Ptr a) -> IO b -> IO (Ptr a)
 throwErrnoIfNullRetryMayBlock  = throwErrnoIfRetryMayBlock (== nullPtr)
 
+-- | as 'throwErrno', but exceptions include the given path when appropriate.
+--
+throwErrnoPath :: String -> FilePath -> IO a
+throwErrnoPath loc path =
+  do
+    errno <- getErrno
+    ioError (errnoToIOError loc errno Nothing (Just path))
+
+-- | as 'throwErrnoIf', but exceptions include the given path when
+--   appropriate.
+--
+throwErrnoPathIf :: (a -> Bool) -> String -> FilePath -> IO a -> IO a
+throwErrnoPathIf pred loc path f =
+  do
+    res <- f
+    if pred res then throwErrnoPath loc path else return res
+
+-- | as 'throwErrnoIf_', but exceptions include the given path when
+--   appropriate.
+--
+throwErrnoPathIf_ :: (a -> Bool) -> String -> FilePath -> IO a -> IO ()
+throwErrnoPathIf_ pred loc path f  = void $ throwErrnoPathIf pred loc path f
+
+-- | as 'throwErrnoIfNull', but exceptions include the given path when
+--   appropriate.
+--
+throwErrnoPathIfNull :: String -> FilePath -> IO (Ptr a) -> IO (Ptr a)
+throwErrnoPathIfNull  = throwErrnoPathIf (== nullPtr)
+
+-- | as 'throwErrnoIfMinus1', but exceptions include the given path when
+--   appropriate.
+--
+throwErrnoPathIfMinus1 :: Num a => String -> FilePath -> IO a -> IO a
+throwErrnoPathIfMinus1 = throwErrnoPathIf (== -1)
+
+-- | as 'throwErrnoIfMinus1_', but exceptions include the given path when
+--   appropriate.
+--
+throwErrnoPathIfMinus1_ :: Num a => String -> FilePath -> IO a -> IO ()
+throwErrnoPathIfMinus1_  = throwErrnoPathIf_ (== -1)
+
 -- conversion of an "errno" value into IO error
 -- --------------------------------------------
 
@@ -471,7 +516,7 @@ errnoToIOError loc errno maybeHdl maybeName = unsafePerformIO $ do
         | errno == eAFNOSUPPORT    = UnsupportedOperation
         | errno == eAGAIN          = ResourceExhausted
         | errno == eALREADY        = AlreadyExists
-        | errno == eBADF           = OtherError
+        | errno == eBADF           = InvalidArgument
         | errno == eBADMSG         = InappropriateType
         | errno == eBADRPC         = OtherError
         | errno == eBUSY           = ResourceBusy