OSDN Git Service

2010-08-14 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / io / inquire.c
index 290f31c..c4994ed 100644 (file)
@@ -1,11 +1,12 @@
-/* Copyright (C) 2002-2003 Free Software Foundation, Inc.
+/* Copyright (C) 2002, 2003, 2005, 2007, 2009, 2010
+   Free Software Foundation, Inc.
    Contributed by Andy Vaught
 
 This file is part of the GNU Fortran 95 runtime library (libgfortran).
 
 Libgfortran is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
 Libgfortran is distributed in the hope that it will be useful,
@@ -13,45 +14,91 @@ but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License
-along with Libgfortran; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+Under Section 7 of GPL version 3, you are granted additional
+permissions described in the GCC Runtime Library Exception, version
+3.1, as published by the Free Software Foundation.
+
+You should have received a copy of the GNU General Public License and
+a copy of the GCC Runtime Library Exception along with this program;
+see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+<http://www.gnu.org/licenses/>.  */
 
 
 /* Implement the non-IOLENGTH variant of the INQUIRY statement */
 
-#include "config.h"
-#include "libgfortran.h"
 #include "io.h"
+#include "unix.h"
+#include <string.h>
 
 
-static char undefined[] = "UNDEFINED";
+static const char undefined[] = "UNDEFINED";
 
 
 /* inquire_via_unit()-- Inquiry via unit number.  The unit might not exist. */
 
 static void
-inquire_via_unit (gfc_unit * u)
+inquire_via_unit (st_parameter_inquire *iqp, gfc_unit * u)
 {
   const char *p;
+  GFC_INTEGER_4 cf = iqp->common.flags;
 
-  if (ioparm.exist != NULL)
-    *ioparm.exist = (u != NULL);
+  if ((cf & IOPARM_INQUIRE_HAS_EXIST) != 0)
+    {
+      *iqp->exist = (iqp->common.unit >= 0
+                    && iqp->common.unit <= GFC_INTEGER_4_HUGE);
 
-  if (ioparm.opened != NULL)
-    *ioparm.opened = (u != NULL);
+      if ((cf & IOPARM_INQUIRE_HAS_FILE) == 0)
+       {
+         if (!(*iqp->exist))
+           *iqp->common.iostat = LIBERROR_BAD_UNIT;
+         *iqp->exist = *iqp->exist
+                       && (*iqp->common.iostat != LIBERROR_BAD_UNIT);
+       }
+    }
 
-  if (ioparm.number != NULL)
-    *ioparm.number = (u != NULL) ? u->unit_number : -1;
+  if ((cf & IOPARM_INQUIRE_HAS_OPENED) != 0)
+    *iqp->opened = (u != NULL);
 
-  if (ioparm.named != NULL)
-    *ioparm.named = (u != NULL && u->flags.status != STATUS_SCRATCH);
+  if ((cf & IOPARM_INQUIRE_HAS_NUMBER) != 0)
+    *iqp->number = (u != NULL) ? u->unit_number : -1;
 
-  if (ioparm.name != NULL && u != NULL && u->flags.status != STATUS_SCRATCH)
-    fstrcpy (ioparm.name, ioparm.name_len, u->file, u->file_len);
+  if ((cf & IOPARM_INQUIRE_HAS_NAMED) != 0)
+    *iqp->named = (u != NULL && u->flags.status != STATUS_SCRATCH);
 
-  if (ioparm.access != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_NAME) != 0
+      && u != NULL && u->flags.status != STATUS_SCRATCH)
+    {
+#ifdef HAVE_TTYNAME
+      if (u->unit_number == options.stdin_unit
+         || u->unit_number == options.stdout_unit
+         || u->unit_number == options.stderr_unit)
+       {
+         char * tmp = ttyname (((unix_stream *) u->s)->fd);
+         if (tmp != NULL)
+           {
+             int tmplen = strlen (tmp);
+             fstrcpy (iqp->name, iqp->name_len, tmp, tmplen);
+           }
+         else /* If ttyname does not work, go with the default.  */
+           fstrcpy (iqp->name, iqp->name_len, u->file, u->file_len);
+       }
+      else
+       fstrcpy (iqp->name, iqp->name_len, u->file, u->file_len);
+#elif defined __MINGW32__
+      if (u->unit_number == options.stdin_unit)
+       fstrcpy (iqp->name, iqp->name_len, "CONIN$", sizeof("CONIN$"));
+      else if (u->unit_number == options.stdout_unit)
+       fstrcpy (iqp->name, iqp->name_len, "CONOUT$", sizeof("CONOUT$"));
+      else if (u->unit_number == options.stderr_unit)
+       fstrcpy (iqp->name, iqp->name_len, "CONERR$", sizeof("CONERR$"));
+      else
+       fstrcpy (iqp->name, iqp->name_len, u->file, u->file_len);
+#else
+    fstrcpy (iqp->name, iqp->name_len, u->file, u->file_len);
+#endif
+    }
+
+  if ((cf & IOPARM_INQUIRE_HAS_ACCESS) != 0)
     {
       if (u == NULL)
        p = undefined;
@@ -64,35 +111,59 @@ inquire_via_unit (gfc_unit * u)
          case ACCESS_DIRECT:
            p = "DIRECT";
            break;
+         case ACCESS_STREAM:
+           p = "STREAM";
+           break;
          default:
-           internal_error ("inquire_via_unit(): Bad access");
+           internal_error (&iqp->common, "inquire_via_unit(): Bad access");
          }
 
