]> pd.if.org Git - pdclib/blob - functions/stdio/fputs.c
9a980892adc3f1b8b39ea1e10397160d10fe211c
[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 #include <_PDCLIB_glue.h>
13
14 int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
15 {
16     if ( _PDCLIB_prepwrite( stream ) == EOF )
17     {
18         return EOF;
19     }
20     while ( *s != '\0' )
21     {
22         /* Unbuffered and line buffered streams get flushed when fputs() does
23            write the terminating end-of-line. All streams get flushed if the
24            buffer runs full.
25         */
26         stream->buffer[ stream->bufidx++ ] = *s;
27         if ( ( stream->bufidx == stream->bufsize ) ||
28              ( ( stream->status & _IOLBF ) && *s == '\n' )
29            )
30         {
31             if ( _PDCLIB_flushbuffer( stream ) == EOF )
32             {
33                 return EOF;
34             }
35         }
36         ++s;
37     }
38     if ( stream->status & _IONBF )
39     {
40         if ( _PDCLIB_flushbuffer( stream ) == EOF )
41         {
42             return EOF;
43         }
44     }
45     return 0;
46 }
47
48 #endif
49 #ifdef TEST
50 #include <_PDCLIB_test.h>
51
52 int main( void )
53 {
54     char const * const testfile = "testfile";
55     char const * const message = "SUCCESS testing fputs()";
56     FILE * fh;
57     remove( testfile );
58     TESTCASE( ( fh = fopen( testfile, "w+" ) ) != NULL );
59     TESTCASE( fputs( message, fh ) >= 0 );
60     rewind( fh );
61     for ( size_t i = 0; i < 23; ++i )
62     {
63         TESTCASE( fgetc( fh ) == message[i] );
64     }
65     TESTCASE( fclose( fh ) == 0 );
66     TESTCASE( remove( testfile ) == 0 );
67     return TEST_RESULTS;
68 }
69
70 #endif
71