]> Git Repo - uclibc-ng.git/blob - libc/stdio/_trans2w.c
make sure TARGET_ARCH is set ... thought i committed this already?
[uclibc-ng.git] / libc / stdio / _trans2w.c
1 /* Copyright (C) 2004       Manuel Novoa III    <[email protected]>
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 /* 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.
17  *
18  * Notes:
19  *   There are two function signatures, depending on wchar support,
20  *   since with no wchar support the orientation is narrow by default.
21  */
22
23 #ifdef __UCLIBC_HAS_WCHAR__
24 int __stdio_trans2w_o(FILE * __restrict stream, int oflag)
25 #else
26 int __stdio_trans2w(FILE * __restrict stream)
27 #endif
28 {
29         __STDIO_STREAM_VALIDATE(stream);
30         assert(!__STDIO_STREAM_IS_WRITING(stream));
31
32 #ifdef __UCLIBC_HAS_WCHAR__
33         if (!(stream->__modeflags & oflag)) {
34                 if (stream->__modeflags & (__FLAG_NARROW|__FLAG_WIDE)) {
35                         __UNDEFINED_OR_NONPORTABLE;
36                         goto DO_EBADF;
37                 }
38                 stream->__modeflags |= oflag;
39         }
40 #endif
41
42         if (stream->__modeflags & __FLAG_READONLY) {
43 #if defined(__UCLIBC_HAS_WCHAR__) || !defined(__UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__)
44         DO_EBADF:
45 #endif
46                 __set_errno(EBADF);
47 #ifdef __UCLIBC_HAS_STDIO_AUTO_RW_TRANSITION__
48         ERROR:
49 #endif
50                 __STDIO_STREAM_SET_ERROR(stream);
51                 __STDIO_STREAM_VALIDATE(stream);
52                 return EOF;
53         }
54
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
60                          * seek to the end.
61                          *
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))
67                                 && fseek(stream, 0L,
68                                                  ((stream->__modeflags & __FLAG_APPEND)
69                                                   ? SEEK_END : SEEK_CUR))
70                                 ) {
71                                 /* fseek() only sets error indicator on read/write error. */
72                                 goto ERROR;
73                         }
74 #else
75                         /* C99 requires either at EOF or currently not reading. */
76                         __UNDEFINED_OR_NONPORTABLE;
77                         goto DO_EBADF;
78 #endif
79                 }
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);
84         }
85
86         __STDIO_STREAM_SET_WRITING(stream);
87         if (__STDIO_STREAM_IS_NARROW_FBF(stream)) {
88                 __STDIO_STREAM_ENABLE_PUTC(stream);
89         }
90
91         __STDIO_STREAM_VALIDATE(stream);
92         return 0;
93 }
This page took 0.027032 seconds and 4 git commands to generate.