module Unix.C.Errors
    ( orErrno
    ) where

import Foreign.C.Error
import Zhp

orErrno :: IO a -> IO (Either Errno a)
orErrno :: IO a -> IO (Either Errno a)
orErrno IO a
io = do
    IO ()
resetErrno
    a
r <- IO a
io
    Errno
e <- IO Errno
getErrno
    Either Errno a -> IO (Either Errno a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Errno a -> IO (Either Errno a))
-> Either Errno a -> IO (Either Errno a)
forall a b. (a -> b) -> a -> b
$! if Errno
e Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
/= Errno
eOK
        then Errno -> Either Errno a
forall a b. a -> Either a b
Left Errno
e
        else a -> Either Errno a
forall a b. b -> Either a b
Right a
r