Bug 23262 - [mingw32] rewind truncates file
Summary: [mingw32] rewind truncates file
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.1.0
: P2 normal
Target Milestone: 4.0.3
Assignee: Francois-Xavier Coudert
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: patch
: 23264 23483 (view as bug list)
Depends on:
Blocks: 23261
  Show dependency treegraph
 
Reported: 2005-08-06 19:46 UTC by Francois-Xavier Coudert
Modified: 2005-11-13 06:39 UTC (History)
2 users (show)

See Also:
Host:
Target: i386-mingw32
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-09-04 06:29:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Francois-Xavier Coudert 2005-08-06 19:46:01 UTC
$ cat endfile.f90 
      integer i
      open(unit=10,file='test.dat')
      do i=1,5
        write (10,'(I4)') i
      end do
      rewind(10)
      close(10)
      end
$ gfc endfile.f90 && a && cat test.dat
   1
   2
   3
   4
$ xxd test.dat
0000000: 2020 2031 0d0a 2020 2032 0d0a 2020 2033     1..   2..   3
0000010: 0d0a 2020 2034 0d0a 20                   ..   4..


If you remove the rewind statement, the program behaves correctly, that is we
have a fifth line with "   5" in it.
Comment 1 Andrew Pinski 2005-08-07 15:17:12 UTC
Works correctly on i686-pc-linux-gnu.
Comment 2 Francois-Xavier Coudert 2005-09-03 08:46:02 UTC
Well, that was not so difficult: when we write "1234\n6789\n" for example, the
length of the write is 10. But the size of the resulting file is 12, since each
newline character is translated into a two-byte \n\r sequence! Then, the first
step of the rewind is to truncate the file to length 10.

I don't know exactly how we should handle this :(
Comment 3 Francois-Xavier Coudert 2005-09-03 10:15:41 UTC
OK, it's worse than I thought. When you open a file with O_BINARY, it is opened
using Unix conventions, and you need to add yourself the \r in the right places.

Otherwise, you can use the Windows CRLF convention, but then lseek and ftruncate
are *not* usable. Not at all.
Comment 4 Francois-Xavier Coudert 2005-09-03 19:09:00 UTC
*** Bug 23264 has been marked as a duplicate of this bug. ***
Comment 5 Francois-Xavier Coudert 2005-09-04 06:29:00 UTC
Patch waiting for some feedback about its interaction with cygwin.
Comment 6 GCC Commits 2005-09-07 21:25:48 UTC
Subject: Bug 23262

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	fxcoudert@gcc.gnu.org	2005-09-07 21:25:40

Modified files:
	libgfortran    : ChangeLog acinclude.m4 config.h.in configure 
	                 configure.ac 
	libgfortran/io : transfer.c unix.c 

Log message:
	PR libfortran/23262
	* acinclude.m4 (LIBGFOR_CHECK_CRLF): New check.
	* configure.ac: Use new check.
	* configure.in: Regenerate.
	* config.h.in: Regenerate.
	* configure: Regenerate.
	* io/transfer.c (next_record_w): Add case for CRLF as line
	terminator.
	* io/unix.c (tempfile, regular_file): Open files with
	O_BINARY on systems with CRLF.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/ChangeLog.diff?cvsroot=gcc&r1=1.296&r2=1.297
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/acinclude.m4.diff?cvsroot=gcc&r1=1.6&r2=1.7
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/config.h.in.diff?cvsroot=gcc&r1=1.24&r2=1.25
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/configure.diff?cvsroot=gcc&r1=1.42&r2=1.43
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/configure.ac.diff?cvsroot=gcc&r1=1.32&r2=1.33
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/transfer.c.diff?cvsroot=gcc&r1=1.56&r2=1.57
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/unix.c.diff?cvsroot=gcc&r1=1.36&r2=1.37

Comment 7 GCC Commits 2005-09-07 21:32:09 UTC
Subject: Bug 23262

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-4_0-branch
Changes by:	fxcoudert@gcc.gnu.org	2005-09-07 21:31:57

Modified files:
	libgfortran    : ChangeLog acinclude.m4 config.h.in configure 
	                 configure.ac 
	libgfortran/io : transfer.c unix.c 

Log message:
	PR libfortran/23262
	* acinclude.m4 (LIBGFOR_CHECK_CRLF): New check.
	* configure.ac: Use new check.
	* configure.in: Regenerate.
	* config.h.in: Regenerate.
	* configure: Regenerate.
	* io/transfer.c (next_record_w): Add case for CRLF as line
	terminator.
	* io/unix.c (tempfile, regular_file): Open files with
	O_BINARY on systems with CRLF.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.163.2.88&r2=1.163.2.89
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/acinclude.m4.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.5.12.1&r2=1.5.12.2
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/config.h.in.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.16.12.6&r2=1.16.12.7
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/configure.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.26.12.8&r2=1.26.12.9
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/configure.ac.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.20.12.8&r2=1.20.12.9
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/transfer.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.32.2.15&r2=1.32.2.16
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/unix.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.21.10.13&r2=1.21.10.14

Comment 8 Francois-Xavier Coudert 2005-09-07 21:33:16 UTC
Patch commited, bug fixed.
Comment 9 Francois-Xavier Coudert 2005-09-08 20:50:43 UTC
*** Bug 23483 has been marked as a duplicate of this bug. ***