fseeko.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #ifndef __DO_LARGEFILE
  12. # define FSEEK fseek
  13. # define OFFSET_TYPE long int
  14. #endif
  15. #if defined(__UCLIBC_HAS_LFS__) && !defined(__DO_LARGEFILE)
  16. libc_hidden_proto(fseeko64)
  17. #endif
  18. int FSEEK(register FILE *stream, OFFSET_TYPE offset, int whence)
  19. {
  20. #if defined(__UCLIBC_HAS_LFS__) && !defined(__DO_LARGEFILE)
  21. return fseeko64(stream, offset, whence);
  22. #else
  23. __offmax_t pos = offset;
  24. int retval = -1;
  25. __STDIO_AUTO_THREADLOCK_VAR;
  26. if (((unsigned int) whence) > 2) {
  27. __set_errno(EINVAL);
  28. } else {
  29. __STDIO_AUTO_THREADLOCK(stream);
  30. __STDIO_STREAM_VALIDATE(stream);
  31. if ((!__STDIO_STREAM_IS_WRITING(stream)
  32. || !__STDIO_COMMIT_WRITE_BUFFER(stream))
  33. && ((whence != SEEK_CUR)
  34. || (__stdio_adjust_position(stream, &pos) >= 0))
  35. && (__SEEK(stream, &pos, whence) >= 0)
  36. ) {
  37. /* Clear reading/writing modes, EOF, and ungots. */
  38. stream->__modeflags &=
  39. ~(__MASK_READING|__FLAG_WRITING|__FLAG_EOF);
  40. /* Make sure all pointers are reset. */
  41. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
  42. __STDIO_STREAM_DISABLE_GETC(stream);
  43. __STDIO_STREAM_DISABLE_PUTC(stream);
  44. /* We reinitialize the mbstate object. Doing so is
  45. * implementation defined behavior. */
  46. #ifdef __STDIO_MBSTATE
  47. __INIT_MBSTATE(&(stream->__state));
  48. #endif
  49. #ifdef __UCLIBC_HAS_WCHAR__
  50. stream->__ungot_width[0] = 0;
  51. #endif
  52. retval = 0;
  53. }
  54. __STDIO_STREAM_VALIDATE(stream);
  55. __STDIO_AUTO_THREADUNLOCK(stream);
  56. }
  57. return retval;
  58. #endif
  59. }
  60. #ifdef __DO_LARGEFILE
  61. libc_hidden_proto(fseeko64)
  62. libc_hidden_def(fseeko64)
  63. #else
  64. libc_hidden_proto(fseek)
  65. libc_hidden_def(fseek)
  66. strong_alias(fseek,fseeko)
  67. #endif