From 566bfcc6924abd9fccbd97fa8207711e899dd0bc Mon Sep 17 00:00:00 2001 From: solar Date: Tue, 25 Jul 2006 10:12:25 +0000 Subject: [PATCH] Intermediate stdio work. --- functions/stdio/fopen.c | 6 +++++- functions/stdio/fputs.c | 9 +++++++-- functions/stdio/fseek.c | 4 ++++ functions/stdio/fsetpos.c | 4 ++++ functions/stdio/rewind.c | 4 ++++ 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index e153383..d181cc1 100644 --- a/functions/stdio/fopen.c +++ b/functions/stdio/fopen.c @@ -38,7 +38,11 @@ struct _PDCLIB_file_t * fopen( const char * _PDCLIB_restrict filename, const cha 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: diff --git a/functions/stdio/fputs.c b/functions/stdio/fputs.c index 748fa5a..94af1ad 100644 --- a/functions/stdio/fputs.c +++ b/functions/stdio/fputs.c @@ -16,16 +16,21 @@ int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_rest 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 diff --git a/functions/stdio/fseek.c b/functions/stdio/fseek.c index f33f250..ca0017c 100644 --- a/functions/stdio/fseek.c +++ b/functions/stdio/fseek.c @@ -12,6 +12,10 @@ int fseek( struct _PDCLIB_file_t * stream, long int offset, int whence ) { + if ( stream->status & _PDCLIB_WROTELAST ) + { + fflush( stream ); + } /* TODO: Implement. */ return 0; } diff --git a/functions/stdio/fsetpos.c b/functions/stdio/fsetpos.c index e40be0b..76c5ede 100644 --- a/functions/stdio/fsetpos.c +++ b/functions/stdio/fsetpos.c @@ -12,6 +12,10 @@ int fsetpos( struct _PDCLIB_file_t * stream, const _PDCLIB_fpos_t * pos ) { + if ( stream->status & _PDCLIB_WROTELAST ) + { + fflush( stream ); + } /* TODO: Implement. */ return 0; } diff --git a/functions/stdio/rewind.c b/functions/stdio/rewind.c index 543e284..d97e4e2 100644 --- a/functions/stdio/rewind.c +++ b/functions/stdio/rewind.c @@ -12,6 +12,10 @@ void rewind( struct _PDCLIB_file_t * stream ) { + if ( stream->status & _PDCLIB_WROTELAST ) + { + fflush( stream ); + } /* TODO: Implement. */ return; } -- 2.40.0