]> pd.if.org Git - pdclib/blob - functions/stdio/fputc.c
Minimize the amount of internal definitions which get exposed via the user-visible...
[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 #include <_PDCLIB_io.h>
13
14 /* Write the value c (cast to unsigned char) to the given stream.
15    Returns c if successful, EOF otherwise.
16    If a write error occurs, the error indicator of the stream is set.
17 */
18 int fputc_unlocked( int c, FILE * stream )
19 {
20     if ( _PDCLIB_prepwrite( stream ) == EOF )
21     {
22         return EOF;
23     }
24     stream->buffer[stream->bufidx++] = (char)c;
25     if ( ( stream->bufidx == stream->bufsize )                   /* _IOFBF */
26            || ( ( stream->status & _IOLBF ) && ( (char)c == '\n' ) ) /* _IOLBF */
27            || ( stream->status & _IONBF )                        /* _IONBF */
28     )
29     {
30         /* buffer filled, unbuffered stream, or end-of-line. */
31         return ( _PDCLIB_flushbuffer( stream ) == 0 ) ? c : EOF;
32     }
33     return c;
34 }
35
36 int fputc( int c, FILE * stream )
37 {
38     flockfile( stream );
39     int r = fputc_unlocked( c, stream );
40     funlockfile( stream );
41     return r;
42 }
43
44 #endif
45
46 #ifdef TEST
47 #include <_PDCLIB_test.h>
48
49 int main( void )
50 {
51     /* Testing covered by ftell.c */
52     return TEST_RESULTS;
53 }
54
55 #endif