1ac3d526614fac42094fb562e22da82f29d309d3
[ghc-hetmet.git] / ghc / lib / cbits / freeFile.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1997
3 %
4 \subsection[freeFile.lc]{Giving up files}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 /* sigh, the FILEs attached to the standard descriptors are 
12    handled differently. We don't want them freed via the
13    ForeignObj finaliser, as we probably want to use these
14    before we *really* shut down (dumping stats etc.)
15 */
16 void freeStdFile(fp)
17 StgForeignObj fp;
18 { return; }
19
20 void freeFile(fp)
21 StgForeignObj fp;
22 {
23     int rc;
24
25     if ( fp == NULL || (rc = unlockFile(fileno((FILE *)fp))) ) {
26         /* If the file handle has been explicitly closed
27          * (via closeFile()) or freed, we will have given
28          * up our process lock, so we silently return here.
29          */
30        return;
31     }
32
33     /*
34      * The finaliser for the FILEs embedded in Handles. The RTS
35      * assumes that the finaliser runs without problems, so all
36      * we can do here is fclose(), and hope nothing went wrong.
37      *
38      * Assume fclose() flushes output stream.
39      */
40
41     rc = fclose((FILE *)fp);
42     /* Error or no error, we don't care.. */
43
44     /* 
45     if ( rc == EOF ) {
46        fprintf(stderr. "Warning: file close ran into trouble\n");
47     }
48     */
49
50     return;
51 }
52 \end{code}