_trans2r.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 __stdio_trans2r_o(FILE * __restrict stream, int oflag)
  23. #else
  24. int __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. DO_EBADF:
  40. __set_errno(EBADF);
  41. ERROR:
  42. __STDIO_STREAM_SET_ERROR(stream);
  43. __STDIO_STREAM_VALIDATE(stream);
  44. return EOF;
  45. }
  46. if (__STDIO_STREAM_IS_WRITING(stream)) {
  47. #ifdef __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__
  48. if (__STDIO_COMMIT_WRITE_BUFFER(stream)) { /* commit failed! */
  49. goto ERROR;
  50. }
  51. assert(!__STDIO_STREAM_BUFFER_WUSED(stream));
  52. __STDIO_STREAM_DISABLE_PUTC(stream);
  53. __STDIO_STREAM_CLEAR_WRITING(stream);
  54. #else
  55. /* C99: Output shall not be directly followed by input without an
  56. intervening call to the fflush function or to a file positioning
  57. function (fseek, fsetpos, or rewind). */
  58. __UNDEFINED_OR_NONPORTABLE;
  59. goto DO_EBADF;
  60. #endif
  61. }
  62. __STDIO_STREAM_SET_READING(stream);
  63. /* getc macro is enabled when data is read into buffer. */
  64. __STDIO_STREAM_VALIDATE(stream);
  65. return 0;
  66. }