fseek.c 911 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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) 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) {
  15. f->flags |= F_ERR;
  16. return -1;
  17. }
  18. /* If seek succeeded, file is seekable and we discard read buffer. */
  19. f->rpos = f->rend = 0;
  20. f->flags &= ~F_EOF;
  21. return 0;
  22. }
  23. int __fseeko(FILE *f, off_t off, int whence)
  24. {
  25. int result;
  26. FLOCK(f);
  27. result = __fseeko_unlocked(f, off, whence);
  28. FUNLOCK(f);
  29. return result;
  30. }
  31. int fseek(FILE *f, long off, int whence)
  32. {
  33. return __fseeko(f, off, whence);
  34. }
  35. weak_alias(__fseeko, fseeko);
  36. LFS64(fseeko);