]> pd.if.org Git - pdclib/blob - functions/stdio/fputs.c
PDCLib includes with quotes, not <>.
[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 #include "_PDCLIB_io.h"
11
12 int _PDCLIB_fputs_unlocked( const char * _PDCLIB_restrict s, 
13                     FILE * _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 int fputs( const char * _PDCLIB_restrict s,
48            FILE * _PDCLIB_restrict stream )
49 {
50     _PDCLIB_flockfile( stream );
51     int r = _PDCLIB_fputs_unlocked( s, stream );
52     _PDCLIB_funlockfile( stream );
53     return r;
54 }
55
56 #endif
57 #ifdef TEST
58 #include "_PDCLIB_test.h"
59
60 int main( void )
61 {
62     char const * const message = "SUCCESS testing fputs()";
63     FILE * fh;
64     TESTCASE( ( fh = tmpfile() ) != NULL );
65     TESTCASE( fputs( message, fh ) >= 0 );
66     rewind( fh );
67     for ( size_t i = 0; i < 23; ++i )
68     {
69         TESTCASE( fgetc( fh ) == message[i] );
70     }
71     TESTCASE( fclose( fh ) == 0 );
72     return TEST_RESULTS;
73 }
74
75 #endif
76