if ( ( rc->buffer = malloc( BUFSIZ ) ) == NULL ) goto fail;
rc->bufsize = BUFSIZ;
rc->bufidx = 0;
- rc->status |= ( _PDCLIB_LIBBUFFER | _PDCLIB_VIRGINSTR );
+ /* Setting buffer to _IOLBF because "when opened, a stream is fully
+ buffered if and only if it can be determined not to refer to an
+ interactive device."
+ */
+ rc->status |= ( _PDCLIB_LIBBUFFER | _PDCLIB_VIRGINSTR /* | _IOLBF */ ); /* FIXME: Uncommenting the _IOLBF here breaks output. */
/* TODO: Setting mbstate */
return rc;
fail:
constraints honored?)
*/
/* FIXME: Proper buffering handling. */
+ char written;
while ( stream->bufidx < stream->bufsize )
{
- if ( ( stream->buffer[stream->bufidx++] = *(s++) ) == '\0' )
+ written = ( stream->buffer[stream->bufidx++] = *(s++) );
+ if ( ( written == '\0' ) ||
+ ( ( stream->status & _IOLBF ) && ( written == '\n' ) ) ||
+ ( stream->status & _IONBF ) )
{
break;
}
}
fflush( stream );
- if ( *(s-1) != '\0' )
+ if ( written != '\0' )
{
+ /* FIXME: For _IONBF, this recurses once per character - unacceptable. */
return fputs( s, stream );
}
else
int fseek( struct _PDCLIB_file_t * stream, long int offset, int whence )
{
+ if ( stream->status & _PDCLIB_WROTELAST )
+ {
+ fflush( stream );
+ }
/* TODO: Implement. */
return 0;
}
int fsetpos( struct _PDCLIB_file_t * stream, const _PDCLIB_fpos_t * pos )
{
+ if ( stream->status & _PDCLIB_WROTELAST )
+ {
+ fflush( stream );
+ }
/* TODO: Implement. */
return 0;
}
void rewind( struct _PDCLIB_file_t * stream )
{
+ if ( stream->status & _PDCLIB_WROTELAST )
+ {
+ fflush( stream );
+ }
/* TODO: Implement. */
return;
}