]> git.sur5r.net Git - cc65/blob - libsrc/common/fseek.c
Merge pull request #457 from pfusik/const-arrays
[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         _seterrno (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 the seek was successful. Discard any effects of the ungetc function,
44     ** and clear the end-of-file indicator. Otherwise set the error indicator
45     ** on the stream, and return -1. We will check for >= 0 here, because that
46     ** saves some code, and we don't have files with 2 gigabytes in size
47     ** anyway:-)
48     */
49     if (res >= 0) {
50         f->f_flags &= ~(_FEOF | _FPUSHBACK);
51         return 0;
52     } else {
53         f->f_flags |= _FERROR;
54         return -1;
55     }
56 }
57