]> pd.if.org Git - pdclib/blob - functions/stdio/puts.c
Whitespace cleanups.
[pdclib] / functions / stdio / puts.c
1 /* puts( const char * )
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 extern char * _PDCLIB_eol;
14
15 int puts( const char * s )
16 {
17     if ( _PDCLIB_prepwrite( stdout ) == EOF )
18     {
19         return EOF;
20     }
21     while ( *s != '\0' )
22     {
23         stdout->buffer[ stdout->bufidx++ ] = *s++;
24         if ( stdout->bufidx == stdout->bufsize )
25         {
26             if ( _PDCLIB_flushbuffer( stdout ) == EOF )
27             {
28                 return EOF;
29             }
30         }
31     }
32     stdout->buffer[ stdout->bufidx++ ] = '\n';
33     if ( ( stdout->bufidx == stdout->bufsize ) ||
34          ( stdout->status & ( _IOLBF | _IONBF ) ) )
35     {
36         return _PDCLIB_flushbuffer( stdout );
37     }
38     else
39     {
40         return 0;
41     }
42 }
43
44 #endif
45
46 #ifdef TEST
47
48 #include "_PDCLIB_test.h"
49
50 int main( void )
51 {
52     FILE * fh;
53     char const * message = "SUCCESS testing puts()";
54     char buffer[23];
55     buffer[22] = 'x';
56     TESTCASE( ( fh = freopen( testfile, "wb+", stdout ) ) != NULL );
57     TESTCASE( puts( message ) >= 0 );
58     rewind( fh );
59     TESTCASE( fread( buffer, 1, 22, fh ) == 22 );
60     TESTCASE( memcmp( buffer, message, 22 ) == 0 );
61     TESTCASE( buffer[22] == 'x' );
62     TESTCASE( fclose( fh ) == 0 );
63     TESTCASE( remove( testfile ) == 0 );
64     return TEST_RESULTS;
65 }
66
67 #endif