1 /* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */
2 /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
3 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
5 #include <msvcrt/stdio.h>
6 #include <msvcrt/errno.h>
7 #include <msvcrt/fcntl.h>
9 #include <msvcrt/internal/file.h>
12 int fseek(FILE *f, long offset, int ptrname)
14 long p = -1; /* can't happen? */
23 if (f->_base && !(f->_flag & _IONBF))
26 if (ptrname == SEEK_CUR)
31 /* check if the target position is in the buffer and
32 optimize seek by moving inside the buffer */
33 if (ptrname == SEEK_SET && (f->_flag & (_IOUNGETC|_IOREAD|_IOWRT )) == 0
34 && p-offset <= f->_ptr-f->_base && offset-p <= f->_cnt)
42 p = lseek(fileno(f), offset, ptrname);
45 f->_flag &= ~_IOUNGETC;
50 return lseek(fileno(f), offset, ptrname) == -1 || p == EOF ?
53 return p==-1 ? -1 : 0;