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 #ifdef __STDIO_BUFFERS
13 /* Either buffer data or (commit buffer if necessary and) write. */
15 size_t attribute_hidden
__stdio_fwrite(const unsigned char * __restrict buffer
,
17 register FILE * __restrict stream
)
20 const unsigned char *p
;
22 __STDIO_STREAM_VALIDATE(stream
);
23 assert(__STDIO_STREAM_IS_WRITING(stream
));
27 if (!__STDIO_STREAM_IS_NBF(stream
)) { /* FBF or LBF. */
28 if (__STDIO_STREAM_IS_FAKE_VSNPRINTF(stream
)) {
29 pending
= __STDIO_STREAM_BUFFER_WAVAIL(stream
);
30 if (pending
> bytes
) {
33 memcpy(stream
->__bufpos
, buffer
, pending
);
34 stream
->__bufpos
+= pending
;
35 __STDIO_STREAM_VALIDATE(stream
);
40 if (bytes
<= __STDIO_STREAM_BUFFER_WAVAIL(stream
)) {
41 memcpy(stream
->__bufpos
, buffer
, bytes
);
42 stream
->__bufpos
+= bytes
;
43 if (__STDIO_STREAM_IS_LBF(stream
)
44 && memrchr(buffer
, '\n', bytes
) /* Search backwards. */
46 if ((pending
= __STDIO_COMMIT_WRITE_BUFFER(stream
)) > 0) {
47 if (pending
> bytes
) {
50 buffer
+= (bytes
- pending
);
51 if ((p
= memchr(buffer
, '\n', pending
)) != NULL
) {
52 pending
= (buffer
+ pending
) - p
;
54 stream
->__bufpos
-= pending
;
58 __STDIO_STREAM_VALIDATE(stream
);
61 /* FBF or LBF and not enough space in buffer. */
62 if (__STDIO_STREAM_BUFFER_WUSED(stream
)) { /* Buffered data. */
63 if (__STDIO_COMMIT_WRITE_BUFFER(stream
)) { /* Commit failed! */
70 return __stdio_WRITE(stream
, buffer
, bytes
);