-      cf_strcpy (ioparm.access, ioparm.access_len, p);
+      cf_strcpy (iqp->access, iqp->access_len, p);
     }
 
-  if (ioparm.sequential != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_SEQUENTIAL) != 0)
     {
-      /* disallow an open direct access file to be accessed
-         sequentially */
-      if (u->flags.access==ACCESS_DIRECT)
-        p = "NO";
-      else   
-        p = (u == NULL) ? inquire_sequential (NULL, 0) :
-        inquire_sequential (u->file, u->file_len);
-
-      cf_strcpy (ioparm.sequential, ioparm.sequential_len, p);
+      if (u == NULL)
+       p = inquire_sequential (NULL, 0);
+      else
+       switch (u->flags.access)
+         {
+         case ACCESS_DIRECT:
+         case ACCESS_STREAM:
+           p = "NO";
+           break;
+         case ACCESS_SEQUENTIAL:
+           p = "YES";
+           break;
+         default:
+           internal_error (&iqp->common, "inquire_via_unit(): Bad access");
+         }
+
+      cf_strcpy (iqp->sequential, iqp->sequential_len, p);
     }
 
-  if (ioparm.direct != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_DIRECT) != 0)
     {
-      p = (u == NULL) ? inquire_direct (NULL, 0) :
-       inquire_direct (u->file, u->file_len);
+      if (u == NULL)
+       p = inquire_direct (NULL, 0);
+      else
+       switch (u->flags.access)
+         {
+         case ACCESS_SEQUENTIAL:
+         case ACCESS_STREAM:
+           p = "NO";
+           break;
+         case ACCESS_DIRECT:
+           p = "YES";
+           break;
+         default:
+           internal_error (&iqp->common, "inquire_via_unit(): Bad access");
+         }
 
-      cf_strcpy (ioparm.direct, ioparm.direct_len, p);
+      cf_strcpy (iqp->direct, iqp->direct_len, p);
     }
 
-  if (ioparm.form != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_FORM) != 0)
     {
       if (u == NULL)
        p = undefined;
@@ -106,67 +177,268 @@ inquire_via_unit (gfc_unit * u)
            p = "UNFORMATTED";
            break;
          default:
-           internal_error ("inquire_via_unit(): Bad form");
+           internal_error (&iqp->common, "inquire_via_unit(): Bad form");
          }
 
-      cf_strcpy (ioparm.form, ioparm.form_len, p);
+      cf_strcpy (iqp->form, iqp->form_len, p);
     }
 
-  if (ioparm.formatted != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_FORMATTED) != 0)
     {
-      p = (u == NULL) ? inquire_formatted (NULL, 0) :
-       inquire_formatted (u->file, u->file_len);
+      if (u == NULL)
+       p = inquire_formatted (NULL, 0);
+      else
+       switch (u->flags.form)
+         {
+         case FORM_FORMATTED:
+           p = "YES";
+           break;
+         case FORM_UNFORMATTED:
+           p = "NO";
+           break;
+         default:
+           internal_error (&iqp->common, "inquire_via_unit(): Bad form");
+         }
 
-      cf_strcpy (ioparm.formatted, ioparm.formatted_len, p);
+      cf_strcpy (iqp->formatted, iqp->formatted_len, p);
     }
 
