2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: filePosn.c,v 1.6 1999/12/08 15:47:07 simonmar Exp $
6 * hGetPosn and hSetPosn Runtime Support
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
20 IOFileObject* fo = (IOFileObject*)ptr;
23 while ( (posn = lseek(fo->fd, 0, SEEK_CUR)) == -1) {
30 if (fo->flags & FILEOBJ_WRITE) {
32 } else if (fo->flags & FILEOBJ_READ) {
33 posn -= (fo->bufWPtr - fo->bufRPtr);
35 if (!(fo->flags & FILEOBJ_BINARY)) {
36 /* Sigh, to get at the Real file position for files opened
37 in text mode, we need to scan the read buffer looking for
38 '\n's, making them count as \r\n (i.e., undoing the work of
39 read()), since lseek() returns the raw position.
45 if (((char*)fo->buf)[i] == '\n') {
56 /* The following is only called with a position that we've already visited
57 (this is ensured by making the Haskell file posn. type abstract.)
60 setFilePosn(StgForeignPtr ptr, StgInt size, StgByteArray d)
62 IOFileObject* fo = (IOFileObject*)ptr;
67 * We need to snatch the offset out of an MP_INT. The bits are there sans sign,
68 * which we pick up from our size parameter. If abs(size) is greater than 1,
69 * this integer is just too big.
73 offset = -*(StgInt *) d;
79 offset = *(StgInt *) d;
82 ghc_errtype = ERR_INVALIDARGUMENT;
83 ghc_errstr = "offset out of range";
87 rc = flushBuffer(ptr);
88 if (rc < 0) return rc;
90 while (lseek(fo->fd, offset, SEEK_SET) == -1) {
97 FILEOBJ_CLEAR_EOF(fo);