fputc.c 2.4 KB

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