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.
16 #include "_PDCLIB_glue.h"
18 #include </usr/include/errno.h>
21 extern ssize_t write( int fd, const void * buf, size_t count );
23 /* The number of attempts to complete an output buffer flushing before giving
26 #define _PDCLIB_IO_RETRIES 1
28 /* What the system should do after an I/O operation did not succeed, before */
29 /* trying again. (Empty by default.) */
30 #define _PDCLIB_IO_RETRY_OP( stream )
32 int _PDCLIB_flushbuffer( struct _PDCLIB_file_t * stream )
34 if ( ! ( stream->status & _PDCLIB_FBIN ) )
36 /* TODO: Text stream conversion here */
38 /* No need to handle buffers > INT_MAX, as PDCLib doesn't allow them */
39 _PDCLIB_size_t written = 0;
41 /* Keep trying to write data until everything is written, an error
42 occurs, or the configured number of retries is exceeded.
44 for ( unsigned int retries = _PDCLIB_IO_RETRIES; retries > 0; --retries )
46 rc = (int)write( stream->handle, stream->buffer + written, stream->bufidx - written );
52 /* See <_PDCLIB_config.h>. There should be differenciated errno
53 handling here, possibly even a 1:1 mapping; but that is up
54 to the individual platform.
64 _PDCLIB_errno = _PDCLIB_ERROR;
67 /* This should be something like EUNKNOWN. */
68 _PDCLIB_errno = _PDCLIB_ERROR;
71 stream->status |= _PDCLIB_ERRORFLAG;
72 /* Move unwritten remains to begin of buffer. */
73 stream->bufidx -= written;
74 memmove( stream->buffer, stream->buffer + written, stream->bufidx );
77 written += (_PDCLIB_size_t)rc;
78 stream->pos.offset += rc;
79 if ( written == stream->bufidx )
81 /* Buffer written completely. */
86 /* Number of retries exceeded. You probably want a different errno value
89 _PDCLIB_errno = _PDCLIB_ERROR;
90 stream->status |= _PDCLIB_ERRORFLAG;
91 /* Move unwritten remains to begin of buffer. */
92 stream->bufidx -= written;
93 memmove( stream->buffer, stream->buffer + written, stream->bufidx );
101 #include "_PDCLIB_test.h"
105 /* Testing covered by ftell.c */