[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / lib / std / cbits / filePosn.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
3 %
4 \subsection[filePosn.lc]{hGetPosn and hSetPosn Runtime Support}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 StgInt
12 getFilePosn(ptr)
13 StgForeignObj ptr;
14 {
15     IOFileObject* fo = (IOFileObject*)ptr;
16     StgInt posn;
17    
18     while ( (posn = lseek(fo->fd, 0, SEEK_CUR)) == -1) {
19         if (errno != EINTR) {
20             cvtErrno();
21             stdErrno();
22             return -1;
23         }
24     }
25     if (fo->flags & FILEOBJ_WRITE)  {
26        posn += fo->bufWPtr;
27     } else if (fo->flags & FILEOBJ_READ) {
28        posn -= (fo->bufWPtr - fo->bufRPtr);
29     }
30     return posn;
31 }
32
33 /* The following is only called with a position that we've already visited 
34    (this is ensured by making the Haskell file posn. type abstract.)
35 */
36 StgInt
37 setFilePosn(ptr, posn)
38 StgForeignObj ptr;
39 StgInt posn;
40 {
41     IOFileObject* fo = (IOFileObject*)ptr;
42     int rc;
43
44     rc = flushBuffer(ptr);
45     if (rc < 0) return rc;
46
47     while (lseek(fo->fd, posn, SEEK_SET) == -1) {
48         if (errno != EINTR) {
49             cvtErrno();
50             stdErrno();
51             return -1;
52         }
53     }
54     FILEOBJ_CLEAR_EOF(fo);
55     return 0;
56 }
57
58 \end{code}
59
60
61