]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
When file status is unknown, don't set O_CREAT when opening read-only.
authorJanne Blomqvist <jb@gcc.gnu.org>
Fri, 15 Nov 2013 22:00:36 +0000 (00:00 +0200)
committerJanne Blomqvist <jb@gcc.gnu.org>
Fri, 15 Nov 2013 22:00:36 +0000 (00:00 +0200)
2013-11-15  Janne Blomqvist  <jb@gcc.gnu.org>
    Jerry DeLisle  <jvdelisle@gcc.gnu.org>

PR fortran/59108
* io/unix.c (regular_file): Don't set O_CREAT when opening a file
read-only with unknown status. Mask out O_CREAT when falling back
to opening read-only if ACTION= is not set and read-write fails.

Co-Authored-By: Jerry DeLisle <jvdelisle@gcc.gnu.org>
From-SVN: r204864

libgfortran/ChangeLog
libgfortran/io/unix.c

index 31a4fb5f4a7d15022cb54da227c06d370e0359f0..c18391f106fe5245936142c5b1b5f6e35b25f2fa 100644 (file)
@@ -1,3 +1,11 @@
+2013-11-15  Janne Blomqvist  <jb@gcc.gnu.org>
+           Jerry DeLisle  <jvdelisle@gcc.gnu.org>
+
+       PR fortran/59108
+       * io/unix.c (regular_file): Don't set O_CREAT when opening a file
+       read-only with unknown status. Mask out O_CREAT when falling back
+       to opening read-only if ACTION= is not set and read-write fails.
+
 2013-11-15  Steve Ellcey  <sellcey@mips.com>
 
        * configure.ac: Do not define HAVE_STRTOLD.
index 8a84ae4eef36ac0d235a71d3350d3524807cfd38..c2bc28aed15771e22bb20d0b0553f842ef4b59f9 100644 (file)
@@ -1245,7 +1245,7 @@ regular_file (st_parameter_open *opp, unit_flags *flags)
   char path[min(PATH_MAX, opp->file_len + 1)];
   int mode;
   int rwflag;
-  int crflag;
+  int crflag, crflag2;
   int fd;
   int err;
 
@@ -1297,8 +1297,6 @@ regular_file (st_parameter_open *opp, unit_flags *flags)
     }
 #endif
 
-  rwflag = 0;
-
   switch (flags->action)
     {
     case ACTION_READ:
@@ -1329,8 +1327,10 @@ regular_file (st_parameter_open *opp, unit_flags *flags)
       break;
 
     case STATUS_UNKNOWN:
-    case STATUS_SCRATCH:
-      crflag = O_CREAT;
+      if (rwflag == O_RDONLY)
+       crflag = 0;
+      else
+       crflag = O_CREAT;
       break;
 
     case STATUS_REPLACE:
@@ -1338,6 +1338,8 @@ regular_file (st_parameter_open *opp, unit_flags *flags)
       break;
 
     default:
+      /* Note: STATUS_SCRATCH is handled by tempfile () and should
+        never be seen here.  */
       internal_error (&opp->common, "regular_file(): Bad status");
     }
 
@@ -1366,14 +1368,18 @@ regular_file (st_parameter_open *opp, unit_flags *flags)
 
   /* retry for read-only access */
   rwflag = O_RDONLY;
-  fd = open (path, rwflag | crflag, mode);
+  if (flags->status == STATUS_UNKNOWN)
+    crflag2 = crflag & ~(O_CREAT);
+  else
+    crflag2 = crflag;
+  fd = open (path, rwflag | crflag2, mode);
   if (fd >=0)
     {
       flags->action = ACTION_READ;
       return fd;               /* success */
     }
   
-  if (errno != EACCES)
+  if (errno != EACCES && errno != ENOENT)
     return fd;                 /* failure */
 
   /* retry for write-only access */