fflush.c 3.9 KB

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