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 );
53 /* See <_PDCLIB_config.h>. There should be differenciated errno
54 handling here, possibly even a 1:1 mapping; but that is up
55 to the individual platform.
65 _PDCLIB_errno = _PDCLIB_ERROR;
68 /* This should be something like EUNKNOWN. */
69 _PDCLIB_errno = _PDCLIB_ERROR;
72 stream->status |= _PDCLIB_ERRORFLAG;
73 /* Move unwritten remains to begin of buffer. */
74 stream->bufidx -= written;
75 memmove( stream->buffer, stream->buffer + written, stream->bufidx );
78 written += (_PDCLIB_size_t)rc;
79 stream->pos.offset += rc;
80 if ( written == stream->bufidx )
82 /* Buffer written completely. */
87 /* Number of retries exceeded. You probably want a different errno value
90 _PDCLIB_errno = _PDCLIB_ERROR;
91 stream->status |= _PDCLIB_ERRORFLAG;
92 /* Move unwritten remains to begin of buffer. */
93 stream->bufidx -= written;
94 memmove( stream->buffer, stream->buffer + written, stream->bufidx );
102 #include <_PDCLIB_test.h>
106 /* Testing covered by ftell.c */