fputc.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. weak_alias(__fputc_unlocked,fputc_unlocked);
  14. weak_alias(__fputc_unlocked,putc_unlocked);
  15. #ifndef __UCLIBC_HAS_THREADS__
  16. weak_alias(__fputc_unlocked,fputc);
  17. weak_alias(__fputc_unlocked,putc);
  18. #endif
  19. int __fputc_unlocked(int c, register FILE *stream)
  20. {
  21. __STDIO_STREAM_VALIDATE(stream);
  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. /* Next quickest... writing and narrow oriented, but macro
  28. * disabled and/or buffer is full. */
  29. if (__STDIO_STREAM_IS_NARROW_WRITING(stream)
  30. || !__STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_NARROW)
  31. ) {
  32. if (__STDIO_STREAM_IS_FAKE_VSNPRINTF(stream)) {
  33. return (unsigned char) c;
  34. }
  35. if (__STDIO_STREAM_BUFFER_SIZE(stream)) { /* Do we have a buffer? */
  36. /* The buffer is full and/or the stream is line buffered. */
  37. if (!__STDIO_STREAM_BUFFER_WAVAIL(stream) /* Buffer full? */
  38. && __STDIO_COMMIT_WRITE_BUFFER(stream) /* Commit failed! */
  39. ) {
  40. goto BAD;
  41. }
  42. #ifdef __UCLIBC_MJN3_ONLY__
  43. #warning CONSIDER: Should we fail if the commit fails but we now have room?
  44. #endif
  45. __STDIO_STREAM_BUFFER_ADD(stream, ((unsigned char) c));
  46. if (__STDIO_STREAM_IS_LBF(stream)) {
  47. if ((((unsigned char) c) == '\n')
  48. && __STDIO_COMMIT_WRITE_BUFFER(stream)) {
  49. /* Commit failed! */
  50. __STDIO_STREAM_BUFFER_UNADD(stream); /* Undo the write! */
  51. goto BAD;
  52. }
  53. }
  54. } else {
  55. /* NOTE: Do not try to save space by moving uc to the top of
  56. * the file, as that dramaticly increases runtime. */
  57. unsigned char uc = (unsigned char) c;
  58. if (! __stdio_WRITE(stream, &uc, 1)) {
  59. goto BAD;
  60. }
  61. }
  62. return (unsigned char) c;
  63. }
  64. BAD:
  65. return EOF;
  66. }
  67. #elif defined __UCLIBC_HAS_THREADS__
  68. weak_alias(fputc,putc);
  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. #endif