X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=GHC%2FHandle.hs;h=7c252c5ddc9fd3eb83eb54eb49d627378a0d334b;hb=4c98224cdf6e5a1620721faea837656f429f4f27;hp=c29c6c9b41b3ad68f698f0c3239c86d8dc273dc4;hpb=7ff72f2628d225b08f7efdd21cb43514181b9bb7;p=ghc-base.git diff --git a/GHC/Handle.hs b/GHC/Handle.hs index c29c6c9..7c252c5 100644 --- a/GHC/Handle.hs +++ b/GHC/Handle.hs @@ -3,12 +3,19 @@ #undef DEBUG_DUMP #undef DEBUG --- ----------------------------------------------------------------------------- --- $Id: Handle.hs,v 1.6 2002/03/26 17:06:32 simonmar Exp $ +----------------------------------------------------------------------------- +-- | +-- Module : GHC.Handle +-- Copyright : (c) The University of Glasgow, 1994-2001 +-- License : see libraries/base/LICENSE +-- +-- Maintainer : libraries@haskell.org +-- Stability : internal +-- Portability : non-portable -- --- (c) The University of Glasgow, 1994-2001 +-- This module defines the basic operations on I\/O \"handles\". -- --- This module defines the basic operations on I/O "handles". +----------------------------------------------------------------------------- module GHC.Handle ( withHandle, withHandle', withHandle_, @@ -17,7 +24,7 @@ module GHC.Handle ( newEmptyBuffer, allocateBuffer, readCharFromBuffer, writeCharIntoBuffer, flushWriteBufferOnly, flushWriteBuffer, flushReadBuffer, fillReadBuffer, read_off, read_off_ba, - write_off, write_off_ba, + write_off, write_off_ba, unlockFile, ioe_closedHandle, ioe_EOF, ioe_notReadable, ioe_notWritable, @@ -299,16 +306,23 @@ stdHandleFinalizer m = do handleFinalizer :: MVar Handle__ -> IO () handleFinalizer m = do h_ <- takeMVar m - flushWriteBufferOnly h_ - let fd = fromIntegral (haFD h_) - unlockFile fd - when (fd /= -1) + let + -- hClose puts both the fd and the handle's type + -- into a closed state, so it's a bit excessive + -- to test for both here, but caution sometimes + -- pays off.. + alreadyClosed = + case haType h_ of { ClosedHandle{} -> True; _ -> False } + fd = fromIntegral (haFD h_) + + when (not alreadyClosed && fd /= -1) $ do + flushWriteBufferOnly h_ + unlockFile fd #ifdef mingw32_TARGET_OS (closeFd (haIsStream h_) fd >> return ()) #else (c_close fd >> return ()) #endif - return () -- --------------------------------------------------------------------------- -- Grimy buffer operations @@ -1181,6 +1195,14 @@ hIsTerminalDevice handle = do -- ----------------------------------------------------------------------------- -- hSetBinaryMode +-- | On Windows, reading a file in text mode (which is the default) will +-- translate CRLF to LF, and writing will translate LF to CRLF. This +-- is usually what you want with text files. With binary files this is +-- undesirable; also, as usual under Microsoft operating systems, text +-- mode treats control-Z as EOF. Setting binary mode using +-- 'hSetBinaryMode' turns off all special treatment of end-of-line and +-- end-of-file characters. +-- hSetBinaryMode :: Handle -> Bool -> IO () hSetBinaryMode handle bin = withAllHandles__ "hSetBinaryMode" handle $ \ handle_ ->