]> pd.if.org Git - pdclib/blob - functions/stdio/fputs.c
Yet closer to functional output.
[pdclib] / functions / stdio / fputs.c
1 /* $Id$ */
2
3 /* fputs( const char *, FILE * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10
11 #ifndef REGTEST
12
13 int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
14 {
15     /* FIXME: This is devoid of any error checking (file writeable? r/w
16        constraints honored?)
17     */
18     /* FIXME: Proper buffering handling. */
19     while ( stream->bufidx < stream->bufsize )
20     {
21         if ( ( stream->buffer[stream->bufidx++] = *(s++) ) == '\0' )
22         {
23             break;
24         }
25     }
26     fflush( stream );
27     if ( *(s-1) != '\0' )
28     {
29         return fputs( s, stream );
30     }
31     else
32     {
33         return 1;
34     }
35 }
36
37 #endif
38
39 #ifdef TEST
40 #include <_PDCLIB_test.h>
41
42 #include <string.h>
43
44 int main( void )
45 {
46     FILE * fh;
47     char buffer[100];
48     char text[] = "SUCCESS testing fputs().";
49     TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL );
50     TESTCASE( fputs( text, fh ) != EOF );
51     TESTCASE( fclose( fh ) == 0 );
52     TESTCASE( ( fh = fopen( "testfile", "r" ) ) != NULL );
53     TESTCASE( fread( buffer, 1, strlen( text ), fh ) == strlen( text ) );
54     TESTCASE( memcmp( buffer, text, strlen( text ) ) == 0 );
55     TESTCASE( fclose( fh ) == 0 );
56     TESTCASE( remove( "testfile" ) == 0 );
57     return TEST_RESULTS;
58 }
59
60 #endif