This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC 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]

[Bug fortran/44698] I/O: FLUSH does not actually flush the buffer?



------- Comment #2 from jb at gcc dot gnu dot org  2010-06-28 17:27 -------
(In reply to comment #0)
> Question 1: Why does gfortran always use "stat". Wouldn't it be better to use
> for open files:
>   save = ftell(s)
>   seek(s, 0, SEEK_END)
>   size = ftell(s)
>   seek (s, save, SEEK_SET

Yes, this would work, and we actually already have an implementation for this
(see unix.c:file_length() ), and it might be better because one doesn't need to
flush the buffer which one does need to do in order to get correct size results
from stat(). That being said, I don't think inquiring for file size is such a
common operation that flushing the buffers when this happens is an issue to
worry about.

> Question 2: I fail to see how FLUSH actually works. It seems to work most of
> the time, but I probably miss something. I somehow had expected to see a call
> to fflush - but I only see this for STDIN/STDOUT/STDERR. For the others, I
> find:
> 
>   raw_flush  ==  return 0;
>   buf_flush: Some action, including lseek and raw_write (= unistd.h's write)
> 
> The function is called via sflush. And the FLUSH statement is file_pos.c's
> st_flush:
> 
>   library_start (&fpp->common);
>   u = find_unit (fpp->common.unit);
>   if (u != NULL)
>     {
>       if (u->flags.form == FORM_FORMATTED)
>         fbuf_flush (u, u->mode);
>       sflush (u->s);
>       unlock_unit (u);
>     } [...]
>   library_end ();
> 
> Thus: I fail to see any fflush call for non-std(out/err/in) units. Did I miss
> something?

See unix.c:buf_flush(). Called via the sflush() inline function/function
pointer thingy. libgfortran does not use C stdio
(fread/fwrite/fflush/fseek/...), but rather the lower level unbuffered POSIX IO
(read/write/lseek), with its own buffering implementation in
libgfortran/io/unix.c, see the buf_* functions.

I suspect the fflush() calls are there so that programs which mix Fortran and C
I/O to stdio/our/err work correctly (or well, hopefully at least better). But
the fflush() calls are not needed for pure gfortran IO per se.

As to the original issue, I have no idea why it fails on MinGW, and I don't
have a windows installation to test on either.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44698


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