4fac27b3cc601f7952f42d127dfd09ce070ff7e4
[ghc-hetmet.git] / ghc / lib / std / cbits / closeFile.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
3 %
4 \subsection[closeFile.lc]{hClose Runtime Support}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 StgInt
12 closeFile(fp)
13 StgForeignObj fp;
14 {
15     int rc;
16     int unlocked=1;
17
18     if ( unlockFile(fileno((FILE *) fp)) ) {
19       /* If the file has already been unlocked (or an entry
20          for it in the locking tables couldn't be found), could
21          mean two things:
22
23             - we're repeating an hClose on an already
24               closed file (this is likely to be a bug
25               in the implementation of hClose, as this 
26               condition should have been caught before
27               we ended up here.)
28               
29             - the file wasn't locked in the first place!
30               (file descriptors to non regular files.)
31
32          We proceed with attempting to close the file,
33          but don't flag the error should fclose() return
34          EBADF
35       */
36         unlocked=0;
37         
38     }
39
40     while ((rc = fclose((FILE *) fp)) != 0) {
41         /* See above comment */
42         if ( errno != EINTR && (!unlocked && errno != EBADF ) ) {
43             cvtErrno();
44             stdErrno();
45             return rc;
46         }
47     }
48     return 0;
49 }
50
51 \end{code}
52
53
54