unix-pty-light-0.1: POSIX pseudo-terminal support




POSIX pseudo-terminal support. The functions openPt, grantPt, unlockPt, and ptsName all correspond directly to POSIX functions, whereas the openPseudoTerminal function provides more convenient interface to pseudo-terminal functionality.

Note that later versions of the unix package include pseudo-terminal support in the System.Posix.Terminal module. You should probably only use this module if you need backwards compatibility with GHC versions earlier than 6.8, or if you want direct bindings to the low-level pseudo-terminal functions.


Opening a pseudo-terminal



:: CInt

bitwise-or of desired flags

-> IO Fd 

Opens a pseudo-terminal master and associates it with a file descriptor.

Corresponds to posix_openpt from <stdlib.h>.


oRDWR :: CIntSource

Causes openPt to open the pseudo-terminal for both reading and writing. (This flag is usually specified.)

Corresponds to O_RDWR from <fcntl.h>.

oNOCTTY :: CIntSource

Prevents openPt from causing the terminal device to become the controlling terminal for the process.

Corresponds to O_NOCTTY from <fcntl.h>.

Allowing access

grantPt :: Fd -> IO ()Source

Change the ownership of the pseudo-terminal slave corresponding to the given master so that it belongs to and can be read/written by the current process's owner. This should be called before unlockPt.

Corresponds to grantpt from <stdlib.h>.

unlockPt :: Fd -> IO ()Source

Unlocks the pseudo-terminal slave corresponding to the given master. This must be called before opening the slave side of the pseudo-terminal.

Corresponds to unlockpt from <stdlib.h>.

ptsName :: Fd -> IO StringSource

Obtains the name of a pseudo-terminal slave device from the master's file descriptor.

Corresponds to ptsname from <stdlib.h>.

Convenience wrapper

openPseudoTerminal :: IO (Fd, String)Source

Creates, grants, and unlocks a read/write pseudo-terminal, returning an open file descriptor for the master device and the file-system path of the slave device.

Performs the following actions:

  • opens a pseudo-terminal
  • grants and unlocks its slave
  • obtains the slave's name
  • returns the master's file descriptor and the slave's name

If an exception is thrown, the master file descriptor will be closed. Otherwise, the caller is responsible for closing it.


For convenience, the Fd type and closeFd function are re-exported from this module.

closeFd :: Fd -> IO ()

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