-  if (ioparm.unformatted != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_UNFORMATTED) != 0)
     {
-      p = (u == NULL) ? inquire_unformatted (NULL, 0) :
-       inquire_unformatted (u->file, u->file_len);
+      if (u == NULL)
+       p = inquire_unformatted (NULL, 0);
+      else
+       switch (u->flags.form)
+         {
+         case FORM_FORMATTED:
+           p = "NO";
+           break;
+         case FORM_UNFORMATTED:
+           p = "YES";
+           break;
+         default:
+           internal_error (&iqp->common, "inquire_via_unit(): Bad form");
+         }
 
-      cf_strcpy (ioparm.unformatted, ioparm.unformatted_len, p);
+      cf_strcpy (iqp->unformatted, iqp->unformatted_len, p);
     }
 
-  if (ioparm.recl_out != NULL)
-    *ioparm.recl_out = (u != NULL) ? u->recl : 0;
+  if ((cf & IOPARM_INQUIRE_HAS_RECL_OUT) != 0)
+    *iqp->recl_out = (u != NULL) ? u->recl : 0;
 
-  if (ioparm.nextrec != NULL)
-    *ioparm.nextrec = (u != NULL) ? u->last_record + 1 : 0;
+  if ((cf & IOPARM_INQUIRE_HAS_STRM_POS_OUT) != 0)
+    *iqp->strm_pos_out = (u != NULL) ? u->strm_pos : 0;
 
-  if (ioparm.blank != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_NEXTREC) != 0)
     {
-      if (u == NULL)
+      /* This only makes sense in the context of DIRECT access.  */
+      if (u != NULL && u->flags.access == ACCESS_DIRECT)
+       *iqp->nextrec = u->last_record + 1;
+      else
+       *iqp->nextrec = 0;
+    }
+
+  if ((cf & IOPARM_INQUIRE_HAS_BLANK) != 0)
+    {
+      if (u == NULL || u->flags.form != FORM_FORMATTED)
        p = undefined;
       else
        switch (u->flags.blank)
          {
          case BLANK_NULL:
-          p = "NULL";
+           p = "NULL";
            break;
          case BLANK_ZERO:
            p = "ZERO";
            break;
          default:
-           internal_error ("inquire_via_unit(): Bad blank");
+           internal_error (&iqp->common, "inquire_via_unit(): Bad blank");
          }
 
-      cf_strcpy (ioparm.blank, ioparm.blank_len, p);
+      cf_strcpy (iqp->blank, iqp->blank_len, p);
     }
 
