1 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
5 -- Module : System.Posix.Internals
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.
14 -- This library is built on *every* platform, including Win32.
16 -- Non-posix compliant in order to support the following features:
17 -- * S_ISSOCK (no sockets in POSIX)
19 -----------------------------------------------------------------------------
22 module System.Posix.Internals where
27 import System.Posix.Types
35 #ifdef __GLASGOW_HASKELL__
45 import Hugs.Prelude (IOException(..), IOErrorType(..))
47 {-# CBITS PrelIOUtils.c dirUtils.c #-}
50 -- ---------------------------------------------------------------------------
68 #ifndef __GLASGOW_HASKELL__
72 -- ---------------------------------------------------------------------------
73 -- stat()-related stuff
75 fdFileSize :: Int -> IO Integer
77 allocaBytes sizeof_stat $ \ p_stat -> do
78 throwErrnoIfMinus1Retry "fileSize" $
79 c_fstat (fromIntegral fd) p_stat
80 c_mode <- st_mode p_stat :: IO CMode
81 if not (s_isreg c_mode)
84 c_size <- st_size p_stat :: IO COff
85 return (fromIntegral c_size)
87 data FDType = Directory | Stream | RegularFile
90 fileType :: FilePath -> IO FDType
92 allocaBytes sizeof_stat $ \ p_stat -> do
93 withCString file $ \p_file -> do
94 throwErrnoIfMinus1Retry "fileType" $
98 -- NOTE: On Win32 platforms, this will only work with file descriptors
99 -- referring to file handles. i.e., it'll fail for socket FDs.
100 fdType :: Int -> IO FDType
102 allocaBytes sizeof_stat $ \ p_stat -> do
103 throwErrnoIfMinus1Retry "fdType" $
104 c_fstat (fromIntegral fd) p_stat
107 statGetType p_stat = do
108 c_mode <- st_mode p_stat :: IO CMode
110 _ | s_isdir c_mode -> return Directory
111 | s_isfifo c_mode || s_issock c_mode || s_ischr c_mode
113 | s_isreg c_mode -> return RegularFile
114 | otherwise -> ioError 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 stdcall unsafe "HsBase.h closesocket"
139 c_closesocket :: CInt -> IO CInt
142 fdGetMode :: Int -> IO IOMode
144 #ifdef mingw32_TARGET_OS
145 flags1 <- throwErrnoIfMinus1Retry "fdGetMode"
146 (c__setmode (fromIntegral fd) (fromIntegral o_WRONLY))
147 flags <- throwErrnoIfMinus1Retry "fdGetMode"
148 (c__setmode (fromIntegral fd) (fromIntegral flags1))
150 flags <- throwErrnoIfMinus1Retry "fdGetMode"
151 (c_fcntl_read (fromIntegral fd) const_f_getfl)
154 wH = (flags .&. o_WRONLY) /= 0
155 aH = (flags .&. o_APPEND) /= 0
156 rwH = (flags .&. o_RDWR) /= 0
159 | wH && aH = AppendMode
161 | rwH = ReadWriteMode
162 | otherwise = ReadMode
166 -- ---------------------------------------------------------------------------
167 -- Terminal-related stuff
169 fdIsTTY :: Int -> IO Bool
170 fdIsTTY fd = c_isatty (fromIntegral fd) >>= return.toBool
172 #ifndef mingw32_TARGET_OS
174 setEcho :: Int -> Bool -> IO ()
176 tcSetAttr fd $ \ p_tios -> do
177 c_lflag <- c_lflag p_tios :: IO CTcflag
179 | on = c_lflag .|. fromIntegral const_echo
180 | otherwise = c_lflag .&. complement (fromIntegral const_echo)
181 poke_c_lflag p_tios (new_c_lflag :: CTcflag)
183 getEcho :: Int -> IO Bool
185 tcSetAttr fd $ \ p_tios -> do
186 c_lflag <- c_lflag p_tios :: IO CTcflag
187 return ((c_lflag .&. fromIntegral const_echo) /= 0)
189 setCooked :: Int -> Bool -> IO ()
190 setCooked fd cooked =
191 tcSetAttr fd $ \ p_tios -> do
193 -- turn on/off ICANON
194 c_lflag <- c_lflag p_tios :: IO CTcflag
195 let new_c_lflag | cooked = c_lflag .|. (fromIntegral const_icanon)
196 | otherwise = c_lflag .&. complement (fromIntegral const_icanon)
197 poke_c_lflag p_tios (new_c_lflag :: CTcflag)
199 -- set VMIN & VTIME to 1/0 respectively
200 when (not cooked) $ do
201 c_cc <- ptr_c_cc p_tios
202 let vmin = (c_cc `plusPtr` (fromIntegral const_vmin)) :: Ptr Word8
203 vtime = (c_cc `plusPtr` (fromIntegral const_vtime)) :: Ptr Word8
207 tcSetAttr :: FD -> (Ptr CTermios -> IO a) -> IO a
208 tcSetAttr fd fun = do
209 allocaBytes sizeof_termios $ \p_tios -> do
210 throwErrnoIfMinus1Retry "tcSetAttr"
211 (c_tcgetattr (fromIntegral fd) p_tios)
213 -- Save a copy of termios, if this is a standard file descriptor.
214 -- These terminal settings are restored in hs_exit().
216 p <- get_saved_termios fd
217 when (p == nullPtr) $ do
218 saved_tios <- mallocBytes sizeof_termios
219 copyBytes saved_tios p_tios sizeof_termios
220 set_saved_termios fd saved_tios
222 -- tcsetattr() when invoked by a background process causes the process
223 -- to be sent SIGTTOU regardless of whether the process has TOSTOP set
224 -- in its terminal flags (try it...). This function provides a
225 -- wrapper which temporarily blocks SIGTTOU around the call, making it
227 allocaBytes sizeof_sigset_t $ \ p_sigset -> do
228 allocaBytes sizeof_sigset_t $ \ p_old_sigset -> do
229 c_sigemptyset p_sigset
230 c_sigaddset p_sigset const_sigttou
231 c_sigprocmask const_sig_block p_sigset p_old_sigset
232 r <- fun p_tios -- do the business
233 throwErrnoIfMinus1Retry_ "tcSetAttr" $
234 c_tcsetattr (fromIntegral fd) const_tcsanow p_tios
235 c_sigprocmask const_sig_setmask p_old_sigset nullPtr
238 foreign import ccall unsafe "HsBase.h __hscore_get_saved_termios"
239 get_saved_termios :: Int -> IO (Ptr CTermios)
241 foreign import ccall unsafe "HsBase.h __hscore_set_saved_termios"
242 set_saved_termios :: Int -> (Ptr CTermios) -> IO ()
246 -- 'raw' mode for Win32 means turn off 'line input' (=> buffering and
247 -- character translation for the console.) The Win32 API for doing
248 -- this is GetConsoleMode(), which also requires echoing to be disabled
249 -- when turning off 'line input' processing. Notice that turning off
250 -- 'line input' implies enter/return is reported as '\r' (and it won't
251 -- report that character until another character is input..odd.) This
252 -- latter feature doesn't sit too well with IO actions like IO.hGetLine..
253 -- consider yourself warned.
254 setCooked :: Int -> Bool -> IO ()
255 setCooked fd cooked = do
256 x <- set_console_buffering (fromIntegral fd) (if cooked then 1 else 0)
258 then ioException (ioe_unk_error "setCooked" "failed to set buffering")
261 ioe_unk_error loc msg
262 = IOError Nothing OtherError loc msg Nothing
264 -- Note: echoing goes hand in hand with enabling 'line input' / raw-ness
265 -- for Win32 consoles, hence setEcho ends up being the inverse of setCooked.
266 setEcho :: Int -> Bool -> IO ()
268 x <- set_console_echo (fromIntegral fd) (if on then 1 else 0)
270 then ioException (ioe_unk_error "setEcho" "failed to set echoing")
273 getEcho :: Int -> IO Bool
275 r <- get_console_echo (fromIntegral fd)
277 then ioException (ioe_unk_error "getEcho" "failed to get echoing")
280 foreign import ccall unsafe "consUtils.h set_console_buffering__"
281 set_console_buffering :: CInt -> CInt -> IO CInt
283 foreign import ccall unsafe "consUtils.h set_console_echo__"
284 set_console_echo :: CInt -> CInt -> IO CInt
286 foreign import ccall unsafe "consUtils.h get_console_echo__"
287 get_console_echo :: CInt -> IO CInt
291 -- ---------------------------------------------------------------------------
292 -- Turning on non-blocking for a file descriptor
294 #ifndef mingw32_TARGET_OS
296 setNonBlockingFD fd = do
297 flags <- throwErrnoIfMinus1Retry "setNonBlockingFD"
298 (c_fcntl_read (fromIntegral fd) const_f_getfl)
299 -- An error when setting O_NONBLOCK isn't fatal: on some systems
300 -- there are certain file handles on which this will fail (eg. /dev/null
301 -- on FreeBSD) so we throw away the return code from fcntl_write.
302 c_fcntl_write (fromIntegral fd) const_f_setfl (flags .|. o_NONBLOCK)
305 -- bogus defns for win32
306 setNonBlockingFD fd = return ()
310 -- -----------------------------------------------------------------------------
313 foreign import ccall unsafe "HsBase.h access"
314 c_access :: CString -> CMode -> IO CInt
316 foreign import ccall unsafe "HsBase.h chmod"
317 c_chmod :: CString -> CMode -> IO CInt
319 foreign import ccall unsafe "HsBase.h chdir"
320 c_chdir :: CString -> IO CInt
322 foreign import ccall unsafe "HsBase.h close"
323 c_close :: CInt -> IO CInt
325 foreign import ccall unsafe "HsBase.h closedir"
326 c_closedir :: Ptr CDir -> IO CInt
328 foreign import ccall unsafe "HsBase.h creat"
329 c_creat :: CString -> CMode -> IO CInt
331 foreign import ccall unsafe "HsBase.h dup"
332 c_dup :: CInt -> IO CInt
334 foreign import ccall unsafe "HsBase.h dup2"
335 c_dup2 :: CInt -> CInt -> IO CInt
337 foreign import ccall unsafe "HsBase.h fstat"
338 c_fstat :: CInt -> Ptr CStat -> IO CInt
340 foreign import ccall unsafe "HsBase.h getcwd"
341 c_getcwd :: Ptr CChar -> CInt -> IO (Ptr CChar)
343 foreign import ccall unsafe "HsBase.h isatty"
344 c_isatty :: CInt -> IO CInt
346 foreign import ccall unsafe "HsBase.h lseek"
347 c_lseek :: CInt -> COff -> CInt -> IO COff
349 foreign import ccall unsafe "HsBase.h __hscore_lstat"
350 lstat :: CString -> Ptr CStat -> IO CInt
352 foreign import ccall unsafe "HsBase.h open"
353 c_open :: CString -> CInt -> CMode -> IO CInt
355 foreign import ccall unsafe "HsBase.h opendir"
356 c_opendir :: CString -> IO (Ptr CDir)
358 foreign import ccall unsafe "HsBase.h __hscore_mkdir"
359 mkdir :: CString -> CInt -> IO CInt
361 foreign import ccall unsafe "HsBase.h read"
362 c_read :: CInt -> Ptr CChar -> CSize -> IO CSsize
364 foreign import ccall unsafe "HsBase.h readdir"
365 c_readdir :: Ptr CDir -> IO (Ptr CDirent)
367 foreign import ccall unsafe "dirUtils.h __hscore_renameFile"
368 c_rename :: CString -> CString -> IO CInt
370 foreign import ccall unsafe "HsBase.h rewinddir"
371 c_rewinddir :: Ptr CDir -> IO ()
373 foreign import ccall unsafe "HsBase.h rmdir"
374 c_rmdir :: CString -> IO CInt
376 foreign import ccall unsafe "HsBase.h stat"
377 c_stat :: CString -> Ptr CStat -> IO CInt
379 foreign import ccall unsafe "HsBase.h umask"
380 c_umask :: CMode -> IO CMode
382 foreign import ccall unsafe "HsBase.h write"
383 c_write :: CInt -> Ptr CChar -> CSize -> IO CSsize
385 foreign import ccall unsafe "HsBase.h unlink"
386 c_unlink :: CString -> IO CInt
388 #ifndef mingw32_TARGET_OS
389 foreign import ccall unsafe "HsBase.h fcntl"
390 c_fcntl_read :: CInt -> CInt -> IO CInt
392 foreign import ccall unsafe "HsBase.h fcntl"
393 c_fcntl_write :: CInt -> CInt -> CInt -> IO CInt
395 foreign import ccall unsafe "HsBase.h fcntl"
396 c_fcntl_lock :: CInt -> CInt -> Ptr CFLock -> IO CInt
398 foreign import ccall unsafe "HsBase.h fork"
401 foreign import ccall unsafe "HsBase.h getpid"
404 foreign import ccall unsafe "HsBase.h link"
405 c_link :: CString -> CString -> IO CInt
407 foreign import ccall unsafe "HsBase.h mkfifo"
408 c_mkfifo :: CString -> CMode -> IO CInt
410 foreign import ccall unsafe "HsBase.h pipe"
411 c_pipe :: Ptr CInt -> IO CInt
413 foreign import ccall unsafe "HsBase.h __hscore_sigemptyset"
414 c_sigemptyset :: Ptr CSigset -> IO CInt
416 foreign import ccall unsafe "HsBase.h __hscore_sigaddset"
417 c_sigaddset :: Ptr CSigset -> CInt -> IO CInt
419 foreign import ccall unsafe "HsBase.h sigprocmask"
420 c_sigprocmask :: CInt -> Ptr CSigset -> Ptr CSigset -> IO CInt
422 foreign import ccall unsafe "HsBase.h tcgetattr"
423 c_tcgetattr :: CInt -> Ptr CTermios -> IO CInt
425 foreign import ccall unsafe "HsBase.h tcsetattr"
426 c_tcsetattr :: CInt -> CInt -> Ptr CTermios -> IO CInt
428 foreign import ccall unsafe "HsBase.h utime"
429 c_utime :: CString -> Ptr CUtimbuf -> IO CMode
431 foreign import ccall unsafe "HsBase.h waitpid"
432 c_waitpid :: CPid -> Ptr CInt -> CInt -> IO CPid
434 foreign import ccall unsafe "HsBase.h _setmode"
435 c__setmode :: CInt -> CInt -> IO CInt
437 -- /* Set "stdin" to have binary mode: */
438 -- result = _setmode( _fileno( stdin ), _O_BINARY );
439 -- if( result == -1 )
440 -- perror( "Cannot set mode" );
442 -- printf( "'stdin' successfully changed to binary mode\n" );
445 -- traversing directories
446 foreign import ccall unsafe "dirUtils.h __hscore_readdir"
447 readdir :: Ptr CDir -> Ptr (Ptr CDirent) -> IO CInt
449 foreign import ccall unsafe "HsBase.h __hscore_free_dirent"
450 freeDirEnt :: Ptr CDirent -> IO ()
452 foreign import ccall unsafe "HsBase.h __hscore_end_of_dir"
455 foreign import ccall unsafe "HsBase.h __hscore_d_name"
456 d_name :: Ptr CDirent -> IO CString
459 foreign import ccall unsafe "HsBase.h __hscore_o_rdonly" o_RDONLY :: CInt
460 foreign import ccall unsafe "HsBase.h __hscore_o_wronly" o_WRONLY :: CInt
461 foreign import ccall unsafe "HsBase.h __hscore_o_rdwr" o_RDWR :: CInt
462 foreign import ccall unsafe "HsBase.h __hscore_o_append" o_APPEND :: CInt
463 foreign import ccall unsafe "HsBase.h __hscore_o_creat" o_CREAT :: CInt
464 foreign import ccall unsafe "HsBase.h __hscore_o_excl" o_EXCL :: CInt
465 foreign import ccall unsafe "HsBase.h __hscore_o_trunc" o_TRUNC :: CInt
468 foreign import ccall unsafe "HsBase.h __hscore_o_noctty" o_NOCTTY :: CInt
469 foreign import ccall unsafe "HsBase.h __hscore_o_nonblock" o_NONBLOCK :: CInt
470 foreign import ccall unsafe "HsBase.h __hscore_o_binary" o_BINARY :: CInt
472 foreign import ccall unsafe "HsBase.h __hscore_s_isreg" s_isreg :: CMode -> Bool
473 foreign import ccall unsafe "HsBase.h __hscore_s_ischr" s_ischr :: CMode -> Bool
474 foreign import ccall unsafe "HsBase.h __hscore_s_isblk" s_isblk :: CMode -> Bool
475 foreign import ccall unsafe "HsBase.h __hscore_s_isdir" s_isdir :: CMode -> Bool
476 foreign import ccall unsafe "HsBase.h __hscore_s_isfifo" s_isfifo :: CMode -> Bool
478 foreign import ccall unsafe "HsBase.h __hscore_sizeof_stat" sizeof_stat :: Int
479 foreign import ccall unsafe "HsBase.h __hscore_st_mtime" st_mtime :: Ptr CStat -> IO CTime
480 foreign import ccall unsafe "HsBase.h __hscore_st_size" st_size :: Ptr CStat -> IO COff
481 foreign import ccall unsafe "HsBase.h __hscore_st_mode" st_mode :: Ptr CStat -> IO CMode
483 foreign import ccall unsafe "HsBase.h __hscore_echo" const_echo :: CInt
484 foreign import ccall unsafe "HsBase.h __hscore_tcsanow" const_tcsanow :: CInt
485 foreign import ccall unsafe "HsBase.h __hscore_icanon" const_icanon :: CInt
486 foreign import ccall unsafe "HsBase.h __hscore_vmin" const_vmin :: CInt
487 foreign import ccall unsafe "HsBase.h __hscore_vtime" const_vtime :: CInt
488 foreign import ccall unsafe "HsBase.h __hscore_sigttou" const_sigttou :: CInt
489 foreign import ccall unsafe "HsBase.h __hscore_sig_block" const_sig_block :: CInt
490 foreign import ccall unsafe "HsBase.h __hscore_sig_setmask" const_sig_setmask :: CInt
491 foreign import ccall unsafe "HsBase.h __hscore_f_getfl" const_f_getfl :: CInt
492 foreign import ccall unsafe "HsBase.h __hscore_f_setfl" const_f_setfl :: CInt
494 #ifndef mingw32_TARGET_OS
495 foreign import ccall unsafe "HsBase.h __hscore_sizeof_termios" sizeof_termios :: Int
496 foreign import ccall unsafe "HsBase.h __hscore_sizeof_sigset_t" sizeof_sigset_t :: Int
498 foreign import ccall unsafe "HsBase.h __hscore_lflag" c_lflag :: Ptr CTermios -> IO CTcflag
499 foreign import ccall unsafe "HsBase.h __hscore_poke_lflag" poke_c_lflag :: Ptr CTermios -> CTcflag -> IO ()
500 foreign import ccall unsafe "HsBase.h __hscore_ptr_c_cc" ptr_c_cc :: Ptr CTermios -> IO (Ptr Word8)
503 #ifndef mingw32_TARGET_OS
504 foreign import ccall unsafe "HsBase.h __hscore_s_issock" s_issock :: CMode -> Bool
506 s_issock :: CMode -> Bool
507 s_issock cmode = False