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