]> pd.if.org Git - pdclib/blob - functions/stdio/fputs.c
Intermediate stdio work.
[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
13 int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
14 {
15     /* FIXME: This is devoid of any error checking (file writeable? r/w
16        constraints honored?)
17     */
18     /* FIXME: Proper buffering handling. */
19     char written;
20     while ( stream->bufidx < stream->bufsize )
21     {
22         written = ( stream->buffer[stream->bufidx++] = *(s++) );
23         if ( ( written == '\0' ) ||
24              ( ( stream->status & _IOLBF ) && ( written == '\n' ) ) ||
25              ( stream->status & _IONBF ) )
26         {
27             break;
28         }
29     }
30     fflush( stream );
31     if ( written != '\0' )
32     {
33         /* FIXME: For _IONBF, this recurses once per character - unacceptable. */
34         return fputs( s, stream );
35     }
36     else
37     {
38         return 1;
39     }
40 }
41
42 #endif
43
44 #ifdef TEST
45 #include <_PDCLIB_test.h>
46
47 #include <string.h>
48
49 int main( void )
50 {
51     FILE * fh;
52     char buffer[100];
53     char text[] = "SUCCESS testing fputs().";
54     TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL );
55     TESTCASE( fputs( text, fh ) != EOF );
56     TESTCASE( fclose( fh ) == 0 );
57     TESTCASE( ( fh = fopen( "testfile", "r" ) ) != NULL );
58     TESTCASE( fread( buffer, 1, strlen( text ), fh ) == strlen( text ) );
59     TESTCASE( memcmp( buffer, text, strlen( text ) ) == 0 );
60     TESTCASE( fclose( fh ) == 0 );
61     TESTCASE( remove( "testfile" ) == 0 );
62     return TEST_RESULTS;
63 }
64
65 #endif