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