X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;ds=sidebyside;f=functions%2Fstdio%2Ffputs.c;h=94af1ad6a9154dd88b36a4bbbdf7bb75f90c8975;hb=566bfcc6924abd9fccbd97fa8207711e899dd0bc;hp=716fef838b5ebddaba629c760c52e777f268eadb;hpb=13ee02fa27a739d9d602f801126eadcfbfeab8a8;p=pdclib diff --git a/functions/stdio/fputs.c b/functions/stdio/fputs.c index 716fef8..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 @@ -39,9 +44,21 @@ int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_rest #ifdef TEST #include <_PDCLIB_test.h> +#include + int main( void ) { - TESTCASE( NO_TESTDRIVER ); + FILE * fh; + char buffer[100]; + char text[] = "SUCCESS testing fputs()."; + TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL ); + TESTCASE( fputs( text, fh ) != EOF ); + TESTCASE( fclose( fh ) == 0 ); + TESTCASE( ( fh = fopen( "testfile", "r" ) ) != NULL ); + TESTCASE( fread( buffer, 1, strlen( text ), fh ) == strlen( text ) ); + TESTCASE( memcmp( buffer, text, strlen( text ) ) == 0 ); + TESTCASE( fclose( fh ) == 0 ); + TESTCASE( remove( "testfile" ) == 0 ); return TEST_RESULTS; }