--- /dev/null
+#if defined(__WIN32__) && !defined(__CYGWIN__)
+ /* Mostly skip on Windows, cf. main file why. */
+
+int expect_open_to_fail () { return 1; }
+
+void my_verify_not_exists (const char *dir) { }
+void my_mkdir (const char *dir) { }
+void my_rmdir (const char *dir) { }
+
+#else
+
+#include <sys/stat.h> /* For mkdir + permission bits. */
+#include <unistd.h> /* For rmdir. */
+#include <errno.h> /* For errno. */
+#include <stdio.h> /* For perror. */
+#include <stdlib.h> /* For abort. */
+
+
+int expect_open_to_fail () { return 0; }
+
+void
+my_verify_not_exists (const char *dir)
+{
+ struct stat path_stat;
+ int err = stat (dir, &path_stat);
+ if (err && errno == ENOENT)
+ return; /* OK */
+ if (err)
+ perror ("my_verify_not_exists");
+ else
+ printf ("my_verify_not_exists: pathname %s still exists\n", dir);
+ abort ();
+ }
+
+void
+my_mkdir (const char *dir)
+{
+ int err;
+ struct stat path_stat;
+
+ /* Check whether 'dir' exists and is a directory. */
+ err = stat (dir, &path_stat);
+ if (err && errno != ENOENT)
+ {
+ perror ("my_mkdir: failed to call stat for directory");
+ abort ();
+ }
+ if (err == 0 && !S_ISDIR (path_stat.st_mode))
+ {
+ printf ("my_mkdir: pathname %s is not a directory\n", dir);
+ abort ();
+ }
+
+ err = mkdir (dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
+ if (err != 0)
+ {
+ perror ("my_mkdir: failed to create directory");
+ abort ();
+ }
+}
+
+void
+my_rmdir (const char *dir)
+{
+ rmdir (dir);
+}
+
+#endif /* !defined(__WIN32__) || defined(__CYGWIN__) */
! { dg-do run }
+! { dg-additional-sources read_dir-aux.c }
+!
! PR67367
+
program bug
+ use iso_c_binding
implicit none
+
+ interface
+ integer(c_int) function expect_open_to_fail () bind(C)
+ import
+ end
+ subroutine my_verify_not_exists(s) bind(C)
+ ! Aborts if the passed pathname (still) exists
+ import
+ character(len=1,kind=c_char) :: s(*)
+ end subroutine
+ subroutine my_mkdir(s) bind(C)
+ ! Call POSIX's mkdir - and ignore fails due to
+ ! existing directories but fail otherwise
+ import
+ character(len=1,kind=c_char) :: s(*)
+ end subroutine
+ subroutine my_rmdir(s) bind(C)
+ ! Call POSIX's rmdir - and ignore fails
+ import
+ character(len=1,kind=c_char) :: s(*)
+ end subroutine
+ end interface
+
+ character(len=*), parameter :: sdir = "junko.dir"
+ character(len=*,kind=c_char), parameter :: c_sdir = sdir // c_null_char
+
character(len=1) :: c
- character(len=256) :: message
integer ios
- call system('[ -d junko.dir ] || mkdir junko.dir')
- open(unit=10, file='junko.dir',iostat=ios,action='read',access='stream')
+
+ if (expect_open_to_fail () /= 0) then
+ ! Windows is documented to fail with EACCESS when trying to open a
+ ! directory. However, target macros such as __WIN32__ are not defined
+ ! in Fortran; hence, we use a detour via this C function.
+ ! Check for '.' which is a known-to-exist directory:
+ open(unit=10, file='.',iostat=ios,action='read',access='stream')
+ if (ios == 0) &
+ stop 3 ! Error: open to fail (EACCESS)
+ stop 0 ! OK
+ endif
+
+ call my_mkdir(c_sdir)
+ open(unit=10, file=sdir,iostat=ios,action='read',access='stream')
+
if (ios.ne.0) then
- call system('rmdir junko.dir')
+ call my_rmdir(c_sdir)
STOP 1
end if
read(10, iostat=ios) c
- if (ios.ne.21.and.ios.ne.0) then
+ if (ios.ne.21.and.ios.ne.0) then ! EISDIR has often the value 21
close(10, status='delete')
+ call my_verify_not_exists(c_sdir)
STOP 2
end if
close(10, status='delete')
+ call my_verify_not_exists(c_sdir)
end program bug