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 
16 #ifdef __DO_UNLOCKED
17 
__fputc_unlocked(int c,register FILE * stream)18 int __fputc_unlocked(int c, register FILE *stream)
19 {
20 	__STDIO_STREAM_VALIDATE(stream);
21 
22 	/* First the fast path.  We're good to go if putc macro enabled. */
23 	if (__STDIO_STREAM_CAN_USE_BUFFER_ADD(stream)) {
24 		__STDIO_STREAM_BUFFER_ADD(stream, ((unsigned char) c));
25 		return (unsigned char) c;
26 	}
27 
28 	/* Next quickest... writing and narrow oriented, but macro
29 	 * disabled and/or buffer is full. */
30 	if (__STDIO_STREAM_IS_NARROW_WRITING(stream)
31 		|| !__STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_NARROW)
32 		) {
33 		if (__STDIO_STREAM_IS_FAKE_VSNPRINTF(stream)) {
34 			return (unsigned char) c;
35 		}
36 
37 		if (__STDIO_STREAM_BUFFER_SIZE(stream)) { /* Do we have a buffer? */
38 			/* The buffer is full and/or the stream is line buffered. */
39 			if (!__STDIO_STREAM_BUFFER_WAVAIL(stream) /* Buffer full? */
40 				&& __STDIO_COMMIT_WRITE_BUFFER(stream) /* Commit failed! */
41 				) {
42 				goto BAD;
43 			}
44 #ifdef __UCLIBC_MJN3_ONLY__
45 #warning CONSIDER: Should we fail if the commit fails but we now have room?
46 #endif
47 
48 			__STDIO_STREAM_BUFFER_ADD(stream, ((unsigned char) c));
49 
50 			if (__STDIO_STREAM_IS_LBF(stream)) {
51 				if ((((unsigned char) c) == '\n')
52 					&& __STDIO_COMMIT_WRITE_BUFFER(stream)) {
53 					/* Commit failed! */
54 					__STDIO_STREAM_BUFFER_UNADD(stream); /* Undo the write! */
55 					goto BAD;
56 				}
57 			}
58 		} else {
59 			/* NOTE: Do not try to save space by moving uc to the top of
60 			 * the file, as that dramaticly increases runtime. */
61 			unsigned char uc = (unsigned char) c;
62 			if (! __stdio_WRITE(stream, &uc, 1)) {
63 				goto BAD;
64 			}
65 		}
66 		return (unsigned char) c;
67 	}
68 
69  BAD:
70 	return EOF;
71 }
72 libc_hidden_def(__fputc_unlocked)
73 
74 strong_alias(__fputc_unlocked,fputc_unlocked)
75 
76 strong_alias(__fputc_unlocked,putc_unlocked)
77 #ifndef __UCLIBC_HAS_THREADS__
78 strong_alias(__fputc_unlocked,fputc)
79 libc_hidden_def(fputc)
80 
81 strong_alias(__fputc_unlocked,putc)
82 #endif
83 
84 #elif defined __UCLIBC_HAS_THREADS__
85 
86 int fputc(int c, register FILE *stream)
87 {
88 	if (stream->__user_locking != 0) {
89 		return __PUTC_UNLOCKED_MACRO(c, stream);
90 	} else {
91 		int retval;
92 		__STDIO_ALWAYS_THREADLOCK(stream);
93 		retval = __PUTC_UNLOCKED_MACRO(c, stream);
94 		__STDIO_ALWAYS_THREADUNLOCK(stream);
95 		return retval;
96 	}
97 }
98 libc_hidden_def(fputc)
99 
100 strong_alias(fputc,putc)
101 
102 #endif
103