re PR fortran/51825 (Fortran runtime error: Cannot match namelist object name)
[gcc.git] / libgfortran / io / intrinsics.c
index 9428b759d1518e5598f4cf3dc4dde39a83644e3e..1573434d6dba89fda48f12c67bfea86936bd19c2 100644 (file)
@@ -1,8 +1,8 @@
 /* Implementation of the FGET, FGETC, FPUT, FPUTC, FLUSH 
    FTELL, TTYNAM and ISATTY intrinsics.
-   Copyright (C) 2005, 2007, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2005-2013 Free Software Foundation, Inc.
 
-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
@@ -26,13 +26,10 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #include "io.h"
 #include "fbuf.h"
 #include "unix.h"
-
-#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
-#endif
-
 #include <string.h>
 
+
 static const int five = 5;
 static const int six = 6;
 
@@ -245,7 +242,7 @@ fseek_sub (int * unit, GFC_IO_INT * offset, int * whence, int * status)
   gfc_unit * u = find_unit (*unit);
   ssize_t result = -1;
 
-  if (u != NULL && is_seekable(u->s))
+  if (u != NULL)
     {
       result = sseek(u->s, *offset, *whence);
 
@@ -260,35 +257,56 @@ fseek_sub (int * unit, GFC_IO_INT * offset, int * whence, int * status)
 
 /* FTELL intrinsic */
 
+static gfc_offset
+gf_ftell (int unit)
+{
+  gfc_unit * u = find_unit (unit);
+  if (u == NULL)
+    return -1;
+  int pos = fbuf_reset (u);
+  if (pos != 0)
+    sseek (u->s, pos, SEEK_CUR);
+  gfc_offset ret = stell (u->s);
+  unlock_unit (u);
+  return ret;
+}
+
+
+/* Here is the ftell function with an incorrect return type; retained
+   due to ABI compatibility.  */
+
 extern size_t PREFIX(ftell) (int *);
 export_proto_np(PREFIX(ftell));
 
 size_t
 PREFIX(ftell) (int * unit)
 {
-  gfc_unit * u = find_unit (*unit);
-  size_t ret;
-  if (u == NULL)
-    return ((size_t) -1);
-  ret = (size_t) stell (u->s);
-  unlock_unit (u);
-  return ret;
+  return gf_ftell (*unit);
+}
+
+
+/* Here is the ftell function with the correct return type, ensuring
+   that large files can be supported as long as the target supports
+   large integers; as of 4.8 the FTELL intrinsic function will call
+   this one instead of the old ftell above.  */
+
+extern GFC_IO_INT PREFIX(ftell2) (int *);
+export_proto_np(PREFIX(ftell2));
+
+GFC_IO_INT
+PREFIX(ftell2) (int * unit)
+{
+  return gf_ftell (*unit);
 }
 
+
 #define FTELL_SUB(kind) \
   extern void ftell_i ## kind ## _sub (int *, GFC_INTEGER_ ## kind *); \
   export_proto(ftell_i ## kind ## _sub); \
   void \
   ftell_i ## kind ## _sub (int * unit, GFC_INTEGER_ ## kind * offset) \
   { \
-    gfc_unit * u = find_unit (*unit); \
-    if (u == NULL) \
-      *offset = -1; \
-    else \
-      { \
-       *offset = stell (u->s); \
-       unlock_unit (u); \
-      } \
+    *offset = gf_ftell (*unit);                        \
   }
 
 FTELL_SUB(1)
@@ -350,22 +368,23 @@ void
 ttynam_sub (int *unit, char * name, gfc_charlen_type name_len)
 {
   gfc_unit *u;
-  char * n;
-  int i;
+  int nlen;
+  int err = 1;
 
-  memset (name, ' ', name_len);
   u = find_unit (*unit);
   if (u != NULL)
     {
-      n = stream_ttyname (u->s);
-      if (n != NULL)
+      err = stream_ttyname (u->s, name, name_len);
+      if (err == 0)
        {
-         i = 0;
-         while (*n && i < name_len)
-           name[i++] = *(n++);
+         nlen = strlen (name);
+         memset (&name[nlen], ' ', name_len - nlen);
        }
+
       unlock_unit (u);
     }
+  if (err != 0)
+    memset (name, ' ', name_len);
 }
 
 
@@ -380,14 +399,15 @@ ttynam (char ** name, gfc_charlen_type * name_len, int unit)
   u = find_unit (unit);
   if (u != NULL)
     {
-      *name = stream_ttyname (u->s);
-      if (*name != NULL)
+      *name = xmalloc (TTY_NAME_MAX);
+      int err = stream_ttyname (u->s, *name, TTY_NAME_MAX);
+      if (err == 0)
        {
          *name_len = strlen (*name);
-         *name = strdup (*name);
          unlock_unit (u);
          return;
        }
+      free (*name);
       unlock_unit (u);
     }