-  if (ioparm.position != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_PAD) != 0)
     {
-      if (u == NULL || u->flags.access == ACCESS_DIRECT)
+      if (u == NULL || u->flags.form != FORM_FORMATTED)
        p = undefined;
       else
+       switch (u->flags.pad)
+         {
+         case PAD_YES:
+           p = "YES";
+           break;
+         case PAD_NO:
+           p = "NO";
+           break;
+         default:
+           internal_error (&iqp->common, "inquire_via_unit(): Bad pad");
+         }
+
+      cf_strcpy (iqp->pad, iqp->pad_len, p);
+    }
+
+  if (cf & IOPARM_INQUIRE_HAS_FLAGS2)
+    {
+      GFC_INTEGER_4 cf2 = iqp->flags2;
+
+      if ((cf2 & IOPARM_INQUIRE_HAS_PENDING) != 0)
+       *iqp->pending = 0;
+  
+      if ((cf2 & IOPARM_INQUIRE_HAS_ID) != 0)
+        *iqp->id = 0;
+
+      if ((cf2 & IOPARM_INQUIRE_HAS_ENCODING) != 0)
+       {
+         if (u == NULL || u->flags.form != FORM_FORMATTED)
+           p = undefined;
+          else
+           switch (u->flags.encoding)
+             {
+             case ENCODING_DEFAULT:
+               p = "UNKNOWN";
+               break;
+             case ENCODING_UTF8:
+               p = "UTF-8";
+               break;
+             default:
+               internal_error (&iqp->common, "inquire_via_unit(): Bad encoding");
+             }
+
+         cf_strcpy (iqp->encoding, iqp->encoding_len, p);
+       }
+
+      if ((cf2 & IOPARM_INQUIRE_HAS_DECIMAL) != 0)
+       {
+         if (u == NULL || u->flags.form != FORM_FORMATTED)
+           p = undefined;
+         else
+           switch (u->flags.decimal)
+             {
+             case DECIMAL_POINT:
+               p = "POINT";
+               break;
+             case DECIMAL_COMMA:
+               p = "COMMA";
+               break;
+             default:
+               internal_error (&iqp->common, "inquire_via_unit(): Bad comma");
+             }
+
+         cf_strcpy (iqp->decimal, iqp->decimal_len, p);
+       }
+
+      if ((cf2 & IOPARM_INQUIRE_HAS_ASYNCHRONOUS) != 0)
+       {
+         if (u == NULL)
+           p = undefined;
+         else
+           switch (u->flags.async)
+           {
+             case ASYNC_YES:
+               p = "YES";
+               break;
+             case ASYNC_NO:
+               p = "NO";
+               break;
+             default:
+               internal_error (&iqp->common, "inquire_via_unit(): Bad async");
+           }
+
+         cf_strcpy (iqp->asynchronous, iqp->asynchronous_len, p);
+       }
+
+      if ((cf2 & IOPARM_INQUIRE_HAS_SIGN) != 0)
+       {
+         if (u == NULL)
+           p = undefined;
+         else
+           switch (u->flags.sign)
+           {
+             case SIGN_PROCDEFINED:
+               p = "PROCESSOR_DEFINED";
+               break;
+             case SIGN_SUPPRESS:
+               p = "SUPPRESS";
+               break;
+             case SIGN_PLUS:
+               p = "PLUS";
+               break;
+             default:
+               internal_error (&iqp->common, "inquire_via_unit(): Bad sign");
+           }
+
+         cf_strcpy (iqp->sign, iqp->sign_len, p);
+       }
+
+      if ((cf2 & IOPARM_INQUIRE_HAS_ROUND) != 0)
+       {
+         if (u == NULL)
+           p = undefined;
+         else
+           switch (u->flags.round)
+           {
+             case ROUND_UP:
+               p = "UP";
+               break;
+             case ROUND_DOWN:
+               p = "DOWN";
+               break;
+             case ROUND_ZERO:
+               p = "ZERO";
+               break;
+             case ROUND_NEAREST:
+               p = "NEAREST";
+               break;
+             case ROUND_COMPATIBLE:
+               p = "COMPATIBLE";
+               break;
+             case ROUND_PROCDEFINED:
+               p = "PROCESSOR_DEFINED";
+               break;
+             default:
+               internal_error (&iqp->common, "inquire_via_unit(): Bad round");
+           }
+
+         cf_strcpy (iqp->round, iqp->round_len, p);
+       }
+
+      if ((cf2 & IOPARM_INQUIRE_HAS_SIZE) != 0)
        {
-         p = NULL;             /* TODO: Try to decode what the standard says... */
+         if (u == NULL)
+           *iqp->size = -1;
+         else
+           *iqp->size = file_size (u->file, (gfc_charlen_type) u->file_len);
        }
+    }
 
-      cf_strcpy (ioparm.blank, ioparm.blank_len, p);
+  if ((cf & IOPARM_INQUIRE_HAS_POSITION) != 0)
+    {
+      if (u == NULL || u->flags.access == ACCESS_DIRECT)
+        p = undefined;
+      else
+        switch (u->flags.position)
+          {
+             case POSITION_REWIND:
+               p = "REWIND";
+               break;
+             case POSITION_APPEND:
+               p = "APPEND";
+               break;
+             case POSITION_ASIS:
+               p = "ASIS";
+               break;
+             default:
+               /* if not direct access, it must be
+                  either REWIND, APPEND, or ASIS.
+                  ASIS seems to be the best default */
+               p = "ASIS";
+               break;
+          }
+      cf_strcpy (iqp->position, iqp->position_len, p);
     }
 
