1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
3 * GNU Library General Public License (LGPL) version 2 or later.
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
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.
20 * There are two function signatures, depending on wchar support,
21 * since with no wchar support the orientation is narrow by default.
24 #ifdef __UCLIBC_HAS_WCHAR__
25 int attribute_hidden
__stdio_trans2w_o(FILE * __restrict stream
, int oflag
)
27 int attribute_hidden
__stdio_trans2w(FILE * __restrict stream
)
30 __STDIO_STREAM_VALIDATE(stream
);
31 assert(!__STDIO_STREAM_IS_WRITING(stream
));
33 #ifdef __UCLIBC_HAS_WCHAR__
34 if (!(stream
->__modeflags
& oflag
)) {
35 if (stream
->__modeflags
& (__FLAG_NARROW
|__FLAG_WIDE
)) {
36 __UNDEFINED_OR_NONPORTABLE
;
39 stream
->__modeflags
|= oflag
;
43 if (stream
->__modeflags
& __FLAG_READONLY
) {
44 #if defined(__UCLIBC_HAS_WCHAR__) || !defined(__UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__)
48 #ifdef __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__
51 __STDIO_STREAM_SET_ERROR(stream
);
52 __STDIO_STREAM_VALIDATE(stream
);
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
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
))
69 ((stream
->__modeflags
& __FLAG_APPEND
)
70 ? SEEK_END
: SEEK_CUR
))
72 /* fseek() only sets error indicator on read/write error. */
76 /* C99 requires either at EOF or currently not reading. */
77 __UNDEFINED_OR_NONPORTABLE
;
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
);
87 __STDIO_STREAM_SET_WRITING(stream
);
88 if (__STDIO_STREAM_IS_NARROW_FBF(stream
)) {
89 __STDIO_STREAM_ENABLE_PUTC(stream
);
92 __STDIO_STREAM_VALIDATE(stream
);