The attached patch is very simple. This provides for the case where a UNIT or FILE is connected as unformatted vs formatted.

Minor adjustment to existing test case.

Regression tested on x86_64

Thanks Walter for spotting this.

I will commit this later tonight.

Regards,

Jerry

Author: Jerry DeLisle <[email protected]>
Date:   Fri Apr 3 10:17:03 2026 -0700

    fortran: Minor adjustment to INQUIRE for ENCODING=

            PR libfortran/124543

    libgfortran/ChangeLog:

            * io/inquire.c (inquire_via_unit): Adjust the logic to
            treat formatted vs unformatted correctly.

    gcc/testsuite/ChangeLog:

            * gfortran.dg/pr124543.f90: Set result to 'UNDEFINED' in two
            places.
commit 841f1422c1e6f5b09968d453e49950ac65993955
Author: Jerry DeLisle <[email protected]>
Date:   Fri Apr 3 10:17:03 2026 -0700

    fortran: Minor adjustment to INQUIRE for ENCODING=
    
            PR libfortran/124543
    
    libgfortran/ChangeLog:
    
            * io/inquire.c (inquire_via_unit): Adjust the logic to
            treat formatted vs unformatted correctly.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/pr124543.f90: Set result to 'UNDEFINED' in two
            places.

diff --git a/gcc/testsuite/gfortran.dg/pr124543.f90 b/gcc/testsuite/gfortran.dg/pr124543.f90
index 7a7f15dd391..2ab5b5a023b 100644
--- a/gcc/testsuite/gfortran.dg/pr124543.f90
+++ b/gcc/testsuite/gfortran.dg/pr124543.f90
@@ -202,7 +202,7 @@ program inq_tests
   inquire (unit=lun, decimal=decimal, encoding=encoding, pos=pos,  &
       round=round, sign=signc, stream=stream, iostat=iostat)
   if  (decimal /= 'POINT' .or. (iostat /= 0)) stop 204
-  if  (encoding /= 'UNKNOWN') stop 205
+  if  (encoding /= 'UNDEFINED') stop 205
   if  (round /= 'PROCESSOR_DEFINED') stop 206
   if  (signc /= 'PROCESSOR_DEFINED') stop 207
   if  (stream /= 'NO') stop 208
@@ -284,7 +284,7 @@ program inq_tests
   inquire (unit=lun, decimal=decimal, encoding=encoding, pos=pos,  &
       round=round, sign=signc, stream=stream, iostat=iostat)
   if  (decimal /= 'UNDEFINED' .or. (iostat /= 0)) stop 286
-  if  (encoding /= 'UNKNOWN') stop 287
+  if  (encoding /= 'UNDEFINED') stop 287
   if  (round /= 'PROCESSOR_DEFINED') stop 288
   if  (signc /= 'PROCESSOR_DEFINED') stop 289
   if  (stream /= 'YES') stop 290
diff --git a/libgfortran/io/inquire.c b/libgfortran/io/inquire.c
index f96210f1d57..4d773349fa3 100644
--- a/libgfortran/io/inquire.c
+++ b/libgfortran/io/inquire.c
@@ -311,13 +311,13 @@ inquire_via_unit (st_parameter_inquire *iqp, gfc_unit *u)
 
       if ((cf2 & IOPARM_INQUIRE_HAS_ENCODING) != 0)
 	{
-	  if (u == NULL || u->flags.form != FORM_FORMATTED)
+	  if (u == NULL)
 	    p = "UNKNOWN";
-          else
+	  else if (u->flags.form == FORM_FORMATTED)
 	    switch (u->flags.encoding)
 	      {
 	      case ENCODING_DEFAULT:
-		p = "UNKNOWN";
+		p = "UNDEFINED";
 		break;
 	      case ENCODING_UTF8:
 		p = "UTF-8";
@@ -325,6 +325,8 @@ inquire_via_unit (st_parameter_inquire *iqp, gfc_unit *u)
 	      default:
 		internal_error (&iqp->common, "inquire_via_unit(): Bad encoding");
 	      }
+	    else
+	      p = "UNKNOWN";
 
 	  cf_strcpy (iqp->encoding, iqp->encoding_len, p);
 	}

Reply via email to