Hi, the attached code suggests that the implementation does not conform to the F2003 standard w.r.t. the POSITION= specifier. The standard says: 9.9.1.22 POSITION= specifier in the INQUIRE statement [...] If the file has been repositioned since the connection, the scalar-default-char-variable is assigned a processor-dependent value, which shall not be REWIND unless the file is positioned at its initial point and shall not be APPEND unless the file is positioned so that its endfile record is the next record or at its terminal point if it has no endfile record. In my interpretation of the above wording, when positioned at the endfile record, the result should be APPEND. The attached program shows that the current libgfortran returns ASIS in this case. REWIND obviously works correctly. The test program generates and then reads a file with a single line. After reaching the end of file, it rewinds and reads again. The output is: Generating test file containing one text line Open file inquire: position = ASIS nextrec = 1 Reading... Read line 1 : iostat = 0 inquire: position = ASIS nextrec = 2 Read line 2 : iostat = -1 inquire: position = ASIS nextrec = 2 Rewind file inquire: position = REWIND nextrec = 1 Reading... Read line 1 : iostat = 0 inquire: position = ASIS nextrec = 2 Read line 2 : iostat = -1 inquire: position = ASIS nextrec = 2 In both cases, after attempting to read line 2, the INQUIRE should return position = APPEND instead of ASIS.
Created attachment 13351 [details] Test program
(In reply to comment #0) > If the file has been repositioned since the connection, the > scalar-default-char-variable is assigned a processor-dependent value, > which shall not be REWIND unless the file is positioned at its > initial point and shall not be APPEND unless the file is positioned > so that its endfile record is the next record or at its terminal > point if it has no endfile record. I'm not too skilled at reading normative language, but I guess it only says that if we're not at endfile, if can't be APPEND. It doesn't say that when we're at enfile, it shall be APPEND. Of course, there also is the quality of implementation issue to consider.
I have not a chance to study this yet, but I will get to it sometime soon.
My interpretation of the standard as quoted in the original post. The value of POSITION can only be APPEND if we are positioned just before the EOF record or if there is no EOF record and the position is at it's terminating point. In the case given, there is an EOF record, so that rules that part out. The question then is after the first read, what is the position? The position after the first read is just before the EOF record. So the POSITION in this case should be APPEND. To fix this, we can have the INQUIRE statement attempt to read one more byte. If it is successful, we back up one byte and report ASIS. If we get an EOF trying to read the one more byte, the we report APPEND.
Subject: Bug number PR31532 A patch for this bug has been added to the patch tracker. The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-04/msg01415.html
Subject: Bug 31532 Author: jvdelisle Date: Sat Apr 28 03:03:21 2007 New Revision: 124252 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=124252 Log: 2007-04-27 Jerry DeLisle <jvdelisle@gcc.gnu.org> PR libfortran/31532 * io/file_pos.c (st_backspace): Set flags.position for end of file condition and use new function update_position. (st_endfile): Use new function update_position. * io/io.h: Add prototype for new function. * io/inquire.c (inquire_via_unit): If not direct access, set NEXTREC to zero. * io/unit.c (update_position): New function to update position info used by inquire. * io/transfer.c (next_record): Fix typo and use new function. Modified: trunk/libgfortran/ChangeLog trunk/libgfortran/io/file_pos.c trunk/libgfortran/io/inquire.c trunk/libgfortran/io/io.h trunk/libgfortran/io/transfer.c trunk/libgfortran/io/unit.c
Subject: Bug 31532 Author: jvdelisle Date: Sat Apr 28 03:04:35 2007 New Revision: 124254 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=124254 Log: 2007-04-27 Jerry DeLisle PR libfortran/31532 * gfortran.dg/inquire_5.f90: Update test. Modified: trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/gfortran.dg/inquire_5.f90
Fixed on trunk.