OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / libgfortran / io / open.c
index b336079..be8f4bb 100644 (file)
@@ -1,39 +1,36 @@
-/* Copyright (C) 2002, 2003, 2004, 2005
+/* Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
    Contributed by Andy Vaught
+   F2003 I/O support contributed by Jerry DeLisle
 
-This file is part of the GNU Fortran 95 runtime library (libgfortran).
+This file is part of the GNU Fortran 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.
 
-In addition to the permissions in the GNU General Public License, the
-Free Software Foundation gives you unlimited permission to link the
-compiled version of this file into combinations with other programs,
-and to distribute those combinations without any restriction coming
-from the use of this file.  (The General Public License restrictions
-do apply in other respects; for example, they cover modification of
-the file, and distribution when not linked into a combine
-executable.)
-
 Libgfortran is distributed in the hope that it will be useful,
 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, 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, 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/>.  */
 
-#include "config.h"
+#include "io.h"
+#include "fbuf.h"
+#include "unix.h"
 #include <unistd.h>
-#include <stdio.h>
 #include <string.h>
-#include "libgfortran.h"
-#include "io.h"
+#include <errno.h>
+#include <stdlib.h>
 
 
 static const st_option access_opt[] = {
@@ -99,12 +96,52 @@ static const st_option pad_opt[] =
   { NULL, 0}
 };
 
+static const st_option decimal_opt[] =
+{
+  { "point", DECIMAL_POINT},
+  { "comma", DECIMAL_COMMA},
+  { NULL, 0}
+};
+
+static const st_option encoding_opt[] =
+{
+  { "utf-8", ENCODING_UTF8},
+  { "default", ENCODING_DEFAULT},
+  { NULL, 0}
+};
+
+static const st_option round_opt[] =
+{
+  { "up", ROUND_UP},
+  { "down", ROUND_DOWN},
+  { "zero", ROUND_ZERO},
+  { "nearest", ROUND_NEAREST},
+  { "compatible", ROUND_COMPATIBLE},
+  { "processor_defined", ROUND_PROCDEFINED},
+  { NULL, 0}
+};
+
+static const st_option sign_opt[] =
+{
+  { "plus", SIGN_PLUS},
+  { "suppress", SIGN_SUPPRESS},
+  { "processor_defined", SIGN_PROCDEFINED},
+  { NULL, 0}
+};
+
 static const st_option convert_opt[] =
 {
-  { "native", CONVERT_NATIVE},
-  { "swap", CONVERT_SWAP},
-  { "big_endian", CONVERT_BIG},
-  { "little_endian", CONVERT_LITTLE},
+  { "native", GFC_CONVERT_NATIVE},
+  { "swap", GFC_CONVERT_SWAP},
+  { "big_endian", GFC_CONVERT_BIG},
+  { "little_endian", GFC_CONVERT_LITTLE},
+  { NULL, 0}
+};
+
+static const st_option async_opt[] =
+{
+  { "yes", ASYNC_YES},
+  { "no", ASYNC_NO},
   { NULL, 0}
 };
 
@@ -113,10 +150,10 @@ static const st_option convert_opt[] =
    This prevents us from changing the state from AFTER_ENDFILE to
    AT_ENDFILE.  */
 
-void
+static void
 test_endfile (gfc_unit * u)
 {
-  if (u->endfile == NO_ENDFILE && file_length (u->s) == file_position (u->s))
+  if (u->endfile == NO_ENDFILE && file_length (u->s) == stell (u->s))
     u->endfile = AT_ENDFILE;
 }
 
