1 /* _PDCLIB_flushbuffer( struct _PDCLIB_file_t * )
3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
7 /* This is an example implementation of _PDCLIB_flushbuffer() fit for
8 use with POSIX kernels.
15 #include <_PDCLIB_glue.h>
17 #include </usr/include/errno.h>
20 extern ssize_t write( int fd, const void * buf, size_t count );
22 /* The number of attempts to complete an output buffer flushing before giving
25 #define _PDCLIB_IO_RETRIES 1
27 /* What the system should do after an I/O operation did not succeed, before */
28 /* trying again. (Empty by default.) */
29 #define _PDCLIB_IO_RETRY_OP( stream )
31 int _PDCLIB_flushbuffer( struct _PDCLIB_file_t * stream )
33 if ( ! ( stream->status & _PDCLIB_FBIN ) )
35 /* TODO: Text stream conversion here */
37 /* No need to handle buffers > INT_MAX, as PDCLib doesn't allow them */
38 _PDCLIB_size_t written = 0;
40 /* Keep trying to write data until everything is written, an error
41 occurs, or the configured number of retries is exceeded.
43 for ( unsigned int retries = _PDCLIB_IO_RETRIES; retries > 0; --retries )
45 rc = (int)write( stream->handle, stream->buffer + written, stream->bufidx - written );
51 /* See <_PDCLIB_config.h>. There should be differenciated errno
52 handling here, possibly even a 1:1 mapping; but that is up
53 to the individual platform.
63 _PDCLIB_errno = _PDCLIB_ERROR;
66 /* This should be something like EUNKNOWN. */
67 _PDCLIB_errno = _PDCLIB_ERROR;
70 stream->status |= _PDCLIB_ERRORFLAG;
71 /* Move unwritten remains to begin of buffer. */
72 stream->bufidx -= written;
73 memmove( stream->buffer, stream->buffer + written, stream->bufidx );
76 written += (_PDCLIB_size_t)rc;
77 stream->pos.offset += rc;
78 if ( written == stream->bufidx )
80 /* Buffer written completely. */
85 /* Number of retries exceeded. You probably want a different errno value
88 _PDCLIB_errno = _PDCLIB_ERROR;
89 stream->status |= _PDCLIB_ERRORFLAG;
90 /* Move unwritten remains to begin of buffer. */
91 stream->bufidx -= written;
92 memmove( stream->buffer, stream->buffer + written, stream->bufidx );
100 #include <_PDCLIB_test.h>
104 /* Testing covered by ftell.c */