1 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The University of Glasgow, 1992-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable
13 -- POSIX support layer for the standard libraries
15 -- Non-posix compliant in order to support the following features:
16 -- * S_ISSOCK (no sockets in POSIX)
18 -----------------------------------------------------------------------------
20 module GHC.Posix where
37 -- ---------------------------------------------------------------------------
55 type CDev = HTYPE_DEV_T
56 type CIno = HTYPE_INO_T
57 type CMode = HTYPE_MODE_T
58 type COff = HTYPE_OFF_T
59 type CPid = HTYPE_PID_T
61 #ifdef mingw32_TARGET_OS
62 type CSsize = HTYPE_SIZE_T
64 type CGid = HTYPE_GID_T
65 type CNlink = HTYPE_NLINK_T
66 type CSsize = HTYPE_SSIZE_T
67 type CUid = HTYPE_UID_T
69 type CSpeed = HTYPE_SPEED_T
70 type CTcflag = HTYPE_TCFLAG_T
73 -- ---------------------------------------------------------------------------
74 -- stat()-related stuff
76 fdFileSize :: Int -> IO Integer
78 allocaBytes sizeof_stat $ \ p_stat -> do
79 throwErrnoIfMinus1Retry "fileSize" $
80 c_fstat (fromIntegral fd) p_stat
81 c_mode <- st_mode p_stat :: IO CMode
82 if not (s_isreg c_mode)
85 c_size <- st_size p_stat :: IO COff
86 return (fromIntegral c_size)
88 data FDType = Directory | Stream | RegularFile
91 fileType :: FilePath -> IO FDType
93 allocaBytes sizeof_stat $ \ p_stat -> do
94 withCString file $ \p_file -> do
95 throwErrnoIfMinus1Retry "fileType" $
99 -- NOTE: On Win32 platforms, this will only work with file descriptors
100 -- referring to file handles. i.e., it'll fail for socket FDs.
101 fdType :: Int -> IO FDType
103 allocaBytes sizeof_stat $ \ p_stat -> do
104 throwErrnoIfMinus1Retry "fdType" $
105 c_fstat (fromIntegral fd) p_stat
108 statGetType p_stat = do
109 c_mode <- st_mode p_stat :: IO CMode
111 _ | s_isdir c_mode -> return Directory
112 | s_isfifo c_mode || s_issock c_mode -> return Stream
113 | s_isreg c_mode -> return RegularFile
114 | otherwise -> ioException ioe_unknownfiletype
117 ioe_unknownfiletype = IOError Nothing UnsupportedOperation "fdType"
118 "unknown file type" Nothing
120 -- It isn't clear whether ftruncate is POSIX or not (I've read several
121 -- manpages and they seem to conflict), so we truncate using open/2.
122 fileTruncate :: FilePath -> IO ()
123 fileTruncate file = do
124 let flags = o_WRONLY .|. o_TRUNC
125 withCString file $ \file_cstr -> do
126 fd <- fromIntegral `liftM`
127 throwErrnoIfMinus1Retry "fileTruncate"
128 (c_open file_cstr (fromIntegral flags) 0o666)
132 #ifdef mingw32_TARGET_OS
133 closeFd :: Bool -> CInt -> IO CInt
135 | isStream = c_closesocket fd
136 | otherwise = c_close fd
138 foreign import ccall unsafe "closesocket"
139 c_closesocket :: CInt -> IO CInt
142 -- ---------------------------------------------------------------------------
143 -- Terminal-related stuff
145 fdIsTTY :: Int -> IO Bool
146 fdIsTTY fd = c_isatty (fromIntegral fd) >>= return.toBool
148 #ifndef mingw32_TARGET_OS
150 setEcho :: Int -> Bool -> IO ()
152 allocaBytes sizeof_termios $ \p_tios -> do
153 throwErrnoIfMinus1Retry "setEcho"
154 (c_tcgetattr (fromIntegral fd) p_tios)
155 c_lflag <- c_lflag p_tios :: IO CTcflag
157 | on = c_lflag .|. fromIntegral const_echo
158 | otherwise = c_lflag .&. complement (fromIntegral const_echo)
159 poke_c_lflag p_tios (new_c_lflag :: CTcflag)
160 tcSetAttr fd const_tcsanow p_tios
162 getEcho :: Int -> IO Bool
164 allocaBytes sizeof_termios $ \p_tios -> do
165 throwErrnoIfMinus1Retry "setEcho"
166 (c_tcgetattr (fromIntegral fd) p_tios)
167 c_lflag <- c_lflag p_tios :: IO CTcflag
168 return ((c_lflag .&. fromIntegral const_echo) /= 0)
170 setCooked :: Int -> Bool -> IO ()
171 setCooked fd cooked =
172 allocaBytes sizeof_termios $ \p_tios -> do
173 throwErrnoIfMinus1Retry "setCooked"
174 (c_tcgetattr (fromIntegral fd) p_tios)
176 -- turn on/off ICANON
177 c_lflag <- c_lflag p_tios :: IO CTcflag
178 let new_c_lflag | cooked = c_lflag .|. (fromIntegral const_icanon)
179 | otherwise = c_lflag .&. complement (fromIntegral const_icanon)
180 poke_c_lflag p_tios (new_c_lflag :: CTcflag)
182 -- set VMIN & VTIME to 1/0 respectively
184 c_cc <- ptr_c_cc p_tios
185 let vmin = (c_cc `plusPtr` (fromIntegral const_vmin)) :: Ptr Word8
186 vtime = (c_cc `plusPtr` (fromIntegral const_vtime)) :: Ptr Word8
190 tcSetAttr fd const_tcsanow p_tios
192 -- tcsetattr() when invoked by a background process causes the process
193 -- to be sent SIGTTOU regardless of whether the process has TOSTOP set
194 -- in its terminal flags (try it...). This function provides a
195 -- wrapper which temporarily blocks SIGTTOU around the call, making it
198 tcSetAttr :: FD -> CInt -> Ptr CTermios -> IO ()
199 tcSetAttr fd options p_tios = do
200 allocaBytes sizeof_sigset_t $ \ p_sigset -> do
201 allocaBytes sizeof_sigset_t $ \ p_old_sigset -> do
202 c_sigemptyset p_sigset
203 c_sigaddset p_sigset const_sigttou
204 c_sigprocmask const_sig_block p_sigset p_old_sigset
205 throwErrnoIfMinus1Retry_ "tcSetAttr" $
206 c_tcsetattr (fromIntegral fd) options p_tios
207 c_sigprocmask const_sig_setmask p_old_sigset nullPtr
211 -- bogus defns for win32
212 setCooked :: Int -> Bool -> IO ()
213 setCooked fd cooked = return ()
215 setEcho :: Int -> Bool -> IO ()
216 setEcho fd on = return ()
218 getEcho :: Int -> IO Bool
219 getEcho fd = return False
223 -- ---------------------------------------------------------------------------
224 -- Turning on non-blocking for a file descriptor
226 #ifndef mingw32_TARGET_OS
228 setNonBlockingFD fd = do
229 flags <- throwErrnoIfMinus1Retry "setNonBlockingFD"
230 (c_fcntl_read (fromIntegral fd) const_f_getfl)
231 -- An error when setting O_NONBLOCK isn't fatal: on some systems
232 -- there are certain file handles on which this will fail (eg. /dev/null
233 -- on FreeBSD) so we throw away the return code from fcntl_write.
234 c_fcntl_write (fromIntegral fd) const_f_setfl (flags .|. o_NONBLOCK)
237 -- bogus defns for win32
238 setNonBlockingFD fd = return ()
242 -- -----------------------------------------------------------------------------
245 foreign import ccall unsafe "access"
246 c_access :: CString -> CMode -> IO CInt
248 foreign import ccall unsafe "chmod"
249 c_chmod :: CString -> CMode -> IO CInt
251 foreign import ccall unsafe "chdir"
252 c_chdir :: CString -> IO CInt
254 foreign import ccall unsafe "close"
255 c_close :: CInt -> IO CInt
257 foreign import ccall unsafe "closedir"
258 c_closedir :: Ptr CDir -> IO CInt
260 foreign import ccall unsafe "creat"
261 c_creat :: CString -> CMode -> IO CInt
263 foreign import ccall unsafe "dup"
264 c_dup :: CInt -> IO CInt
266 foreign import ccall unsafe "dup2"
267 c_dup2 :: CInt -> CInt -> IO CInt
269 foreign import ccall unsafe "fstat"
270 c_fstat :: CInt -> Ptr CStat -> IO CInt
272 foreign import ccall unsafe "getcwd"
273 c_getcwd :: Ptr CChar -> CInt -> IO (Ptr CChar)
275 foreign import ccall unsafe "isatty"
276 c_isatty :: CInt -> IO CInt
278 foreign import ccall unsafe "lseek"
279 c_lseek :: CInt -> COff -> CInt -> IO COff
281 foreign import ccall unsafe "__hscore_lstat"
282 lstat :: CString -> Ptr CStat -> IO CInt
284 foreign import ccall unsafe "open"
285 c_open :: CString -> CInt -> CMode -> IO CInt
287 foreign import ccall unsafe "opendir"
288 c_opendir :: CString -> IO (Ptr CDir)
290 foreign import ccall unsafe "__hscore_mkdir"
291 mkdir :: CString -> CInt -> IO CInt
293 foreign import ccall unsafe "read"
294 c_read :: CInt -> Ptr CChar -> CSize -> IO CSsize
296 foreign import ccall unsafe "readdir"
297 c_readdir :: Ptr CDir -> IO (Ptr CDirent)
299 foreign import ccall unsafe "rename"
300 c_rename :: CString -> CString -> IO CInt
302 foreign import ccall unsafe "rewinddir"
303 c_rewinddir :: Ptr CDir -> IO ()
305 foreign import ccall unsafe "rmdir"
306 c_rmdir :: CString -> IO CInt
308 foreign import ccall unsafe "stat"
309 c_stat :: CString -> Ptr CStat -> IO CInt
311 foreign import ccall unsafe "umask"
312 c_umask :: CMode -> IO CMode
314 foreign import ccall unsafe "write"
315 c_write :: CInt -> Ptr CChar -> CSize -> IO CSsize
317 foreign import ccall unsafe "unlink"
318 c_unlink :: CString -> IO CInt
320 #ifndef mingw32_TARGET_OS
321 foreign import ccall unsafe "fcntl"
322 c_fcntl_read :: CInt -> CInt -> IO CInt
324 foreign import ccall unsafe "fcntl"
325 c_fcntl_write :: CInt -> CInt -> CInt -> IO CInt
327 foreign import ccall unsafe "fcntl"
328 c_fcntl_lock :: CInt -> CInt -> Ptr CFLock -> IO CInt
330 foreign import ccall unsafe "fork"
333 foreign import ccall unsafe "fpathconf"
334 c_fpathconf :: CInt -> CInt -> IO CLong
336 foreign import ccall unsafe "__hscore_sigemptyset"
337 c_sigemptyset :: Ptr CSigset -> IO ()
339 foreign import ccall unsafe "link"
340 c_link :: CString -> CString -> IO CInt
342 foreign import ccall unsafe "mkfifo"
343 c_mkfifo :: CString -> CMode -> IO CInt
345 foreign import ccall unsafe "pathconf"
346 c_pathconf :: CString -> CInt -> IO CLong
348 foreign import ccall unsafe "pipe"
349 c_pipe :: Ptr CInt -> IO CInt
351 foreign import ccall unsafe "__hscore_sigaddset"
352 c_sigaddset :: Ptr CSigset -> CInt -> IO CInt
354 foreign import ccall unsafe "sigprocmask"
355 c_sigprocmask :: CInt -> Ptr CSigset -> Ptr CSigset -> IO ()
357 foreign import ccall unsafe "tcgetattr"
358 c_tcgetattr :: CInt -> Ptr CTermios -> IO CInt
360 foreign import ccall unsafe "tcsetattr"
361 c_tcsetattr :: CInt -> CInt -> Ptr CTermios -> IO CInt
363 foreign import ccall unsafe "uname"
364 c_uname :: Ptr CUtsname -> IO CInt
366 foreign import ccall unsafe "utime"
367 c_utime :: CString -> Ptr CUtimbuf -> IO CMode
369 foreign import ccall unsafe "waitpid"
370 c_waitpid :: CPid -> Ptr CInt -> CInt -> IO CPid
374 foreign import ccall unsafe "__hscore_o_rdonly" o_RDONLY :: CInt
375 foreign import ccall unsafe "__hscore_o_wronly" o_WRONLY :: CInt
376 foreign import ccall unsafe "__hscore_o_rdwr" o_RDWR :: CInt
377 foreign import ccall unsafe "__hscore_o_append" o_APPEND :: CInt
378 foreign import ccall unsafe "__hscore_o_creat" o_CREAT :: CInt
379 foreign import ccall unsafe "__hscore_o_excl" o_EXCL :: CInt
380 foreign import ccall unsafe "__hscore_o_trunc" o_TRUNC :: CInt
383 foreign import ccall unsafe "__hscore_o_noctty" o_NOCTTY :: CInt
384 foreign import ccall unsafe "__hscore_o_nonblock" o_NONBLOCK :: CInt
385 foreign import ccall unsafe "__hscore_o_binary" o_BINARY :: CInt
387 foreign import ccall unsafe "__hscore_s_isreg" s_isreg :: CMode -> Bool
388 foreign import ccall unsafe "__hscore_s_ischr" s_ischr :: CMode -> Bool
389 foreign import ccall unsafe "__hscore_s_isblk" s_isblk :: CMode -> Bool
390 foreign import ccall unsafe "__hscore_s_isdir" s_isdir :: CMode -> Bool
391 foreign import ccall unsafe "__hscore_s_isfifo" s_isfifo :: CMode -> Bool
393 foreign import ccall unsafe "__hscore_sizeof_stat" sizeof_stat :: Int
394 foreign import ccall unsafe "__hscore_st_mtime" st_mtime :: Ptr CStat -> IO CTime
395 foreign import ccall unsafe "__hscore_st_size" st_size :: Ptr CStat -> IO COff
396 foreign import ccall unsafe "__hscore_st_mode" st_mode :: Ptr CStat -> IO CMode
398 foreign import ccall unsafe "__hscore_echo" const_echo :: CInt
399 foreign import ccall unsafe "__hscore_tcsanow" const_tcsanow :: CInt
400 foreign import ccall unsafe "__hscore_icanon" const_icanon :: CInt
401 foreign import ccall unsafe "__hscore_vmin" const_vmin :: CInt
402 foreign import ccall unsafe "__hscore_vtime" const_vtime :: CInt
403 foreign import ccall unsafe "__hscore_sigttou" const_sigttou :: CInt
404 foreign import ccall unsafe "__hscore_sig_block" const_sig_block :: CInt
405 foreign import ccall unsafe "__hscore_sig_setmask" const_sig_setmask :: CInt
406 foreign import ccall unsafe "__hscore_f_getfl" const_f_getfl :: CInt
407 foreign import ccall unsafe "__hscore_f_setfl" const_f_setfl :: CInt
409 #ifndef mingw32_TARGET_OS
410 foreign import ccall unsafe "__hscore_sizeof_termios" sizeof_termios :: Int
411 foreign import ccall unsafe "__hscore_sizeof_sigset_t" sizeof_sigset_t :: Int
413 foreign import ccall unsafe "__hscore_lflag" c_lflag :: Ptr CTermios -> IO CTcflag
414 foreign import ccall unsafe "__hscore_poke_lflag" poke_c_lflag :: Ptr CTermios -> CTcflag -> IO ()
415 foreign import ccall unsafe "__hscore_ptr_c_cc" ptr_c_cc :: Ptr CTermios -> IO (Ptr Word8)
418 #ifndef mingw32_TARGET_OS
419 foreign import ccall unsafe "__hscore_s_issock" s_issock :: CMode -> Bool
421 s_issock :: CMode -> Bool
422 s_issock cmode = False