fflush.c 3.8 KB

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