HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleunix-2.8.7.0Haskell2010

System.Posix.IO

POSIX IO support. These types and functions correspond to the unix functions open(2), close(2), etc. For more portable functions which are more like fopen(3) and friends from stdio.h, see System.IO.

  • 5 types
  • 24 values
  • Packageunix-2.8.7.0
  • Exports29
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceIO.hsc

Input / Output

0 declarations

Standard file descriptors

Opening and closing files

datadata OpenFileFlags
#

Correspond to some of the int flags from C's fcntl.h.

Constructors

Instances4Eq, Ord, Read, Show
valueopenFdAt
  1. :: Maybe Fd

    Optional directory file descriptor

  2. -> FilePath

    Pathname to open

  3. -> OpenMode

    Read-only, read-write or write-only

  4. -> OpenFileFlags

    Append, exclusive, truncate, etc.

  5. -> IO Fd
#

Open a file relative to an optional directory file descriptor.

Directory file descriptors can be used to avoid some race conditions when navigating changing directory trees, or to retain access to a portion of the directory tree that would otherwise become inaccessible after dropping privileges.

valuecreateFileAt
  1. :: Maybe Fd

    Optional directory file descriptor

  2. -> FilePath

    Pathname to create

  3. -> FileMode

    File permission bits (before umask)

  4. -> IO Fd
#

Create and open a file for write-only, with default flags, relative an optional directory file-descriptor.

Directory file descriptors can be used to avoid some race conditions when navigating changing directory trees, or to retain access to a portion of the directory tree that would otherwise become inaccessible after dropping privileges.

valuecloseFd :: Fd -> IO ()
#

Close this file descriptor. May throw an exception if this is an invalid descriptor.

Reading/writing data

Programmers using the fdRead and fdWrite API should be aware that EAGAIN exceptions may occur for non-blocking IO!

valuefdRead
  1. :: Fd
  2. -> ByteCount

    How many bytes to read

  3. -> IO (String, ByteCount)

    The bytes read, how many bytes were read.

#

Deprecated. This function is scheduled to be dropped in favor of fdRead, because decoding e.g. UTF-8 streams partially is unsafe.

Read data from an Fd and convert it to a String using the locale encoding. Throws an exception if this is an invalid descriptor, or EOF has been reached.

valuefdReadBuf
  1. :: Fd
  2. -> Ptr Word8

    Memory in which to put the data

  3. -> ByteCount

    Maximum number of bytes to read

  4. -> IO ByteCount

    Number of bytes read (zero for EOF)

#

Read data from an Fd into memory. This is exactly equivalent to the POSIX read function.

valuefdWriteBuf
  1. :: Fd
  2. -> Ptr Word8

    Memory containing the data to write

  3. -> ByteCount

    Maximum number of bytes to write

  4. -> IO ByteCount

    Number of bytes written

#

Write data from memory to an Fd. This is exactly equivalent to the POSIX write function.

Seeking

File options

Locking

valuesetLock :: Fd -> FileLock -> IO ()
#

May throw an exception if this is an invalid descriptor.

Pipes

valuecreatePipe :: IO (Fd, Fd)
#

The createPipe function creates a pair of connected file descriptors. The first component is the fd to read from, the second is the write end. Although pipes may be bidirectional, this behaviour is not portable and programmers should use two separate pipes for this purpose. May throw an exception if this is an invalid descriptor.

Duplicating file descriptors

valuedup :: Fd -> IO Fd
#

May throw an exception if this is an invalid descriptor.

valuedupTo :: Fd -> Fd -> IO Fd
#

May throw an exception if this is an invalid descriptor.

Converting file descriptors to/from Handles

valuehandleToFd :: Handle -> IO Fd
#

Extracts the Fd from a Handle. This function has the side effect of closing the Handle (and flushing its write buffer, if necessary), without closing the underlying Fd.

Warning: This means you take over ownership of the underlying Fd. hClose on the Handle will no longer have any effect. This will break common patterns to avoid file descriptor leaks, such as using hClose in the cleanup action of Control.Exception.bracket, making it a silent no-op. Be sure to close the returned Fd yourself to not leak it.