]> git.sur5r.net Git - cc65/blob - libsrc/common/fseek.c
no TGI_ERR_NO_MEM or TGI_ERR_NO_IOCB anymore: replaced by TGI_ERR_NO_RES
[cc65] / libsrc / common / fseek.c
1 /*
2  * fseek.c
3  *
4  * Christian Groessler, 2000-08-07
5  * Ullrich von Bassewitz, 2004-05-12
6  */
7
8
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include <unistd.h>
13 #include "_file.h"
14
15
16
17 /*****************************************************************************/
18 /*                                   Code                                    */
19 /*****************************************************************************/
20
21
22
23 int __fastcall__ fseek (register FILE* f, long offset, int whence)
24 {
25     long res;
26
27     /* Is the file open? */
28     if ((f->f_flags & _FOPEN) == 0) {
29         _errno = EINVAL;                /* File not open */
30         return -1;
31     }
32
33     /* If we have a pushed back character, and whence is relative to the
34      * current position, correct the offset.
35      */
36     if ((f->f_flags & _FPUSHBACK) && whence == SEEK_CUR) {
37         --offset;
38     }
39
40     /* Do the seek */
41     res = lseek(f->f_fd, offset, whence);
42
43     /* If we had an error, set the error indicator on the stream, and
44      * return -1. We will check for < 0 here, because that saves some code,
45      * and we don't have files with 2 gigabytes in size anyway:-)
46      */
47     if (res < 0) {
48         f->f_flags |= _FERROR;
49         return -1;
50     }
51
52     /* The seek was successful. Discard any effects of the ungetc function,
53      * and clear the end-of-file indicator.
54      */
55     f->f_flags &= ~(_FEOF | _FPUSHBACK);
56
57     /* Done */
58     return 0;
59 }
60