lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 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 | |
| 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 | libc_hidden_def(fputc_unlocked) |
| 76 | |
| 77 | strong_alias(__fputc_unlocked,putc_unlocked) |
| 78 | libc_hidden_def(putc_unlocked) |
| 79 | #ifndef __UCLIBC_HAS_THREADS__ |
| 80 | strong_alias(__fputc_unlocked,fputc) |
| 81 | libc_hidden_def(fputc) |
| 82 | |
| 83 | strong_alias(__fputc_unlocked,putc) |
| 84 | libc_hidden_def(putc) |
| 85 | #endif |
| 86 | |
| 87 | #elif defined __UCLIBC_HAS_THREADS__ |
| 88 | |
| 89 | int fputc(int c, register FILE *stream) |
| 90 | { |
| 91 | if (stream->__user_locking != 0) { |
| 92 | return __PUTC_UNLOCKED_MACRO(c, stream); |
| 93 | } else { |
| 94 | int retval; |
| 95 | __STDIO_ALWAYS_THREADLOCK(stream); |
| 96 | retval = __PUTC_UNLOCKED_MACRO(c, stream); |
| 97 | __STDIO_ALWAYS_THREADUNLOCK(stream); |
| 98 | return retval; |
| 99 | } |
| 100 | } |
| 101 | libc_hidden_def(fputc) |
| 102 | |
| 103 | strong_alias(fputc,putc) |
| 104 | libc_hidden_def(putc) |
| 105 | |
| 106 | #endif |