fflush.c 4.0 KB

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