2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
4 \subsection[closeFile.lc]{hClose Runtime Support}
11 StgInt __really_close_stdfiles=1;
14 closeFile(ptr,flush_buf)
18 IOFileObject* fo = (IOFileObject*)ptr;
22 /* Already closed, shouldn't occur. */
27 if ( flush_buf != 0 && (fo->flags & FILEOBJ_FLUSH) ) {
28 writeFileObject(ptr,fo->bufWPtr);
31 /* If the flush failed, we ignore this and soldier on.. */
33 if ( unlockFile(fo->fd) ) {
34 /* If the file has already been unlocked (or an entry
35 for it in the locking tables couldn't be found), could
38 - we're repeating an hClose on an already
39 closed file (this is likely to be a bug
40 in the implementation of hClose, as this
41 condition should have been caught before
44 - the file wasn't locked in the first place!
45 (file descriptors to non regular files.)
47 We proceed with attempting to close the file,
48 but don't flag the error should close() return
55 /* Closing file descriptors that refer to standard channels
56 is problematic, so we back off from doing this by default,
57 just closing them at the Handle level. If you insist on
58 closing them, setting the (global) variable
59 __really_close_stdfiles to 0 turns off this behaviour.
61 if ( (fo->flags & FILEOBJ_STD) && __really_close_stdfiles ) {
65 /* Regardless of success or otherwise, the fd field gets smashed. */
66 while ( (rc = close(fo->fd)) != 0 ) {
67 /* See above unlockFile() comment */
68 if ( errno != EINTR && (!unlocked && errno != EBADF ) ) {