3 * GNU Library General Public License (LGPL) version 2 or later.
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
10 /* Function to handle transition to writing.
11 * Initialize or verify the stream's orientation (even if readonly).
12 * Check that the stream is writable.
13 * If currently reading, check that we can transition to writing.
14 * C99 requires that the stream is at EOF, but attempting to
15 * auto-transition via fseek() is a configurable option.
16 * Returns 0 on success and EOF otherwise.
19 * There are two function signatures, depending on wchar support,
20 * since with no wchar support the orientation is narrow by default.
23 #ifdef __UCLIBC_HAS_WCHAR__
24 int __stdio_trans2w_o(FILE * __restrict stream, int oflag)
26 int __stdio_trans2w(FILE * __restrict stream)
29 __STDIO_STREAM_VALIDATE(stream);
30 assert(!__STDIO_STREAM_IS_WRITING(stream));
32 #ifdef __UCLIBC_HAS_WCHAR__
33 if (!(stream->__modeflags & oflag)) {
34 if (stream->__modeflags & (__FLAG_NARROW|__FLAG_WIDE)) {
35 __UNDEFINED_OR_NONPORTABLE;
38 stream->__modeflags |= oflag;
42 if (stream->__modeflags & __FLAG_READONLY) {
43 #if defined(__UCLIBC_HAS_WCHAR__) || !defined(__UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__)
47 #ifdef __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__
50 __STDIO_STREAM_SET_ERROR(stream);
51 __STDIO_STREAM_VALIDATE(stream);
55 if (__STDIO_STREAM_IS_READING(stream)) {
56 if (!__FEOF_UNLOCKED(stream)) {
57 #ifdef __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__
58 /* Need to seek to correct position if we have buffered
59 * read data or ungots. If appending, we might as well
62 * NOTE: If the OS does not handle append files correctly,
63 * this is insufficient since we would need to seek to
64 * the end even if not reading.*/
65 if (((__STDIO_STREAM_BUFFER_RAVAIL(stream))
66 || (stream->__modeflags & __FLAG_UNGOT))
68 ((stream->__modeflags & __FLAG_APPEND)
69 ? SEEK_END : SEEK_CUR))
71 /* fseek() only sets error indicator on read/write error. */
75 /* C99 requires either at EOF or currently not reading. */
76 __UNDEFINED_OR_NONPORTABLE;
80 __STDIO_STREAM_CLEAR_READING_AND_UNGOTS(stream);
81 __STDIO_STREAM_DISABLE_GETC(stream);
82 /* Reaching EOF does not reset buffer pointers... */
83 __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
86 __STDIO_STREAM_SET_WRITING(stream);
87 if (__STDIO_STREAM_IS_NARROW_FBF(stream)) {
88 __STDIO_STREAM_ENABLE_PUTC(stream);
91 __STDIO_STREAM_VALIDATE(stream);