fseeko.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #include "_stdio.h"
  8. #if SEEK_SET != 0 || SEEK_CUR != 1 || SEEK_END != 2
  9. # error Assumption violated -- values of SEEK_SET, SEEK_CUR, SEEK_END
  10. #endif
  11. #ifdef __DO_LARGEFILE
  12. # ifndef __UCLIBC_HAS_LFS__
  13. # error large file support is not enabled!
  14. # endif
  15. # define FSEEK __fseeko64
  16. # define OFFSET_TYPE __off64_t
  17. #else
  18. # define FSEEK fseek
  19. # define OFFSET_TYPE long int
  20. #endif
  21. int FSEEK(register FILE *stream, OFFSET_TYPE offset, int whence)
  22. {
  23. #if defined(__UCLIBC_HAS_LFS__) && !defined(__DO_LARGEFILE)
  24. return __fseeko64(stream, offset, whence);
  25. #else
  26. __offmax_t pos = offset;
  27. int retval = -1;
  28. __STDIO_AUTO_THREADLOCK_VAR;
  29. if (((unsigned int) whence) > 2) {
  30. __set_errno(EINVAL);
  31. } else {
  32. __STDIO_AUTO_THREADLOCK(stream);
  33. __STDIO_STREAM_VALIDATE(stream);
  34. if ((!__STDIO_STREAM_IS_WRITING(stream)
  35. || !__STDIO_COMMIT_WRITE_BUFFER(stream))
  36. && ((whence != SEEK_CUR)
  37. || (__stdio_adjust_position(stream, &pos) >= 0))
  38. && (__SEEK(stream, &pos, whence) >= 0)
  39. ) {
  40. /* Clear reading/writing modes, EOF, and ungots. */
  41. stream->__modeflags &=
  42. ~(__MASK_READING|__FLAG_WRITING|__FLAG_EOF);
  43. /* Make sure all pointers are reset. */
  44. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
  45. __STDIO_STREAM_DISABLE_GETC(stream);
  46. __STDIO_STREAM_DISABLE_PUTC(stream);
  47. /* We reinitialize the mbstate object. Doing so is
  48. * implementation defined behavior. */
  49. #ifdef __STDIO_MBSTATE
  50. __INIT_MBSTATE(&(stream->__state));
  51. #endif
  52. #ifdef __UCLIBC_HAS_WCHAR__
  53. stream->__ungot_width[0] = 0;
  54. #endif
  55. retval = 0;
  56. }
  57. __STDIO_STREAM_VALIDATE(stream);
  58. __STDIO_AUTO_THREADUNLOCK(stream);
  59. }
  60. return retval;
  61. #endif
  62. }
  63. #ifdef __DO_LARGEFILE
  64. weak_alias(__fseeko64,fseeko64);
  65. #else
  66. weak_alias(fseek,fseeko);
  67. #endif