2 Copyright © 1995-2008, The AROS Development Team. All rights reserved.
5 Change the position in a stream.
11 #include <proto/dos.h>
16 /*****************************************************************************
29 Change the current position in a stream.
32 stream - Modify this stream
33 offset, whence - How to modify the current position. whence
34 can be SEEK_SET, then offset is the absolute position
35 in the file (0 is the first byte), SEEK_CUR then the
36 position will change by offset (ie. -5 means to move
37 5 bytes to the beginning of the file) or SEEK_END.
38 SEEK_END means that the offset is relative to the
39 end of the file (-1 is the last byte and 0 is
43 0 on success and -1 on error. If an error occurred, the global
44 variable errno is set.
51 Not fully compatible with iso fseek, especially in 'ab' and 'a+b'
59 ******************************************************************************/
62 int finalseekposition
= 0;
64 struct FileInfoBlock
*fib
= NULL
;
66 fdesc
*fdesc
= __getfdesc(stream
->fd
);
74 fh
= (BPTR
)(fdesc
->fcb
->fh
);
76 /* This is buffered IO, flush the buffer before any Seek */
79 /* Handling for fseek specific behaviour (not all cases handled) */
80 /* Get current position */
81 cnt
= Seek (fh
, 0, OFFSET_CURRENT
);
84 errno
= IoErr2errno (IoErr ());
89 fib
= AllocDosObject(DOS_FIB
, NULL
);
92 errno
= IoErr2errno(IoErr());
96 if (ExamineFH(fh
, fib
))
97 eofposition
= fib
->fib_Size
;
100 /* Does not happen on sfs/affs */
101 FreeDosObject(DOS_FIB
, fib
);
107 FreeDosObject(DOS_FIB
, fib
);
112 case SEEK_SET
: finalseekposition
= offset
; break;
113 case SEEK_CUR
: finalseekposition
= cnt
+ offset
; break;
114 case SEEK_END
: finalseekposition
= eofposition
+ offset
; break;
120 /* Check conditions */
121 /* Seek before beginning of file */
122 if (finalseekposition
< 0)
128 /* Seek beyond end of file and in write mode */
129 if (finalseekposition
> eofposition
)
131 if (fdesc
->fcb
->flags
& O_WRITE
)
133 /* Write '0' to fill up to requested size - compatible fseek does not write but allows write */
135 int bytestowrite
= finalseekposition
- eofposition
;
136 int chunkcount
= (bytestowrite
)/128;
137 char zeroarray
[128] = {0};
139 Seek (fh
, 0, OFFSET_END
);
140 for (i
= 0; i
< chunkcount
; i
++)
141 FWrite(fh
, (STRPTR
)zeroarray
, 128, 1);
142 FWrite(fh
, (STRPTR
)zeroarray
, bytestowrite
- (chunkcount
* 128), 1);
147 cnt
= Seek (fh
, finalseekposition
, OFFSET_BEGINNING
);
150 errno
= IoErr2errno (IoErr ());