-  if (ioparm.action != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_ACTION) != 0)
     {
       if (u == NULL)
        p = undefined;
@@ -183,37 +455,37 @@ inquire_via_unit (gfc_unit * u)
            p = "READWRITE";
            break;
          default:
-           internal_error ("inquire_via_unit(): Bad action");
+           internal_error (&iqp->common, "inquire_via_unit(): Bad action");
          }
 
-      cf_strcpy (ioparm.action, ioparm.action_len, p);
+      cf_strcpy (iqp->action, iqp->action_len, p);
     }
 
-  if (ioparm.read != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_READ) != 0)
     {
       p = (u == NULL) ? inquire_read (NULL, 0) :
        inquire_read (u->file, u->file_len);
 
-      cf_strcpy (ioparm.read, ioparm.read_len, p);
+      cf_strcpy (iqp->read, iqp->read_len, p);
     }
 
-  if (ioparm.write != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_WRITE) != 0)
     {
       p = (u == NULL) ? inquire_write (NULL, 0) :
        inquire_write (u->file, u->file_len);
 
-      cf_strcpy (ioparm.write, ioparm.write_len, p);
+      cf_strcpy (iqp->write, iqp->write_len, p);
     }
 
-  if (ioparm.readwrite != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_READWRITE) != 0)
     {
       p = (u == NULL) ? inquire_readwrite (NULL, 0) :
        inquire_readwrite (u->file, u->file_len);
 
-      cf_strcpy (ioparm.readwrite, ioparm.readwrite_len, p);
+      cf_strcpy (iqp->readwrite, iqp->readwrite_len, p);
     }
 
-  if (ioparm.delim != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_DELIM) != 0)
     {
       if (u == NULL || u->flags.form != FORM_FORMATTED)
        p = undefined;
@@ -230,13 +502,13 @@ inquire_via_unit (gfc_unit * u)
            p = "APOSTROPHE";
            break;
          default:
-           internal_error ("inquire_via_unit(): Bad delim");
+           internal_error (&iqp->common, "inquire_via_unit(): Bad delim");
          }
 
-      cf_strcpy (ioparm.access, ioparm.access_len, p);
+      cf_strcpy (iqp->delim, iqp->delim_len, p);
     }
 
-  if (ioparm.pad != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_PAD) != 0)
     {
       if (u == NULL || u->flags.form != FORM_FORMATTED)
        p = undefined;
@@ -250,10 +522,33 @@ inquire_via_unit (gfc_unit * u)
            p = "YES";
            break;
          default:
-           internal_error ("inquire_via_unit(): Bad pad");
+           internal_error (&iqp->common, "inquire_via_unit(): Bad pad");
          }
 
-      cf_strcpy (ioparm.pad, ioparm.pad_len, p);
+      cf_strcpy (iqp->pad, iqp->pad_len, p);
+    }
+  if ((cf & IOPARM_INQUIRE_HAS_CONVERT) != 0)
+    {
+      if (u == NULL)
+       p = undefined;
+      else
+       switch (u->flags.convert)
+         {
+           /*  big_endian is 0 for little-endian, 1 for big-endian.  */
+         case GFC_CONVERT_NATIVE:
+           p = big_endian ? "BIG_ENDIAN" : "LITTLE_ENDIAN";
+           break;
+
+         case GFC_CONVERT_SWAP:
+           p = big_endian ? "LITTLE_ENDIAN" : "BIG_ENDIAN";
+           break;
+
+         default:
+           internal_error (&iqp->common, "inquire_via_unit(): Bad convert");
+         }
+
+      cf_strcpy (iqp->convert, iqp->convert_len, p);
     }
 }
 
@@ -262,120 +557,148 @@ inquire_via_unit (gfc_unit * u)
  * only used if the filename is *not* connected to a unit number. */
 
 static void