@@ -131,24 +168,24 @@ edit_modes (st_parameter_open *opp, gfc_unit * u, unit_flags * flags)
 
   if (flags->status != STATUS_UNSPECIFIED && flags->status != STATUS_OLD && 
       u->flags.status != flags->status)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Cannot change STATUS parameter in OPEN statement");
 
   if (flags->access != ACCESS_UNSPECIFIED && u->flags.access != flags->access)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Cannot change ACCESS parameter in OPEN statement");
 
   if (flags->form != FORM_UNSPECIFIED && u->flags.form != flags->form)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Cannot change FORM parameter in OPEN statement");
 
   if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN)
       && opp->recl_in != u->recl)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Cannot change RECL parameter in OPEN statement");
 
   if (flags->action != ACTION_UNSPECIFIED && u->flags.action != flags->action)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Cannot change ACTION parameter in OPEN statement");
 
   /* Status must be OLD if present.  */
@@ -160,25 +197,45 @@ edit_modes (st_parameter_open *opp, gfc_unit * u, unit_flags * flags)
        notify_std (&opp->common, GFC_STD_GNU,
                    "OPEN statement must have a STATUS of OLD or UNKNOWN");
       else
-       generate_error (&opp->common, ERROR_BAD_OPTION,
+       generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "OPEN statement must have a STATUS of OLD or UNKNOWN");
     }
 
   if (u->flags.form == FORM_UNFORMATTED)
     {
       if (flags->delim != DELIM_UNSPECIFIED)
-       generate_error (&opp->common, ERROR_OPTION_CONFLICT,
+       generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
                        "DELIM parameter conflicts with UNFORMATTED form in "
                        "OPEN statement");
 
       if (flags->blank != BLANK_UNSPECIFIED)
-       generate_error (&opp->common, ERROR_OPTION_CONFLICT,
+       generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
                        "BLANK parameter conflicts with UNFORMATTED form in "
                        "OPEN statement");
 
       if (flags->pad != PAD_UNSPECIFIED)
-       generate_error (&opp->common, ERROR_OPTION_CONFLICT,
-                       "PAD paramter conflicts with UNFORMATTED form in "
+       generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
+                       "PAD parameter conflicts with UNFORMATTED form in "
+                       "OPEN statement");
+
+      if (flags->decimal != DECIMAL_UNSPECIFIED)
+       generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
+                       "DECIMAL parameter conflicts with UNFORMATTED form in "
+                       "OPEN statement");
+
+      if (flags->encoding != ENCODING_UNSPECIFIED)
+       generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
+                       "ENCODING parameter conflicts with UNFORMATTED form in "
+                       "OPEN statement");
+
+      if (flags->round != ROUND_UNSPECIFIED)
+       generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
+                       "ROUND parameter conflicts with UNFORMATTED form in "
+                       "OPEN statement");
+
+      if (flags->sign != SIGN_UNSPECIFIED)
+       generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
+                       "SIGN parameter conflicts with UNFORMATTED form in "
                        "OPEN statement");
     }
 
@@ -191,6 +248,16 @@ edit_modes (st_parameter_open *opp, gfc_unit * u, unit_flags * flags)
        u->flags.delim = flags->delim;
       if (flags->pad != PAD_UNSPECIFIED)
        u->flags.pad = flags->pad;
+      if (flags->decimal != DECIMAL_UNSPECIFIED)
+       u->flags.decimal = flags->decimal;
+      if (flags->encoding != ENCODING_UNSPECIFIED)
+       u->flags.encoding = flags->encoding;
+      if (flags->async != ASYNC_UNSPECIFIED)
+       u->flags.async = flags->async;
+      if (flags->round != ROUND_UNSPECIFIED)
+       u->flags.round = flags->round;
+      if (flags->sign != SIGN_UNSPECIFIED)
+       u->flags.sign = flags->sign;
     }
 
   /* Reposition the file if necessary.  */
@@ -202,17 +269,17 @@ edit_modes (st_parameter_open *opp, gfc_unit * u, unit_flags * flags)
       break;
 
     case POSITION_REWIND:
-      if (sseek (u->s, 0) == FAILURE)
+      if (sseek (u->s, 0, SEEK_SET) != 0)
        goto seek_error;
 
       u->current_record = 0;
       u->last_record = 0;
 
-      test_endfile (u);                /* We might be at the end.  */
+      test_endfile (u);
       break;
 
     case POSITION_APPEND:
-      if (sseek (u->s, file_length (u->s)) == FAILURE)
+      if (sseek (u->s, 0, SEEK_END) < 0)
        goto seek_error;
 
       if (flags->access != ACCESS_STREAM)
@@ -222,7 +289,7 @@ edit_modes (st_parameter_open *opp, gfc_unit * u, unit_flags * flags)
       break;
 
     seek_error:
-      generate_error (&opp->common, ERROR_OS, NULL);
+      generate_error (&opp->common, LIBERROR_OS, NULL);
       break;
     }
 
@@ -250,6 +317,13 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
     flags->form = (flags->access == ACCESS_SEQUENTIAL)
       ? FORM_FORMATTED : FORM_UNFORMATTED;
 
+  if (flags->async == ASYNC_UNSPECIFIED)
+    flags->async = ASYNC_NO;
+
+  if (flags->status == STATUS_UNSPECIFIED)
+    flags->status = STATUS_UNKNOWN;
+
+  /* Checks.  */
 
   if (flags->delim == DELIM_UNSPECIFIED)
     flags->delim = DELIM_NONE;
@@ -257,7 +331,7 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
     {
       if (flags->form == FORM_UNFORMATTED)
        {
-         generate_error (&opp->common, ERROR_OPTION_CONFLICT,
+         generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
                          "DELIM parameter conflicts with UNFORMATTED form in "
                          "OPEN statement");
          goto fail;
@@ -270,7 +344,7 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
     {
       if (flags->form == FORM_UNFORMATTED)
        {
-         generate_error (&opp->common, ERROR_OPTION_CONFLICT,
+         generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
                          "BLANK parameter conflicts with UNFORMATTED form in "
                          "OPEN statement");
          goto fail;
@@ -283,8 +357,64 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
     {
       if (flags->form == FORM_UNFORMATTED)
        {
-         generate_error (&opp->common, ERROR_OPTION_CONFLICT,
-                         "PAD paramter conflicts with UNFORMATTED form in "
+         generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
+                         "PAD parameter conflicts with UNFORMATTED form in "
+                         "OPEN statement");
+         goto fail;
+       }
+    }
+
+  if (flags->decimal == DECIMAL_UNSPECIFIED)
+    flags->decimal = DECIMAL_POINT;
+  else
+    {
+      if (flags->form == FORM_UNFORMATTED)
+       {
+         generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
+                         "DECIMAL parameter conflicts with UNFORMATTED form "
+                         "in OPEN statement");
+         goto fail;
+       }
+    }
+
+  if (flags->encoding == ENCODING_UNSPECIFIED)
+    flags->encoding = ENCODING_DEFAULT;
+  else
+    {
+      if (flags->form == FORM_UNFORMATTED)
+       {
+         generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
+                         "ENCODING parameter conflicts with UNFORMATTED form in "
+                         "OPEN statement");
+         goto fail;
+       }
+    }
+
+  /* NB: the value for ROUND when it's not specified by the user does not
+         have to be PROCESSOR_DEFINED; the standard says that it is
+        processor dependent, and requires that it is one of the
+        possible value (see F2003, 9.4.5.13).  */
+  if (flags->round == ROUND_UNSPECIFIED)
+    flags->round = ROUND_PROCDEFINED;
+  else
+    {
+      if (flags->form == FORM_UNFORMATTED)
+       {
+         generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
+                         "ROUND parameter conflicts with UNFORMATTED form in "
+                         "OPEN statement");
+         goto fail;
+       }
+    }
+
+  if (flags->sign == SIGN_UNSPECIFIED)
+    flags->sign = SIGN_PROCDEFINED;
+  else
+    {
+      if (flags->form == FORM_UNFORMATTED)
+       {
+         generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
+                         "SIGN parameter conflicts with UNFORMATTED form in "
                          "OPEN statement");
          goto fail;
        }
@@ -292,7 +422,7 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
 
   if (flags->position != POSITION_ASIS && flags->access == ACCESS_DIRECT)
    {
-     generate_error (&opp->common, ERROR_OPTION_CONFLICT,
+     generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
                      "ACCESS parameter conflicts with SEQUENTIAL access in "
                      "OPEN statement");
      goto fail;
@@ -301,23 +431,17 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
    if (flags->position == POSITION_UNSPECIFIED)
      flags->position = POSITION_ASIS;
 
-
-  if (flags->status == STATUS_UNSPECIFIED)
-    flags->status = STATUS_UNKNOWN;
-
-  /* Checks.  */
-
   if (flags->access == ACCESS_DIRECT
       && (opp->common.flags & IOPARM_OPEN_HAS_RECL_IN) == 0)
     {
-      generate_error (&opp->common, ERROR_MISSING_OPTION,
+      generate_error (&opp->common, LIBERROR_MISSING_OPTION,
                      "Missing RECL parameter in OPEN statement");
       goto fail;
     }
 
   if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN) && opp->recl_in <= 0)
     {
-      generate_error (&opp->common, ERROR_BAD_OPTION,
+      generate_error (&opp->common, LIBERROR_BAD_OPTION,
                      "RECL parameter is non-positive in OPEN statement");
       goto fail;
     }
@@ -331,7 +455,7 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
          break;
        }
 
-      generate_error (&opp->common, ERROR_BAD_OPTION,
+      generate_error (&opp->common, LIBERROR_BAD_OPTION,
                      "FILE parameter must not be present in OPEN statement");
       goto fail;
 
@@ -343,7 +467,12 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
        break;
 
       opp->file = tmpname;
-      opp->file_len = sprintf(opp->file, "fort.%d", opp->common.unit);
+#ifdef HAVE_SNPRINTF
+      opp->file_len = snprintf(opp->file, sizeof (tmpname), "fort.%d", 
+                              (int) opp->common.unit);
+#else
+      opp->file_len = sprintf(opp->file, "fort.%d", (int) opp->common.unit);
+#endif
       break;
 
     default:
@@ -362,7 +491,7 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
       && (options.stderr_unit < 0 || u2->unit_number != options.stderr_unit))
     {
       unlock_unit (u2);
-      generate_error (&opp->common, ERROR_ALREADY_OPEN, NULL);
+      generate_error (&opp->common, LIBERROR_ALREADY_OPEN, NULL);
       goto cleanup;
     }
 
@@ -374,7 +503,34 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
   s = open_external (opp, flags);
   if (s == NULL)
     {
-      generate_error (&opp->common, ERROR_OS, NULL);
+      char *path, *msg;
+      path = (char *) gfc_alloca (opp->file_len + 1);
+      msg = (char *) gfc_alloca (opp->file_len + 51);
+      unpack_filename (path, opp->file, opp->file_len);
+
+      switch (errno)
+       {
+       case ENOENT: 
+         sprintf (msg, "File '%s' does not exist", path);
+         break;
+
+       case EEXIST:
+         sprintf (msg, "File '%s' already exists", path);
+         break;
+
+       case EACCES:
+         sprintf (msg, "Permission denied trying to open file '%s'", path);
+         break;
+
+       case EISDIR:
+         sprintf (msg, "'%s' is a directory", path);
+         break;
+
+       default:
+         msg = NULL;
+       }
+
+      generate_error (&opp->common, LIBERROR_OS, msg);
       goto cleanup;
     }
 
@@ -395,37 +551,50 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
   u->mode = READING;
   u->maxrec = 0;
   u->bytes_left = 0;
+  u->saved_pos = 0;
 
   if (flags->position == POSITION_APPEND)
     {
-      if (sseek (u->s, file_length (u->s)) == FAILURE)
-       generate_error (&opp->common, ERROR_OS, NULL);
+      if (sseek (u->s, 0, SEEK_END) < 0)
+       generate_error (&opp->common, LIBERROR_OS, NULL);
       u->endfile = AT_ENDFILE;
     }
 
   /* Unspecified recl ends up with a processor dependent value.  */
 
   if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN))
