OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / stdio / fputc.c
1 /* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
2  *
3  * GNU Library General Public License (LGPL) version 2 or later.
4  *
5  * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
6  */
7
8 #include "_stdio.h"
9
10 #undef fputc
11 #undef fputc_unlocked
12 #undef putc
13 #undef putc_unlocked
14
15 #ifdef __DO_UNLOCKED
16
17 int __fputc_unlocked(int c, register FILE *stream)
18 {
19         __STDIO_STREAM_VALIDATE(stream);
20
21         /* First the fast path.  We're good to go if putc macro enabled. */
22         if (__STDIO_STREAM_CAN_USE_BUFFER_ADD(stream)) {
23                 __STDIO_STREAM_BUFFER_ADD(stream, ((unsigned char) c));
24                 return (unsigned char) c;
25         }
26
27         /* Next quickest... writing and narrow oriented, but macro
28          * disabled and/or buffer is full. */
29         if (__STDIO_STREAM_IS_NARROW_WRITING(stream)
30                 || !__STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_NARROW)
31                 ) {
32                 if (__STDIO_STREAM_IS_FAKE_VSNPRINTF(stream)) {
33                         return (unsigned char) c;
34                 }
35
36                 if (__STDIO_STREAM_BUFFER_SIZE(stream)) { /* Do we have a buffer? */
37                         /* The buffer is full and/or the stream is line buffered. */
38                         if (!__STDIO_STREAM_BUFFER_WAVAIL(stream) /* Buffer full? */
39                                 && __STDIO_COMMIT_WRITE_BUFFER(stream) /* Commit failed! */
40                                 ) {
41                                 goto BAD;
42                         }
43 #ifdef __UCLIBC_MJN3_ONLY__
44 #warning CONSIDER: Should we fail if the commit fails but we now have room?
45 #endif
46
47                         __STDIO_STREAM_BUFFER_ADD(stream, ((unsigned char) c));
48
49                         if (__STDIO_STREAM_IS_LBF(stream)) {
50                                 if ((((unsigned char) c) == '\n')
51                                         && __STDIO_COMMIT_WRITE_BUFFER(stream)) {
52                                         /* Commit failed! */
53                                         __STDIO_STREAM_BUFFER_UNADD(stream); /* Undo the write! */
54                                         goto BAD;
55                                 }
56                         }
57                 } else {
58                         /* NOTE: Do not try to save space by moving uc to the top of
59                          * the file, as that dramaticly increases runtime. */
60                         unsigned char uc = (unsigned char) c;
61                         if (! __stdio_WRITE(stream, &uc, 1)) {
62                                 goto BAD;
63                         }
64                 }
65                 return (unsigned char) c;
66         }
67
68  BAD:
69         return EOF;
70 }
71 libc_hidden_proto(__fputc_unlocked)
72 libc_hidden_def(__fputc_unlocked)
73
74 strong_alias(__fputc_unlocked,fputc_unlocked)
75
76 strong_alias(__fputc_unlocked,putc_unlocked)
77 libc_hidden_proto(putc_unlocked)
78 libc_hidden_def(putc_unlocked)
79 #ifndef __UCLIBC_HAS_THREADS__
80 strong_alias(__fputc_unlocked,fputc)
81
82 strong_alias(__fputc_unlocked,putc)
83 libc_hidden_proto(putc)
84 libc_hidden_def(putc)
85 #endif
86
87 #elif defined __UCLIBC_HAS_THREADS__
88
89 libc_hidden_proto(__fputc_unlocked)
90
91 int fputc(int c, register FILE *stream)
92 {
93         if (stream->__user_locking != 0) {
94                 return __PUTC_UNLOCKED_MACRO(c, stream);
95         } else {
96                 int retval;
97                 __STDIO_ALWAYS_THREADLOCK(stream);
98                 retval = __PUTC_UNLOCKED_MACRO(c, stream);
99                 __STDIO_ALWAYS_THREADUNLOCK(stream);
100                 return retval;
101         }
102 }
103 libc_hidden_proto(fputc)
104 libc_hidden_def(fputc)
105
106 strong_alias(fputc,putc)
107 libc_hidden_proto(putc)
108 libc_hidden_def(putc)
109
110 #endif