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