2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: closeFile.c,v 1.10 2000/09/25 10:48:50 simonmar Exp $
6 * hClose Runtime Support
13 #if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
21 StgInt __really_close_stdfiles=1;
24 closeFile(StgForeignPtr ptr, StgInt flush_buf)
26 IOFileObject* fo = (IOFileObject*)ptr;
30 /* Already closed, shouldn't occur. */
35 /* Flush buffer if we have unwritten data */
36 if ( flush_buf != 0 ) {
40 /* If the flush failed, we ignore this and soldier on.. */
42 if ( unlockFile(fo->fd) ) {
43 /* If the file has already been unlocked (or an entry
44 for it in the locking tables couldn't be found), could
47 - we're repeating an hClose on an already
48 closed file (this is likely to be a bug
49 in the implementation of hClose, as this
50 condition should have been caught before
53 - the file wasn't locked in the first place!
54 (file descriptors to non regular files.)
56 We proceed with attempting to close the file,
57 but don't flag the error should close() return
64 /* Free the buffer straight away. We can't free the file object
65 * itself until the finalizer runs.
67 if ( fo->buf != NULL ) {
72 /* Closing file descriptors that refer to standard channels
73 is problematic, so we back off from doing this by default,
74 just closing them at the Handle level. If you insist on
75 closing them, setting the (global) variable
76 __really_close_stdfiles to 0 turns off this behaviour.
78 if ( (fo->flags & FILEOBJ_STD) && __really_close_stdfiles ) {
82 /* Regardless of success or otherwise, the fd field gets smashed. */
86 fo->flags & FILEOBJ_WINSOCK ?
88 close(fo->fd))) != 0 ) {
90 close(fo->fd))) != 0 ) {
92 /* See above unlockFile() comment */
93 if ( errno != EINTR && (!unlocked && errno != EBADF ) ) {