Updated PCI IDs to latest snapshot.
[tangerine.git] / rom / dos / seek.c
blobe3f89c6f2e64b7ee0752fa0ec833238c9107d937
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Change the current read/write position in a file.
6 Lang: english
7 */
8 #include <proto/exec.h>
9 #include <dos/filesystem.h>
10 #include "dos_intern.h"
12 /*****************************************************************************
14 NAME */
15 #include <proto/dos.h>
17 AROS_LH3(LONG, Seek,
19 /* SYNOPSIS */
20 AROS_LHA(BPTR, file, D1),
21 AROS_LHA(LONG, position, D2),
22 AROS_LHA(LONG, mode, D3),
24 /* LOCATION */
25 struct DosLibrary *, DOSBase, 11, Dos)
27 /* FUNCTION
28 Changes the current read/write position in a file and/or
29 reads the current position, e.g to get the current position
30 do a Seek(file,0,OFFSET_CURRENT).
32 This function may fail (obviously) on certain devices such
33 as pipes or console handlers.
35 INPUTS
36 file - filehandle
37 position - relative offset in bytes (positive, negative or 0).
38 mode - Where to count from. Either OFFSET_BEGINNING,
39 OFFSET_CURRENT or OFFSET_END.
41 RESULT
42 Absolute position in bytes before the Seek(), -1 if an error
43 happened. IoErr() will give additional information in that case.
45 NOTES
47 EXAMPLE
49 BUGS
51 SEE ALSO
53 INTERNALS
55 *****************************************************************************/
57 AROS_LIBFUNC_INIT
59 /* Get pointer to filehandle. */
60 struct FileHandle *fh = (struct FileHandle *)BADDR(file);
61 LONG offset = 0, ret;
63 /* If the file is in append mode, seeking is not allowed. */
64 if( fh->fh_Flags & FHF_APPEND )
66 return InternalSeek( fh, 0, OFFSET_CURRENT, DOSBase );
69 /* If the file is in write mode flush it. */
70 if( fh->fh_Flags & FHF_WRITE )
72 InternalFlush( fh, DOSBase );
74 else
76 /* Read mode. Adjust the offset so that buffering is
77 taken into account. */
78 if (fh->fh_Pos < fh->fh_End && mode == OFFSET_CURRENT)
79 offset = (LONG)(fh->fh_Pos - fh->fh_End);
82 /* Read mode. Just reinit the buffers. We can't call
83 Flush() in this case as that would end up in
84 recursion. */
85 fh->fh_Pos = fh->fh_End = fh->fh_Buf;
88 ret = InternalSeek( fh, position + offset, mode, DOSBase );
89 return (ret == -1) ? -1 : (ret + offset);
91 AROS_LIBFUNC_EXIT
92 } /* Seek */