-    u->recl = opp->recl_in;
+    {
+      u->flags.has_recl = 1;
+      u->recl = opp->recl_in;
+      u->recl_subrecord = u->recl;
+      u->bytes_left = u->recl;
+    }
   else
     {
-      switch (compile_options.record_marker)
+      u->flags.has_recl = 0;
+      u->recl = max_offset;
+      if (compile_options.max_subrecord_length)
        {
-       case 0:
-         u->recl = max_offset;
-         break;
-
-       case sizeof (GFC_INTEGER_4):
-         u->recl = GFC_INTEGER_4_HUGE;
-         break;
-
-       case sizeof (GFC_INTEGER_8):
-         u->recl = max_offset;
-         break;
-
-       default:
-         runtime_error ("Illegal value for record marker");
-         break;
+         u->recl_subrecord = compile_options.max_subrecord_length;
+       }
+      else
+       {
+         switch (compile_options.record_marker)
+           {
+           case 0:
+             /* Fall through */
+           case sizeof (GFC_INTEGER_4):
+             u->recl_subrecord = GFC_MAX_SUBRECORD_LENGTH;
+             break;
+
+           case sizeof (GFC_INTEGER_8):
+             u->recl_subrecord = max_offset - 16;
+             break;
+
+           default:
+             runtime_error ("Illegal value for record marker");
+             break;
+           }
        }
     }
 
