_trans2r.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /* Function to handle transition to reading.
  9. * Initialize or verify the stream's orientation (even if writeonly).
  10. * Check that the stream is readable.
  11. * If currently reading, check that we can transition to writing.
  12. * C99 requires that we not be reading, but attempting to
  13. * auto-transition by commiting the write buffer is a configurable
  14. * 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_trans2r_o(FILE * __restrict stream, int oflag)
  23. #else
  24. int attribute_hidden __stdio_trans2r(FILE * __restrict stream)
  25. #endif
  26. {
  27. __STDIO_STREAM_VALIDATE(stream);
  28. assert(!__STDIO_STREAM_IS_READING(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_WRITEONLY) {
  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_WRITING(stream)) {
  51. #ifdef __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__
  52. if (__STDIO_COMMIT_WRITE_BUFFER(stream)) { /* commit failed! */
  53. goto ERROR;
  54. }
  55. assert(!__STDIO_STREAM_BUFFER_WUSED(stream));
  56. __STDIO_STREAM_DISABLE_PUTC(stream);
  57. __STDIO_STREAM_CLEAR_WRITING(stream);
  58. #else
  59. /* C99: Output shall not be directly followed by input without an
  60. intervening call to the fflush function or to a file positioning
  61. function (fseek, fsetpos, or rewind). */
  62. __UNDEFINED_OR_NONPORTABLE;
  63. goto DO_EBADF;
  64. #endif
  65. }
  66. __STDIO_STREAM_SET_READING(stream);
  67. /* getc macro is enabled when data is read into buffer. */
  68. __STDIO_STREAM_VALIDATE(stream);
  69. return 0;
  70. }