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