fflush.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. libc_hidden_proto(fflush_unlocked)
  9. #ifdef __DO_UNLOCKED
  10. #ifdef __UCLIBC_MJN3_ONLY__
  11. #warning WISHLIST: Add option to test for undefined behavior of fflush.
  12. #endif /* __UCLIBC_MJN3_ONLY__ */
  13. #ifdef __UCLIBC_HAS_THREADS__
  14. /* Even if the stream is set to user-locking, we still need to lock
  15. * when all (lbf) writing streams are flushed. */
  16. #define MY_STDIO_THREADLOCK(STREAM) \
  17. if (_stdio_user_locking != 2) { \
  18. __STDIO_ALWAYS_THREADLOCK(STREAM); \
  19. }
  20. #define MY_STDIO_THREADUNLOCK(STREAM) \
  21. if (_stdio_user_locking != 2) { \
  22. __STDIO_ALWAYS_THREADUNLOCK(STREAM); \
  23. }
  24. #else
  25. #define MY_STDIO_THREADLOCK(STREAM) ((void)0)
  26. #define MY_STDIO_THREADUNLOCK(STREAM) ((void)0)
  27. #endif
  28. int fflush_unlocked(register FILE *stream)
  29. {
  30. #ifdef __STDIO_BUFFERS
  31. int retval = 0;
  32. #ifdef __UCLIBC_MJN3_ONLY__
  33. #warning REMINDER: should probably define a modeflags type
  34. #endif
  35. unsigned short bufmask = __FLAG_LBF;
  36. #ifndef NDEBUG
  37. if ((stream != NULL) && (stream != (FILE *) &_stdio_openlist)) {
  38. __STDIO_STREAM_VALIDATE(stream); /* debugging only */
  39. }
  40. #endif
  41. if (stream == (FILE *) &_stdio_openlist) { /* Flush all lbf streams. */
  42. stream = NULL;
  43. bufmask = 0;
  44. }
  45. if (!stream) { /* Flush all (lbf) writing streams. */
  46. __STDIO_THREADLOCK_OPENLIST;
  47. for (stream = _stdio_openlist; stream ; stream = stream->__nextopen) {
  48. MY_STDIO_THREADLOCK(stream);
  49. if (!(((stream->__modeflags | bufmask)
  50. ^ (__FLAG_WRITING|__FLAG_LBF)
  51. ) & (__FLAG_WRITING|__MASK_BUFMODE))
  52. ) {
  53. if (!__STDIO_COMMIT_WRITE_BUFFER(stream)) {
  54. __STDIO_STREAM_DISABLE_PUTC(stream);
  55. __STDIO_STREAM_CLEAR_WRITING(stream);
  56. } else {
  57. retval = EOF;
  58. }
  59. }
  60. MY_STDIO_THREADUNLOCK(stream);
  61. }
  62. __STDIO_THREADUNLOCK_OPENLIST;
  63. } else if (__STDIO_STREAM_IS_WRITING(stream)) {
  64. if (!__STDIO_COMMIT_WRITE_BUFFER(stream)) {
  65. __STDIO_STREAM_DISABLE_PUTC(stream);
  66. __STDIO_STREAM_CLEAR_WRITING(stream);
  67. } else {
  68. retval = EOF;
  69. }
  70. }
  71. #if 0
  72. else if (stream->__modeflags & (__MASK_READING|__FLAG_READONLY)) {
  73. /* ANSI/ISO says behavior in this case is undefined but also says you
  74. * shouldn't flush a stream you were reading from. As usual, glibc
  75. * caters to broken programs and simply ignores this. */
  76. __UNDEFINED_OR_NONPORTABLE;
  77. __STDIO_STREAM_SET_ERROR(stream);
  78. __set_errno(EBADF);
  79. retval = EOF;
  80. }
  81. #endif
  82. #ifndef NDEBUG
  83. if ((stream != NULL) && (stream != (FILE *) &_stdio_openlist)) {
  84. __STDIO_STREAM_VALIDATE(stream); /* debugging only */
  85. }
  86. #endif
  87. return retval;
  88. #else /* __STDIO_BUFFERS --------------------------------------- */
  89. #ifndef NDEBUG
  90. if ((stream != NULL)
  91. #ifdef __STDIO_HAS_OPENLIST
  92. && (stream != (FILE *) &_stdio_openlist)
  93. #endif
  94. ) {
  95. __STDIO_STREAM_VALIDATE(stream); /* debugging only */
  96. }
  97. #endif
  98. #if 0
  99. if (stream && (stream->__modeflags & (__MASK_READING|__FLAG_READONLY))) {
  100. /* ANSI/ISO says behavior in this case is undefined but also says you
  101. * shouldn't flush a stream you were reading from. As usual, glibc
  102. * caters to broken programs and simply ignores this. */
  103. __UNDEFINED_OR_NONPORTABLE;
  104. __STDIO_STREAM_SET_ERROR(stream);
  105. __set_errno(EBADF);
  106. return EOF;
  107. }
  108. #endif
  109. return 0;
  110. #endif /* __STDIO_BUFFERS */
  111. }
  112. libc_hidden_def(fflush_unlocked)
  113. #ifndef __UCLIBC_HAS_THREADS__
  114. libc_hidden_proto(fflush)
  115. strong_alias(fflush_unlocked,fflush)
  116. libc_hidden_def(fflush)
  117. #endif
  118. #elif defined __UCLIBC_HAS_THREADS__
  119. libc_hidden_proto(fflush)
  120. int fflush(register FILE *stream)
  121. {
  122. int retval;
  123. __STDIO_AUTO_THREADLOCK_VAR;
  124. if (stream
  125. #ifdef __STDIO_HAS_OPENLIST
  126. && (stream != (FILE *) &_stdio_openlist)
  127. #endif
  128. ) {
  129. __STDIO_AUTO_THREADLOCK(stream);
  130. retval = fflush_unlocked(stream);
  131. __STDIO_AUTO_THREADUNLOCK(stream);
  132. } else {
  133. retval = fflush_unlocked(stream);
  134. }
  135. return retval;
  136. }
  137. libc_hidden_def(fflush)
  138. #endif