-inquire_via_filename (void)
+inquire_via_filename (st_parameter_inquire *iqp)
 {
   const char *p;
+  GFC_INTEGER_4 cf = iqp->common.flags;
 
-  if (ioparm.exist != NULL)
-    *ioparm.exist = file_exists ();
+  if ((cf & IOPARM_INQUIRE_HAS_EXIST) != 0)
+    *iqp->exist = file_exists (iqp->file, iqp->file_len);
 
-  if (ioparm.opened != NULL)
-    *ioparm.opened = 0;
+  if ((cf & IOPARM_INQUIRE_HAS_OPENED) != 0)
+    *iqp->opened = 0;
 
-  if (ioparm.number != NULL)
-    *ioparm.number = -1;
+  if ((cf & IOPARM_INQUIRE_HAS_NUMBER) != 0)
+    *iqp->number = -1;
 
-  if (ioparm.named != NULL)
-    *ioparm.named = 1;
+  if ((cf & IOPARM_INQUIRE_HAS_NAMED) != 0)
+    *iqp->named = 1;
 
-  if (ioparm.name != NULL)
-    fstrcpy (ioparm.name, ioparm.name_len, ioparm.file, ioparm.file_len);
+  if ((cf & IOPARM_INQUIRE_HAS_NAME) != 0)
+    fstrcpy (iqp->name, iqp->name_len, iqp->file, iqp->file_len);
 
-  if (ioparm.access != NULL)
-    cf_strcpy (ioparm.access, ioparm.access_len, undefined);
+  if ((cf & IOPARM_INQUIRE_HAS_ACCESS) != 0)
+    cf_strcpy (iqp->access, iqp->access_len, undefined);
 
-  if (ioparm.sequential != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_SEQUENTIAL) != 0)
     {
-      p = inquire_sequential (ioparm.file, ioparm.file_len);
-      cf_strcpy (ioparm.sequential, ioparm.sequential_len, p);
+      p = "UNKNOWN";
+      cf_strcpy (iqp->sequential, iqp->sequential_len, p);
     }
 
-  if (ioparm.direct != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_DIRECT) != 0)
     {
-      p = inquire_direct (ioparm.file, ioparm.file_len);
-      cf_strcpy (ioparm.direct, ioparm.direct_len, p);
+      p = "UNKNOWN";
+      cf_strcpy (iqp->direct, iqp->direct_len, p);
     }
 
-  if (ioparm.form != NULL)
-    cf_strcpy (ioparm.form, ioparm.form_len, undefined);
+  if ((cf & IOPARM_INQUIRE_HAS_FORM) != 0)
+    cf_strcpy (iqp->form, iqp->form_len, undefined);
 
-  if (ioparm.formatted != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_FORMATTED) != 0)
     {
-      p = inquire_formatted (ioparm.file, ioparm.file_len);
-      cf_strcpy (ioparm.formatted, ioparm.formatted_len, p);
+      p = "UNKNOWN";
+      cf_strcpy (iqp->formatted, iqp->formatted_len, p);
     }
 
-  if (ioparm.unformatted != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_UNFORMATTED) != 0)
     {
-      p = inquire_unformatted (ioparm.file, ioparm.file_len);
-      cf_strcpy (ioparm.unformatted, ioparm.unformatted_len, p);
+      p = "UNKNOWN";
+      cf_strcpy (iqp->unformatted, iqp->unformatted_len, p);
     }
 
-  if (ioparm.recl_out != NULL)
-    *ioparm.recl_out = 0;
+  if ((cf & IOPARM_INQUIRE_HAS_RECL_OUT) != 0)
+    *iqp->recl_out = 0;
 
-  if (ioparm.nextrec != NULL)
-    *ioparm.nextrec = 0;
+  if ((cf & IOPARM_INQUIRE_HAS_NEXTREC) != 0)
+    *iqp->nextrec = 0;
 
-  if (ioparm.blank != NULL)
-    cf_strcpy (ioparm.blank, ioparm.blank_len, undefined);
+  if ((cf & IOPARM_INQUIRE_HAS_BLANK) != 0)
+    cf_strcpy (iqp->blank, iqp->blank_len, undefined);
 
-  if (ioparm.position != NULL)
-    cf_strcpy (ioparm.position, ioparm.position_len, undefined);
+  if ((cf & IOPARM_INQUIRE_HAS_PAD) != 0)
+    cf_strcpy (iqp->pad, iqp->pad_len, undefined);
 
