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