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>
15 int fseek(FILE *f, long offset, int ptrname)
17 long p = -1; /* can't happen? */
26 if (f->_base && !(f->_flag & _IONBF))
29 if (ptrname == SEEK_CUR)
34 /* check if the target position is in the buffer and
35 optimize seek by moving inside the buffer */
36 if (ptrname == SEEK_SET && (f->_flag & (_IOUNGETC|_IOREAD|_IOWRT )) == 0
37 && p-offset <= f->_ptr-f->_base && offset-p <= f->_cnt)
45 p = lseek(fileno(f), offset, ptrname);
48 f->_flag &= ~_IOUNGETC;
53 return lseek(fileno(f), offset, ptrname) == -1 || p == EOF ?
56 return p==-1 ? -1 : 0;