fseek.c 509 B

1234567891011121314151617
  1. #include <stdio.h>
  2. #include <peekpoke.h>
  3. __ATTR_LIB_C__ short fseek(FILE *f, long offset, short wh)
  4. {
  5. short bmode=f->flags&_F_BIN;
  6. char *start=(char*)f->base+(bmode?2:5);
  7. char *end=(char*)f->base+peek_w(f->base)+(bmode?2:0);
  8. char *pos=((wh==SEEK_SET)?start:((wh==SEEK_CUR)?(f->fpos):end))+offset;
  9. if(f->flags&_F_ERR) return EOF;
  10. if(pos<start||pos>end||(unsigned short)wh>SEEK_END) __FERROR(f);
  11. f->fpos=pos;
  12. f->unget=0;
  13. if(pos==end) f->flags|=_F_EOF;
  14. else f->flags&=~_F_EOF;
  15. return 0;
  16. }