2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: closeFile.c,v 1.4 1999/05/05 10:33:14 sof Exp $
6 * hClose Runtime Support
16 StgInt __really_close_stdfiles=1;
19 closeFile(ptr,flush_buf)
23 IOFileObject* fo = (IOFileObject*)ptr;
27 /* Already closed, shouldn't occur. */
32 if ( flush_buf != 0 && (fo->flags & FILEOBJ_FLUSH) ) {
33 writeFileObject(ptr,fo->bufWPtr);
36 /* If the flush failed, we ignore this and soldier on.. */
38 if ( unlockFile(fo->fd) ) {
39 /* If the file has already been unlocked (or an entry
40 for it in the locking tables couldn't be found), could
43 - we're repeating an hClose on an already
44 closed file (this is likely to be a bug
45 in the implementation of hClose, as this
46 condition should have been caught before
49 - the file wasn't locked in the first place!
50 (file descriptors to non regular files.)
52 We proceed with attempting to close the file,
53 but don't flag the error should close() return
60 /* Closing file descriptors that refer to standard channels
61 is problematic, so we back off from doing this by default,
62 just closing them at the Handle level. If you insist on
63 closing them, setting the (global) variable
64 __really_close_stdfiles to 0 turns off this behaviour.
66 if ( (fo->flags & FILEOBJ_STD) && __really_close_stdfiles ) {
70 /* Regardless of success or otherwise, the fd field gets smashed. */
74 fo->flags & FILEOBJ_WINSOCK ?
76 close(fo->fd))) != 0 ) {
78 close(fo->fd))) != 0 ) {
80 /* See above unlockFile() comment */
81 if ( errno != EINTR && (!unlocked && errno != EBADF ) ) {