_trans2w.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. libc_hidden_proto(fseek)
  9. /* Function to handle transition to writing.
  10. * Initialize or verify the stream's orientation (even if readonly).
  11. * Check that the stream is writable.
  12. * If currently reading, check that we can transition to writing.
  13. * C99 requires that the stream is at EOF, but attempting to
  14. * auto-transition via fseek() is a configurable option.
  15. * Returns 0 on success and EOF otherwise.
  16. *
  17. * Notes:
  18. * There are two function signatures, depending on wchar support,
  19. * since with no wchar support the orientation is narrow by default.
  20. */
  21. #ifdef __UCLIBC_HAS_WCHAR__
  22. int attribute_hidden __stdio_trans2w_o(FILE * __restrict stream, int oflag)
  23. #else
  24. int attribute_hidden __stdio_trans2w(FILE * __restrict stream)
  25. #endif
  26. {
  27. __STDIO_STREAM_VALIDATE(stream);
  28. assert(!__STDIO_STREAM_IS_WRITING(stream));
  29. #ifdef __UCLIBC_HAS_WCHAR__
  30. if (!(stream->__modeflags & oflag)) {
  31. if (stream->__modeflags & (__FLAG_NARROW|__FLAG_WIDE)) {
  32. __UNDEFINED_OR_NONPORTABLE;
  33. goto DO_EBADF;
  34. }
  35. stream->__modeflags |= oflag;
  36. }
  37. #endif
  38. if (stream->__modeflags & __FLAG_READONLY) {
  39. #if defined(__UCLIBC_HAS_WCHAR__) || !defined(__UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__)
  40. DO_EBADF:
  41. #endif
  42. __set_errno(EBADF);
  43. #ifdef __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__
  44. ERROR:
  45. #endif
  46. __STDIO_STREAM_SET_ERROR(stream);
  47. __STDIO_STREAM_VALIDATE(stream);
  48. return EOF;
  49. }
  50. if (__STDIO_STREAM_IS_READING(stream)) {
  51. if (!__FEOF_UNLOCKED(stream)) {
  52. #ifdef __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__
  53. /* Need to seek to correct position if we have buffered
  54. * read data or ungots. If appending, we might as well
  55. * seek to the end.
  56. *
  57. * NOTE: If the OS does not handle append files correctly,
  58. * this is insufficient since we would need to seek to
  59. * the end even if not reading.*/
  60. if (((__STDIO_STREAM_BUFFER_RAVAIL(stream))
  61. || (stream->__modeflags & __FLAG_UNGOT))
  62. && fseek(stream, 0L,
  63. ((stream->__modeflags & __FLAG_APPEND)
  64. ? SEEK_END : SEEK_CUR))
  65. ) {
  66. /* fseek() only sets error indicator on read/write error. */
  67. goto ERROR;
  68. }
  69. #else
  70. /* C99 requires either at EOF or currently not reading. */
  71. __UNDEFINED_OR_NONPORTABLE;
  72. goto DO_EBADF;
  73. #endif
  74. }
  75. __STDIO_STREAM_CLEAR_READING_AND_UNGOTS(stream);
  76. __STDIO_STREAM_DISABLE_GETC(stream);
  77. /* Reaching EOF does not reset buffer pointers... */
  78. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
  79. }
  80. __STDIO_STREAM_SET_WRITING(stream);
  81. if (__STDIO_STREAM_IS_NARROW_FBF(stream)) {
  82. __STDIO_STREAM_ENABLE_PUTC(stream);
  83. }
  84. __STDIO_STREAM_VALIDATE(stream);
  85. return 0;
  86. }