fseeko.c 1.9 KB

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