[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / lib / posix / PosixUtil.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-1996
3 %
4 \section[PosixUtil]{Haskell 1.3 POSIX utilities}
5
6 \begin{code}
7 module PosixUtil where
8
9 import GlaExts
10 import PrelIOBase  -- IOError representation
11 \end{code}
12
13 First, all of the major Posix data types, to avoid any recursive dependencies
14
15 \begin{code}
16 type ByteCount          = Int
17 type ClockTick          = Int
18 type EpochTime          = Int
19 type FileOffset         = Int
20 type GroupID            = Int
21 type Limit              = Int
22 type LinkCount          = Int
23 type ProcessID          = Int
24 type ProcessGroupID     = ProcessID
25 type UserID             = Int
26 data Fd                 = FD# Int#
27 instance CCallable   Fd
28 instance CReturnable Fd
29
30 instance Eq Fd where
31   (FD# x#) == (FD# y#) = x# ==# y#
32
33 -- use with care.
34 intToFd :: Int -> Fd
35 intToFd (I# fd#) = FD# fd#
36
37 fdToInt :: Fd -> Int
38 fdToInt (FD# x#) = I# x#
39 \end{code}
40
41 Now some local functions that shouldn't go outside this library.
42
43 Fail with a SystemError.  Normally, we do not try to re-interpret
44 POSIX error numbers, so most routines in this file will only fail
45 with SystemError.  The only exceptions are (1) those routines where
46 failure of some kind may be considered ``normal''...e.g. getpwnam()
47 for a non-existent user, or (2) those routines which do not set
48 errno.
49
50 \begin{code}
51 syserr :: String -> IO a
52 syserr str = fail (IOError Nothing     -- ToDo: better
53                            SystemError
54                            str
55                            "")
56
57 -- common templates for system calls
58
59 nonzero_error :: IO Int -> String -> IO ()
60 nonzero_error io err = do
61     rc <- io
62     if rc == 0
63        then return ()
64        else syserr err
65
66 minusone_error :: IO Int -> String -> IO ()
67 minusone_error io err = do
68     rc <- io
69     if rc /= -1
70        then return ()
71        else syserr err
72
73 \end{code}