2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: closeFile.c,v 1.9 2000/03/28 08:49:56 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 if ( flush_buf != 0 && (fo->flags & FILEOBJ_WRITE) ) {
36 writeFileObject(ptr,fo->bufWPtr);
39 /* If the flush failed, we ignore this and soldier on.. */
41 if ( unlockFile(fo->fd) ) {
42 /* If the file has already been unlocked (or an entry
43 for it in the locking tables couldn't be found), could
46 - we're repeating an hClose on an already
47 closed file (this is likely to be a bug
48 in the implementation of hClose, as this
49 condition should have been caught before
52 - the file wasn't locked in the first place!
53 (file descriptors to non regular files.)
55 We proceed with attempting to close the file,
56 but don't flag the error should close() return
63 /* Free the buffer straight away. We can't free the file object
64 * itself until the finalizer runs.
66 if ( fo->buf != NULL ) {
71 /* Closing file descriptors that refer to standard channels
72 is problematic, so we back off from doing this by default,
73 just closing them at the Handle level. If you insist on
74 closing them, setting the (global) variable
75 __really_close_stdfiles to 0 turns off this behaviour.
77 if ( (fo->flags & FILEOBJ_STD) && __really_close_stdfiles ) {
81 /* Regardless of success or otherwise, the fd field gets smashed. */
85 fo->flags & FILEOBJ_WINSOCK ?
87 close(fo->fd))) != 0 ) {
89 close(fo->fd))) != 0 ) {
91 /* See above unlockFile() comment */
92 if ( errno != EINTR && (!unlocked && errno != EBADF ) ) {