3 /* _PDCLIB_flushbuffer( struct _PDCLIB_file_t * )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
9 /* This is an example implementation of _PDCLIB_flushbuffer() fit for
10 use with POSIX kernels.
17 #include <_PDCLIB_glue.h>
19 #include </usr/include/errno.h>
22 extern ssize_t write( int fd, const void * buf, size_t count );
24 /* The number of attempts to complete an output buffer flushing before giving
27 #define _PDCLIB_IO_RETRIES 1
29 /* What the system should do after an I/O operation did not succeed, before */
30 /* trying again. (Empty by default.) */
31 #define _PDCLIB_IO_RETRY_OP( stream )
33 int _PDCLIB_flushbuffer( struct _PDCLIB_file_t * stream )
35 if ( ! ( stream->status & _PDCLIB_FBIN ) )
37 /* TODO: Text stream conversion here */
39 /* No need to handle buffers > INT_MAX, as PDCLib doesn't allow them */
40 _PDCLIB_size_t written = 0;
42 /* Keep trying to write data until everything is written, an error
43 occurs, or the configured number of retries is exceeded.
45 for ( unsigned int retries = _PDCLIB_IO_RETRIES; retries > 0; --retries )
47 rc = (int)write( stream->handle, stream->buffer + written, stream->bufidx - written );
61 _PDCLIB_errno = _PDCLIB_EIO;
64 _PDCLIB_errno = _PDCLIB_EUNKNOWN;
67 stream->status |= _PDCLIB_ERRORFLAG;
68 /* Move unwritten remains to begin of buffer. */
69 stream->bufidx -= written;
70 memmove( stream->buffer, stream->buffer + written, stream->bufidx );
73 written += (_PDCLIB_size_t)rc;
74 stream->pos.offset += rc;
75 if ( written == stream->bufidx )
77 /* Buffer written completely. */
82 _PDCLIB_errno = _PDCLIB_ERETRY;
83 stream->status |= _PDCLIB_ERRORFLAG;
84 /* Move unwritten remains to begin of buffer. */
85 stream->bufidx -= written;
86 memmove( stream->buffer, stream->buffer + written, stream->bufidx );
94 #include <_PDCLIB_test.h>
98 /* Testing covered by ftell.c */