]> pd.if.org Git - pdclib/blob - functions/stdio/fputc.c
Yet closer to functional output.
[pdclib] / functions / stdio / fputc.c
1 /* $Id$ */
2
3 /* fputc( int, 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 /* Write the value c (cast to unsigned char) to the given stream.
14    Returns c if successful, EOF otherwise.
15    If a write error occurs, sets the error indicator of the stream is set.
16 */
17 int fputc( int c, struct _PDCLIB_file_t * stream )
18 {
19     /* FIXME: This is devoid of any error checking (file writeable? r/w
20     constraints honored?) (Text format translations?)
21     */
22     stream->buffer[stream->bufidx++] = (char)c;
23     if ( ( stream->bufidx == stream->bufsize )                   /* _IOFBF */
24            || ( ( stream->status & _IOLBF ) && ( (char)c == '\n' ) ) /* _IOLBF */
25            || ( stream->status & _IONBF )                        /* _IONBF */
26     )
27     {
28         /* buffer filled, unbuffered stream, or end-of-line. */
29         fflush( stream );
30     }
31     else
32     {
33         stream->status |= _PDCLIB_WROTELAST;
34     }
35     return c;
36 }
37
38 #endif
39
40 #ifdef TEST
41 #include <_PDCLIB_test.h>
42
43 int main( void )
44 {
45     FILE * fh;
46     char buffer[100];
47     TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL );
48     TESTCASE( fputc( '!', fh ) == '!' );
49     TESTCASE( fclose( fh ) == 0 );
50     TESTCASE( ( fh = fopen( "testfile", "r" ) ) != NULL );
51     TESTCASE( fread( buffer, 1, 1, fh ) == 1 );
52     TESTCASE( buffer[0] == '!' );
53     TESTCASE( fclose( fh ) == 0 );
54     return TEST_RESULTS;
55 }
56
57 #endif