@@ -440,7 +609,8 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
     {
       u->maxrec = max_offset;
       u->recl = 1;
-      u->last_record = 1;
+      u->bytes_left = 1;
+      u->strm_pos = stell (u->s) + 1;
     }
 
   memmove (u->file, opp->file, opp->file_len);
@@ -448,13 +618,26 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
 
   /* Curiously, the standard requires that the
      position specifier be ignored for new files so a newly connected
-     file starts out that the initial point.  We still need to figure
+     file starts out at the initial point.  We still need to figure
      out if the file is at the end or not.  */
 
   test_endfile (u);
 
   if (flags->status == STATUS_SCRATCH && opp->file != NULL)
-    free_mem (opp->file);
+    free (opp->file);
+    
+  if (flags->form == FORM_FORMATTED)
+    {
+      if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN))
+        fbuf_init (u, u->recl);
+      else
+        fbuf_init (u, 0);
+    }
+  else
+    u->fbuf = NULL;
+
+    
+    
   return u;
 
  cleanup:
@@ -462,7 +645,7 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
   /* Free memory associated with a temporary filename.  */
 
   if (flags->status == STATUS_SCRATCH && opp->file != NULL)
-    free_mem (opp->file);
+    free (opp->file);
 
  fail:
 
@@ -497,17 +680,17 @@ already_open (st_parameter_open *opp, gfc_unit * u, unit_flags * flags)
        }
 #endif
 
