1
0

fseek.c 910 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "stdio_impl.h"
  2. int __fseeko_unlocked(FILE *f, off_t off, int whence)
  3. {
  4. /* Adjust relative offset for unread data in buffer, if any. */
  5. if (whence == SEEK_CUR && f->rend) off -= f->rend - f->rpos;
  6. /* Flush write buffer, and report error on failure. */
  7. if (f->wpos != f->wbase) {
  8. f->write(f, 0, 0);
  9. if (!f->wpos) return -1;
  10. }
  11. /* Leave writing mode */
  12. f->wpos = f->wbase = f->wend = 0;
  13. /* Perform the underlying seek. */
  14. if (f->seek(f, off, whence) < 0) return -1;
  15. /* If seek succeeded, file is seekable and we discard read buffer. */
  16. f->rpos = f->rend = 0;
  17. f->flags &= ~F_EOF;
  18. return 0;
  19. }
  20. int __fseeko(FILE *f, off_t off, int whence)
  21. {
  22. int result;
  23. FLOCK(f);
  24. result = __fseeko_unlocked(f, off, whence);
  25. FUNLOCK(f);
  26. return result;
  27. }
  28. int fseek(FILE *f, long off, int whence)
  29. {
  30. return __fseeko(f, off, whence);
  31. }
  32. weak_alias(__fseeko, fseeko);
  33. weak_alias(fseeko, fseeko64);