Bug 31532 - INQUIRE(...,POSITION=...) not standard conforming
Summary: INQUIRE(...,POSITION=...) not standard conforming
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: 4.3.0
Assignee: Jerry DeLisle
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-04-10 21:34 UTC by Harald Anlauf
Modified: 2007-04-28 03:13 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-04-14 00:40:43


Attachments
Test program (524 bytes, text/plain)
2007-04-10 21:35 UTC, Harald Anlauf
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Harald Anlauf 2007-04-10 21:34:41 UTC
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.
Comment 1 Harald Anlauf 2007-04-10 21:35:53 UTC
Created attachment 13351 [details]
Test program
Comment 2 Francois-Xavier Coudert 2007-04-13 15:24:36 UTC
(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.
Comment 3 Jerry DeLisle 2007-04-14 00:40:43 UTC
I have not a chance to study this yet, but I will get to it sometime soon.
Comment 4 Jerry DeLisle 2007-04-14 03:10:01 UTC
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.
Comment 5 patchapp@dberlin.org 2007-04-22 18:20:15 UTC
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
Comment 6 Jerry DeLisle 2007-04-28 03:03:36 UTC
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

Comment 7 Jerry DeLisle 2007-04-28 03:04:47 UTC
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

Comment 8 Jerry DeLisle 2007-04-28 03:13:40 UTC
Fixed on trunk.