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