]> pd.if.org Git - pdclib/blob - functions/stdio/fputs.c
Whitespace cleanups.
[pdclib] / functions / stdio / fputs.c
1 /* fputs( const char *, FILE * )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdio.h>
8
9 #ifndef REGTEST
10
11 #include "_PDCLIB_glue.h"
12
13 int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
14 {
15     if ( _PDCLIB_prepwrite( stream ) == EOF )
16     {
17         return EOF;
18     }
19     while ( *s != '\0' )
20     {
21         /* Unbuffered and line buffered streams get flushed when fputs() does
22            write the terminating end-of-line. All streams get flushed if the
23            buffer runs full.
24         */
25         stream->buffer[ stream->bufidx++ ] = *s;
26         if ( ( stream->bufidx == stream->bufsize ) ||
27              ( ( stream->status & _IOLBF ) && *s == '\n' )
28            )
29         {
30             if ( _PDCLIB_flushbuffer( stream ) == EOF )
31             {
32                 return EOF;
33             }
34         }
35         ++s;
36     }
37     if ( stream->status & _IONBF )
38     {
39         if ( _PDCLIB_flushbuffer( stream ) == EOF )
40         {
41             return EOF;
42         }
43     }
44     return 0;
45 }
46
47 #endif
48
49 #ifdef TEST
50
51 #include "_PDCLIB_test.h"
52
53 int main( void )
54 {
55     char const * const message = "SUCCESS testing fputs()";
56     FILE * fh;
57     TESTCASE( ( fh = tmpfile() ) != NULL );
58     TESTCASE( fputs( message, fh ) >= 0 );
59     rewind( fh );
60     for ( size_t i = 0; i < 23; ++i )
61     {
62         TESTCASE( fgetc( fh ) == message[i] );
63     }
64     TESTCASE( fclose( fh ) == 0 );
65     return TEST_RESULTS;
66 }
67
68 #endif