-      if (sclose (u->s) == FAILURE)
+      if (sclose (u->s) == -1)
        {
          unlock_unit (u);
-         generate_error (&opp->common, ERROR_OS,
+         generate_error (&opp->common, LIBERROR_OS,
                          "Error closing file in OPEN statement");
          return;
        }
 
       u->s = NULL;
       if (u->file)
-       free_mem (u->file);
+       free (u->file);
       u->file = NULL;
       u->file_len = 0;
 
@@ -563,6 +746,26 @@ st_open (st_parameter_open *opp)
     find_option (&opp->common, opp->pad, opp->pad_len,
                 pad_opt, "Bad PAD parameter in OPEN statement");
 
+  flags.decimal = !(cf & IOPARM_OPEN_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
+    find_option (&opp->common, opp->decimal, opp->decimal_len,
+                decimal_opt, "Bad DECIMAL parameter in OPEN statement");
+
+  flags.encoding = !(cf & IOPARM_OPEN_HAS_ENCODING) ? ENCODING_UNSPECIFIED :
+    find_option (&opp->common, opp->encoding, opp->encoding_len,
+                encoding_opt, "Bad ENCODING parameter in OPEN statement");
+
+  flags.async = !(cf & IOPARM_OPEN_HAS_ASYNCHRONOUS) ? ASYNC_UNSPECIFIED :
+    find_option (&opp->common, opp->asynchronous, opp->asynchronous_len,
+                async_opt, "Bad ASYNCHRONOUS parameter in OPEN statement");
+
+  flags.round = !(cf & IOPARM_OPEN_HAS_ROUND) ? ROUND_UNSPECIFIED :
+    find_option (&opp->common, opp->round, opp->round_len,
+                round_opt, "Bad ROUND parameter in OPEN statement");
+
+  flags.sign = !(cf & IOPARM_OPEN_HAS_SIGN) ? SIGN_UNSPECIFIED :
+    find_option (&opp->common, opp->sign, opp->sign_len,
+                sign_opt, "Bad SIGN parameter in OPEN statement");
+
   flags.form = !(cf & IOPARM_OPEN_HAS_FORM) ? FORM_UNSPECIFIED :
     find_option (&opp->common, opp->form, opp->form_len,
                 form_opt, "Bad FORM parameter in OPEN statement");
@@ -580,7 +783,7 @@ st_open (st_parameter_open *opp)
 
   conv = get_unformatted_convert (opp->common.unit);
 
-  if (conv == CONVERT_NONE)
+  if (conv == GFC_CONVERT_NONE)
     {
       /* Nothing has been set by environment variable, check the convert tag.  */
       if (cf & IOPARM_OPEN_HAS_CONVERT)
@@ -591,20 +794,20 @@ st_open (st_parameter_open *opp)
        conv = compile_options.convert;
     }
   
-  /* We use l8_to_l4_offset, which is 0 on little-endian machines
+  /* We use big_endian, which is 0 on little-endian machines
      and 1 on big-endian machines.  */
   switch (conv)
     {
-    case CONVERT_NATIVE:
-    case CONVERT_SWAP:
+    case GFC_CONVERT_NATIVE:
+    case GFC_CONVERT_SWAP:
       break;
       
-    case CONVERT_BIG:
-      conv = l8_to_l4_offset ? CONVERT_NATIVE : CONVERT_SWAP;
+    case GFC_CONVERT_BIG:
+      conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
       break;
       
-    case CONVERT_LITTLE:
-      conv = l8_to_l4_offset ? CONVERT_SWAP : CONVERT_NATIVE;
+    case GFC_CONVERT_LITTLE:
+      conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
       break;
       
     default:
@@ -614,20 +817,20 @@ st_open (st_parameter_open *opp)
 
   flags.convert = conv;
 
-  if (opp->common.unit < 0)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+  if (!(opp->common.flags & IOPARM_OPEN_HAS_NEWUNIT) && opp->common.unit < 0)
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Bad unit number in OPEN statement");
 
   if (flags.position != POSITION_UNSPECIFIED
       && flags.access == ACCESS_DIRECT)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Cannot use POSITION with direct access files");
 
   if (flags.access == ACCESS_APPEND)
     {
       if (flags.position != POSITION_UNSPECIFIED
          && flags.position != POSITION_APPEND)
-       generate_error (&opp->common, ERROR_BAD_OPTION,
+       generate_error (&opp->common, LIBERROR_BAD_OPTION,
                        "Conflicting ACCESS and POSITION flags in"
                        " OPEN statement");
 
@@ -642,8 +845,13 @@ st_open (st_parameter_open *opp)
 
   if ((opp->common.flags & IOPARM_LIBRETURN_MASK) == IOPARM_LIBRETURN_OK)
     {
-      u = find_or_create_unit (opp->common.unit);
+      if ((opp->common.flags & IOPARM_OPEN_HAS_NEWUNIT))
+       {
+         *opp->newunit = get_unique_unit_number(opp);
+         opp->common.unit = *opp->newunit;
+       }
 
+      u = find_or_create_unit (opp->common.unit);
       if (u->s == NULL)
        {
          u = new_unit (opp, u, &flags);