1 {-# OPTIONS -fno-implicit-prelude #-}
3 -- ---------------------------------------------------------------------------
5 -- POSIX support layer for the standard libraries
7 -- Non-posix compliant in order to support the following features:
8 -- * S_ISSOCK (no sockets in POSIX)
10 module PrelPosix where
12 -- See above comment for non-Posixness reasons.
13 -- #include "PosixSource.h"
29 import PrelMarshalAlloc
30 import PrelMarshalUtils
36 -- ---------------------------------------------------------------------------
42 type CDev = #type dev_t
43 type CIno = #type ino_t
44 type CMode = #type mode_t
45 type COff = #type off_t
46 type CPid = #type pid_t
48 #ifdef mingw32_TARGET_OS
49 type CSsize = #type size_t
51 type CGid = #type gid_t
52 type CNlink = #type nlink_t
53 type CSsize = #type ssize_t
54 type CUid = #type uid_t
56 type CSpeed = #type speed_t
57 type CTcflag = #type tcflag_t
60 -- ---------------------------------------------------------------------------
61 -- stat()-related stuff
65 fdFileSize :: Int -> IO Integer
67 allocaBytes (#const sizeof(struct stat)) $ \ p_stat -> do
68 throwErrnoIfMinus1Retry "fileSize" $
69 c_fstat (fromIntegral fd) p_stat
70 c_mode <- (#peek struct stat, st_mode) p_stat :: IO CMode
71 if not (s_isreg c_mode)
74 c_size <- (#peek struct stat, st_size) p_stat :: IO COff
75 return (fromIntegral c_size)
77 data FDType = Directory | Stream | RegularFile
80 -- NOTE: On Win32 platforms, this will only work with file descriptors
81 -- referring to file handles. i.e., it'll fail for socket FDs.
82 fdType :: Int -> IO FDType
84 allocaBytes (#const sizeof(struct stat)) $ \ p_stat -> do
85 throwErrnoIfMinus1Retry "fdType" $
86 c_fstat (fromIntegral fd) p_stat
87 c_mode <- (#peek struct stat, st_mode) p_stat :: IO CMode
89 _ | s_isdir c_mode -> return Directory
90 | s_isfifo c_mode || s_issock c_mode -> return Stream
91 | s_isreg c_mode -> return RegularFile
92 | otherwise -> ioException ioe_unknownfiletype
94 ioe_unknownfiletype = IOError Nothing UnsupportedOperation "fdType"
95 "unknown file type" Nothing
97 foreign import "s_isreg_PrelPosix_wrap" unsafe s_isreg :: CMode -> Bool
98 #def inline int s_isreg_PrelPosix_wrap(m) { return S_ISREG(m); }
100 foreign import "s_isdir_PrelPosix_wrap" unsafe s_isdir :: CMode -> Bool
101 #def inline int s_isdir_PrelPosix_wrap(m) { return S_ISDIR(m); }
103 foreign import "s_isfifo_PrelPosix_wrap" unsafe s_isfifo :: CMode -> Bool
104 #def inline int s_isfifo_PrelPosix_wrap(m) { return S_ISFIFO(m); }
106 #ifndef mingw32_TARGET_OS
107 foreign import "s_issock_PrelPosix_wrap" unsafe s_issock :: CMode -> Bool
108 #def inline int s_issock_PrelPosix_wrap(m) { return S_ISSOCK(m); }
110 s_issock :: CMode -> Bool
111 s_issock cmode = False
114 -- It isn't clear whether ftruncate is POSIX or not (I've read several
115 -- manpages and they seem to conflict), so we truncate using open/2.
116 fileTruncate :: FilePath -> IO ()
117 fileTruncate file = do
118 let flags = o_WRONLY .|. o_TRUNC
119 withCString file $ \file_cstr -> do
120 fd <- fromIntegral `liftM`
121 throwErrnoIfMinus1Retry "fileTruncate"
122 (c_open file_cstr (fromIntegral flags) 0o666)
126 -- ---------------------------------------------------------------------------
127 -- Terminal-related stuff
129 fdIsTTY :: Int -> IO Bool
130 fdIsTTY fd = c_isatty (fromIntegral fd) >>= return.toBool
132 #ifndef mingw32_TARGET_OS
136 setEcho :: Int -> Bool -> IO ()
138 allocaBytes (#const sizeof(struct termios)) $ \p_tios -> do
139 throwErrnoIfMinus1Retry "setEcho"
140 (c_tcgetattr (fromIntegral fd) p_tios)
141 c_lflag <- (#peek struct termios, c_lflag) p_tios :: IO CTcflag
142 let new_c_lflag | on = c_lflag .|. (#const ECHO)
143 | otherwise = c_lflag .&. complement (#const ECHO)
144 (#poke struct termios, c_lflag) p_tios (new_c_lflag :: CTcflag)
145 tcSetAttr fd (#const TCSANOW) p_tios
147 getEcho :: Int -> IO Bool
149 allocaBytes (#const sizeof(struct termios)) $ \p_tios -> do
150 throwErrnoIfMinus1Retry "setEcho"
151 (c_tcgetattr (fromIntegral fd) p_tios)
152 c_lflag <- (#peek struct termios, c_lflag) p_tios :: IO CTcflag
153 return ((c_lflag .&. (#const ECHO)) /= 0)
155 setCooked :: Int -> Bool -> IO ()
156 setCooked fd cooked =
157 allocaBytes (#const sizeof(struct termios)) $ \p_tios -> do
158 throwErrnoIfMinus1Retry "setCooked"
159 (c_tcgetattr (fromIntegral fd) p_tios)
161 -- turn on/off ICANON
162 c_lflag <- (#peek struct termios, c_lflag) p_tios :: IO CTcflag
163 let new_c_lflag | cooked = c_lflag .|. (#const ICANON)
164 | otherwise = c_lflag .&. complement (#const ICANON)
165 (#poke struct termios, c_lflag) p_tios (new_c_lflag :: CTcflag)
167 -- set VMIN & VTIME to 1/0 respectively
169 let c_cc = (#ptr struct termios, c_cc) p_tios
170 vmin = c_cc `plusPtr` (#const VMIN) :: Ptr Word8
171 vtime = c_cc `plusPtr` (#const VTIME) :: Ptr Word8
175 tcSetAttr fd (#const TCSANOW) p_tios
177 -- tcsetattr() when invoked by a background process causes the process
178 -- to be sent SIGTTOU regardless of whether the process has TOSTOP set
179 -- in its terminal flags (try it...). This function provides a
180 -- wrapper which temporarily blocks SIGTTOU around the call, making it
183 tcSetAttr :: FD -> CInt -> Ptr Termios -> IO ()
184 tcSetAttr fd options p_tios = do
185 allocaBytes (#const sizeof(sigset_t)) $ \ p_sigset -> do
186 allocaBytes (#const sizeof(sigset_t)) $ \ p_old_sigset -> do
187 c_sigemptyset p_sigset
188 c_sigaddset p_sigset (#const SIGTTOU)
189 c_sigprocmask (#const SIG_BLOCK) p_sigset p_old_sigset
190 throwErrnoIfMinus1Retry_ "tcSetAttr" $
191 c_tcsetattr (fromIntegral fd) options p_tios
192 c_sigprocmask (#const SIG_SETMASK) p_old_sigset nullPtr
196 -- bogus defns for win32
197 setCooked :: Int -> Bool -> IO ()
198 setCooked fd cooked = return ()
200 setEcho :: Int -> Bool -> IO ()
201 setEcho fd on = return ()
203 getEcho :: Int -> IO Bool
204 getEcho fd = return False
208 -- ---------------------------------------------------------------------------
209 -- Turning on non-blocking for a file descriptor
211 #ifndef mingw32_TARGET_OS
213 setNonBlockingFD fd = do
214 flags <- throwErrnoIfMinus1Retry "setNonBlockingFD"
215 (fcntl_read (fromIntegral fd) (#const F_GETFL))
216 -- An error when setting O_NONBLOCK isn't fatal: on some systems
217 -- there are certain file handles on which this will fail (eg. /dev/null
218 -- on FreeBSD) so we throw away the return code from fcntl_write.
219 fcntl_write (fromIntegral fd)
220 (#const F_SETFL) (flags .|. #const O_NONBLOCK)
223 -- bogus defns for win32
224 setNonBlockingFD fd = return ()
228 -- -----------------------------------------------------------------------------
231 foreign import "stat" unsafe
232 c_stat :: CString -> Ptr CStat -> IO CInt
234 foreign import "fstat" unsafe
235 c_fstat :: CInt -> Ptr CStat -> IO CInt
238 foreign import "lstat" unsafe
239 c_lstat :: CString -> Ptr CStat -> IO CInt
242 foreign import "open" unsafe
243 c_open :: CString -> CInt -> CMode -> IO CInt
246 o_RDONLY = (#const O_RDONLY) :: CInt
247 o_WRONLY = (#const O_WRONLY) :: CInt
248 o_RDWR = (#const O_RDWR) :: CInt
249 o_APPEND = (#const O_APPEND) :: CInt
250 o_CREAT = (#const O_CREAT) :: CInt
251 o_EXCL = (#const O_EXCL) :: CInt
252 o_TRUNC = (#const O_TRUNC) :: CInt
254 #ifdef mingw32_TARGET_OS
256 o_NONBLOCK = 0 :: CInt
258 o_NOCTTY = (#const O_NOCTTY) :: CInt
259 o_NONBLOCK = (#const O_NONBLOCK) :: CInt
263 o_BINARY = (#const O_BINARY) :: CInt
266 foreign import "isatty" unsafe
267 c_isatty :: CInt -> IO CInt
269 foreign import "close" unsafe
270 c_close :: CInt -> IO CInt
272 #ifdef mingw32_TARGET_OS
273 closeFd :: Bool -> CInt -> IO CInt
275 | isStream = c_closesocket fd
276 | otherwise = c_close fd
278 foreign import "closesocket" unsafe
279 c_closesocket :: CInt -> IO CInt
282 foreign import "lseek" unsafe
283 c_lseek :: CInt -> COff -> CInt -> IO COff
285 foreign import "write" unsafe
286 c_write :: CInt -> Ptr CChar -> CSize -> IO CSsize
288 foreign import "read" unsafe
289 c_read :: CInt -> Ptr CChar -> CSize -> IO CSsize
291 #ifndef mingw32_TARGET_OS
292 foreign import "fcntl" unsafe
293 fcntl_read :: CInt -> CInt -> IO CInt
295 foreign import "fcntl" unsafe
296 fcntl_write :: CInt -> CInt -> CInt -> IO CInt
298 foreign import "fork" unsafe
301 foreign import "sigemptyset_PrelPosix_wrap" unsafe
302 c_sigemptyset :: Ptr CSigset -> IO ()
303 #def inline void sigemptyset_PrelPosix_wrap(sigset_t *set) { sigemptyset(set); }
305 foreign import "sigaddset" unsafe
306 c_sigaddset :: Ptr CSigset -> CInt -> IO ()
308 foreign import "sigprocmask" unsafe
309 c_sigprocmask :: CInt -> Ptr CSigset -> Ptr CSigset -> IO ()
311 foreign import "tcgetattr" unsafe
312 c_tcgetattr :: CInt -> Ptr Termios -> IO CInt
314 foreign import "tcsetattr" unsafe
315 c_tcsetattr :: CInt -> CInt -> Ptr Termios -> IO CInt
317 foreign import "unlink" unsafe
318 c_unlink :: CString -> IO CInt
320 foreign import "waitpid" unsafe
321 c_waitpid :: CPid -> Ptr CInt -> CInt -> IO CPid