fputc.c 2.9 KB

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