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