X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Ffputs.c;h=748fa5aff414ed8436951c48afed57e52f9cb36b;hb=f603f88b4456cf9b7ab1328bf657ede22a0c9940;hp=86abe11188058dd4566cac1c4775b0f07f9da256;hpb=34893ecc2200dc7017c36a54cb6c5f4c2378b5ec;p=pdclib diff --git a/functions/stdio/fputs.c b/functions/stdio/fputs.c index 86abe11..748fa5a 100644 --- a/functions/stdio/fputs.c +++ b/functions/stdio/fputs.c @@ -1,8 +1,60 @@ -// ---------------------------------------------------------------------------- -// $Id$ -// ---------------------------------------------------------------------------- -// Public Domain C Library - http://pdclib.sourceforge.net -// This code is Public Domain. Use, modify, and redistribute at will. -// ---------------------------------------------------------------------------- - -int fputs( const char * restrict s, FILE * restrict stream ) { /* TODO */ }; +/* $Id$ */ + +/* fputs( const char *, FILE * ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +#ifndef REGTEST + +int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +{ + /* FIXME: This is devoid of any error checking (file writeable? r/w + constraints honored?) + */ + /* FIXME: Proper buffering handling. */ + while ( stream->bufidx < stream->bufsize ) + { + if ( ( stream->buffer[stream->bufidx++] = *(s++) ) == '\0' ) + { + break; + } + } + fflush( stream ); + if ( *(s-1) != '\0' ) + { + return fputs( s, stream ); + } + else + { + return 1; + } +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +#include + +int main( void ) +{ + 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; +} + +#endif