-  if (ioparm.access != NULL)
-    cf_strcpy (ioparm.access, ioparm.access_len, undefined);
-
-  if (ioparm.read != NULL)
+  if (cf & IOPARM_INQUIRE_HAS_FLAGS2)
     {
-      p = inquire_read (ioparm.file, ioparm.file_len);
-      cf_strcpy (ioparm.read, ioparm.read_len, p);
+      GFC_INTEGER_4 cf2 = iqp->flags2;
+
+      if ((cf2 & IOPARM_INQUIRE_HAS_ENCODING) != 0)
+       cf_strcpy (iqp->encoding, iqp->encoding_len, undefined);
+  
+      if ((cf2 & IOPARM_INQUIRE_HAS_DELIM) != 0)
+       cf_strcpy (iqp->delim, iqp->delim_len, undefined);
+
+      if ((cf2 & IOPARM_INQUIRE_HAS_DECIMAL) != 0)
+       cf_strcpy (iqp->decimal, iqp->decimal_len, undefined);
+
+      if ((cf2 & IOPARM_INQUIRE_HAS_DELIM) != 0)
+       cf_strcpy (iqp->delim, iqp->delim_len, undefined);
+
+      if ((cf2 & IOPARM_INQUIRE_HAS_PAD) != 0)
+       cf_strcpy (iqp->pad, iqp->pad_len, undefined);
+  
+      if ((cf2 & IOPARM_INQUIRE_HAS_ENCODING) != 0)
+       cf_strcpy (iqp->encoding, iqp->encoding_len, undefined);
+
+      if ((cf2 & IOPARM_INQUIRE_HAS_SIZE) != 0)
+       *iqp->size = file_size (iqp->file, iqp->file_len);
     }
 
-  if (ioparm.write != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_POSITION) != 0)
+    cf_strcpy (iqp->position, iqp->position_len, undefined);
+
+  if ((cf & IOPARM_INQUIRE_HAS_ACCESS) != 0)
+    cf_strcpy (iqp->access, iqp->access_len, undefined);
+
+  if ((cf & IOPARM_INQUIRE_HAS_READ) != 0)
     {
-      p = inquire_write (ioparm.file, ioparm.file_len);
-      cf_strcpy (ioparm.write, ioparm.write_len, p);
+      p = inquire_read (iqp->file, iqp->file_len);
+      cf_strcpy (iqp->read, iqp->read_len, p);
     }
 
-  if (ioparm.readwrite != NULL)
+  if ((cf & IOPARM_INQUIRE_HAS_WRITE) != 0)
     {
-      p = inquire_read (ioparm.file, ioparm.file_len);
-      cf_strcpy (ioparm.readwrite, ioparm.readwrite_len, p);
+      p = inquire_write (iqp->file, iqp->file_len);
+      cf_strcpy (iqp->write, iqp->write_len, p);
     }
 
-  if (ioparm.delim != NULL)
-    cf_strcpy (ioparm.delim, ioparm.delim_len, undefined);
-
-  if (ioparm.pad != NULL)
-    cf_strcpy (ioparm.pad, ioparm.pad_len, undefined);
-
+  if ((cf & IOPARM_INQUIRE_HAS_READWRITE) != 0)
+    {
+      p = inquire_read (iqp->file, iqp->file_len);
+      cf_strcpy (iqp->readwrite, iqp->readwrite_len, p);
+    }
 }
 
 
 /* Library entry point for the INQUIRE statement (non-IOLENGTH
    form).  */
 
-extern void st_inquire (void);
+extern void st_inquire (st_parameter_inquire *);
 export_proto(st_inquire);
 
 void
-st_inquire (void)
+st_inquire (st_parameter_inquire *iqp)
 {
   gfc_unit *u;
 
-  library_start ();
+  library_start (&iqp->common);
 
-  if (ioparm.file == NULL)
-    inquire_via_unit (find_unit (ioparm.unit));
+  if ((iqp->common.flags & IOPARM_INQUIRE_HAS_FILE) == 0)
+    {
+      u = find_unit (iqp->common.unit);
+      inquire_via_unit (iqp, u);
+    }
   else
     {
-      u = find_file ();
+      u = find_file (iqp->file, iqp->file_len);
       if (u == NULL)
-       inquire_via_filename ();
+       inquire_via_filename (iqp);
       else
-       inquire_via_unit (u);
+       inquire_via_unit (iqp, u);
     }
+  if (u != NULL)
+    unlock_unit (u);
 
   library_end ();
 }