fputc.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 attribute_hidden __fputc_unlocked_internal(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. #ifdef __UCLIBC_MJN3_ONLY__
  37. #warning CONSIDER: Should we fail if the commit fails but we now have room?
  38. #endif
  39. __STDIO_STREAM_BUFFER_ADD(stream, ((unsigned char) c));
  40. if (__STDIO_STREAM_IS_LBF(stream)) {
  41. if ((((unsigned char) c) == '\n')
  42. && __STDIO_COMMIT_WRITE_BUFFER(stream)) {
  43. /* Commit failed! */
  44. __STDIO_STREAM_BUFFER_UNADD(stream); /* Undo the write! */
  45. goto BAD;
  46. }
  47. }
  48. } else {
  49. /* NOTE: Do not try to save space by moving uc to the top of
  50. * the file, as that dramaticly increases runtime. */
  51. unsigned char uc = (unsigned char) c;
  52. if (! __stdio_WRITE(stream, &uc, 1)) {
  53. goto BAD;
  54. }
  55. }
  56. return (unsigned char) c;
  57. }
  58. BAD:
  59. return EOF;
  60. }
  61. strong_alias(__fputc_unlocked_internal,__fputc_unlocked)
  62. weak_alias(__fputc_unlocked_internal,fputc_unlocked)
  63. weak_alias(__fputc_unlocked_internal,putc_unlocked)
  64. #ifndef __UCLIBC_HAS_THREADS__
  65. weak_alias(__fputc_unlocked_internal,fputc)
  66. weak_alias(__fputc_unlocked_internal,putc)
  67. #endif
  68. #elif defined __UCLIBC_HAS_THREADS__
  69. int fputc(int c, register FILE *stream)
  70. {
  71. if (stream->__user_locking != 0) {
  72. return __PUTC_UNLOCKED_MACRO(c, stream);
  73. } else {
  74. int retval;
  75. __STDIO_ALWAYS_THREADLOCK(stream);
  76. retval = __PUTC_UNLOCKED_MACRO(c, stream);
  77. __STDIO_ALWAYS_THREADUNLOCK(stream);
  78. return retval;
  79. }
  80. }
  81. weak_alias(fputc,putc)
  82. #endif