This is the mail archive of the ecos-patches@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

fflush not always flushing


fflush() does not always flush. This little program demonstrates the
problem:

#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
 
int main(int argc, char * argv[])
{
  int fd;
  FILE *File;
  
  fd = open ("/dev/haldiag",O_RDWR);
 
  File = fdopen(fd,"r+");
  
  fprintf(File, "hello world");
  
  fflush(File);
 
  return 0;
  
}

fdopen on an interactive device sets up the stream to not do
buffering. ie flags.buffering is false. When fprintf does its write to
the stream, it passes the output straight down to the next level.
When fflush is called, it checks if there is anything in the stream
buffer. If there is, it writes it and then call the flush on the level
below. Now since the output of fprintf went straight down, the buffer
is empty and so the flush is not done and the output can get stuck. 

This patch fixes the problem.

     Andrew

Index: language/c/libc//stdio/current/ChangeLog
===================================================================
RCS file: /cvs/ecos/ecos/packages/language/c/libc/stdio/current/ChangeLog,v
retrieving revision 1.17
diff -u -r1.17 ChangeLog
--- language/c/libc//stdio/current/ChangeLog    2 Sep 2002 07:55:37 -0000      1.17
+++ language/c/libc//stdio/current/ChangeLog    6 Sep 2002 17:41:20 -0000
@@ -1,3 +1,8 @@
+2002-09-06  Andrew Lunn  <andrew.lunn@ascom.ch>
+
+       * src/common/stream.cxx (flush_output_unlocked): Flush the layer
+       below even if there nothing buffered at this level.
+
 2002-09-02  Andrew Lunn  <andrew.lunn@ascom.ch>
 
        * include/stream.inl (set_position): Fixed typo in none buffered
Index: language/c/libc//stdio/current/src/common/stream.cxx
===================================================================
RCS file: /cvs/ecos/ecos/packages/language/c/libc/stdio/current/src/common/stream.cxx,v
retrieving revision 1.5
diff -u -r1.5 stream.cxx
--- language/c/libc//stdio/current/src/common/stream.cxx        23 May 2002 23:07:18 -0000      1.5
+++ language/c/libc//stdio/current/src/common/stream.cxx        6 Sep 2002 17:41:20 -0000
@@ -543,16 +543,10 @@
     if ( !flags.opened_for_write )
         return EINVAL;
 
-    // shortcut if nothing to do
-    if (io_buf.get_buffer_space_used() == 0)
-        return ENOERR;
-        
     len = io_buf.get_buffer_addr_to_read( (cyg_uint8 **)&buffer );
     
-    CYG_ASSERT( len > 0, 
-                "There should be data to read but there isn't!");
-
-    write_err = cyg_stdio_write(my_device, buffer, &len);
+    if ( len )
+      write_err = cyg_stdio_write(my_device, buffer, &len);
 
     // since we're doing a concerted flush, we tell the I/O layer to
     // flush too, otherwise output may just sit there forever


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]