{-# LINE 1 "libraries/unix/System/Posix/Directory/Common.hsc" #-}

{-# LINE 2 "libraries/unix/System/Posix/Directory/Common.hsc" #-}
{-# LANGUAGE Safe #-}

{-# LINE 6 "libraries/unix/System/Posix/Directory/Common.hsc" #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  System.Posix.Directory.Common
-- Copyright   :  (c) The University of Glasgow 2002
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  provisional
-- Portability :  non-portable (requires POSIX)
--
-- POSIX directory support
--
-----------------------------------------------------------------------------



module System.Posix.Directory.Common (
       DirStream(..), CDir, CDirent, DirStreamOffset(..),
       rewindDirStream,
       closeDirStream,

{-# LINE 28 "libraries/unix/System/Posix/Directory/Common.hsc" #-}
       seekDirStream,

{-# LINE 30 "libraries/unix/System/Posix/Directory/Common.hsc" #-}

{-# LINE 31 "libraries/unix/System/Posix/Directory/Common.hsc" #-}
       tellDirStream,

{-# LINE 33 "libraries/unix/System/Posix/Directory/Common.hsc" #-}
       changeWorkingDirectoryFd,
  ) where

import System.Posix.Types
import Foreign
import Foreign.C

newtype DirStream = DirStream (Ptr CDir)

data {-# CTYPE "DIR" #-} CDir
data {-# CTYPE "struct dirent" #-} CDirent

-- | @rewindDirStream dp@ calls @rewinddir@ to reposition
--   the directory stream @dp@ at the beginning of the directory.
rewindDirStream :: DirStream -> IO ()
rewindDirStream :: DirStream -> IO ()
rewindDirStream (DirStream Ptr CDir
dirp) = Ptr CDir -> IO ()
c_rewinddir Ptr CDir
dirp

foreign import ccall unsafe "rewinddir"
   c_rewinddir :: Ptr CDir -> IO ()

-- | @closeDirStream dp@ calls @closedir@ to close
--   the directory stream @dp@.
closeDirStream :: DirStream -> IO ()
closeDirStream :: DirStream -> IO ()
closeDirStream (DirStream Ptr CDir
dirp) = do
  String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1Retry_ String
"closeDirStream" (Ptr CDir -> IO CInt
c_closedir Ptr CDir
dirp)

foreign import ccall unsafe "closedir"
   c_closedir :: Ptr CDir -> IO CInt

newtype DirStreamOffset = DirStreamOffset COff


{-# LINE 65 "libraries/unix/System/Posix/Directory/Common.hsc" #-}
seekDirStream :: DirStream -> DirStreamOffset -> IO ()
seekDirStream :: DirStream -> DirStreamOffset -> IO ()
seekDirStream (DirStream Ptr CDir
dirp) (DirStreamOffset COff
off) =
  Ptr CDir -> CLong -> IO ()
c_seekdir Ptr CDir
dirp (COff -> CLong
forall a b. (Integral a, Num b) => a -> b
fromIntegral COff
off) -- TODO: check for CLong/COff overflow

foreign import ccall unsafe "seekdir"
  c_seekdir :: Ptr CDir -> CLong -> IO ()

{-# LINE 72 "libraries/unix/System/Posix/Directory/Common.hsc" #-}


{-# LINE 74 "libraries/unix/System/Posix/Directory/Common.hsc" #-}
tellDirStream :: DirStream -> IO DirStreamOffset
tellDirStream :: DirStream -> IO DirStreamOffset
tellDirStream (DirStream Ptr CDir
dirp) = do
  CLong
off <- Ptr CDir -> IO CLong
c_telldir Ptr CDir
dirp
  DirStreamOffset -> IO DirStreamOffset
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (COff -> DirStreamOffset
DirStreamOffset (CLong -> COff
forall a b. (Integral a, Num b) => a -> b
fromIntegral CLong
off)) -- TODO: check for overflow

foreign import ccall unsafe "telldir"
  c_telldir :: Ptr CDir -> IO CLong

{-# LINE 82 "libraries/unix/System/Posix/Directory/Common.hsc" #-}

changeWorkingDirectoryFd :: Fd -> IO ()
changeWorkingDirectoryFd :: Fd -> IO ()
changeWorkingDirectoryFd (Fd CInt
fd) =
  String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1Retry_ String
"changeWorkingDirectoryFd" (CInt -> IO CInt
c_fchdir CInt
fd)

foreign import ccall unsafe "fchdir"
  c_fchdir :: CInt -> IO CInt