aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeffrey Armstrong <jeff@approximatrix.com>2021-04-28 11:31:18 -0400
committerJeffrey Armstrong <jeff@approximatrix.com>2021-04-28 11:31:18 -0400
commite789ce4a4bc1f0894a707d2a141bbf357e0ba2d5 (patch)
tree804d6ae08b68d144220245b3daa6e5955b7c970a
parentfae4fd7edddf85c3bde3fb6a971eb3fdcffad2d4 (diff)
downloadlevitating-e789ce4a4bc1f0894a707d2a141bbf357e0ba2d5.tar.gz
levitating-e789ce4a4bc1f0894a707d2a141bbf357e0ba2d5.zip
Directory listing on UNIXy systems modified to work on macOS
-rw-r--r--common/utilities.F9095
1 files changed, 51 insertions, 44 deletions
diff --git a/common/utilities.F90 b/common/utilities.F90
index f9868be..093595b 100644
--- a/common/utilities.F90
+++ b/common/utilities.F90
@@ -296,6 +296,28 @@ contains
end subroutine replace_field_int
+ function get_line_count_in_file(filename) result(res)
+ implicit none
+
+ character(*)::filename
+ integer::res
+
+ integer::unum, ierr
+ character(1024)::line
+
+ open(newunit=unum, file=filename, action='read')
+ res = 0
+
+ read(unum, '(A)', iostat=ierr) line
+ do while(ierr == 0)
+ res = res + 1
+ read(unum, '(A)', iostat=ierr) line
+ end do
+
+ close(unum)
+
+ end function get_line_count_in_file
+
function get_directories_in_directory(directory) result(res)
implicit none
@@ -375,7 +397,10 @@ contains
character(DIR_LIST_STRING_LENGTH), dimension(:), pointer::res
character(80)::line
character(len=:), pointer::tempfile
- integer::dcount, total_count, unum, ierr, i, n
+
+ logical, dimension(:), allocatable::is_real_file
+
+ integer::unum, ierr, i, n, j
tempfile => generate_temporary_filename()
res => null()
@@ -387,75 +412,57 @@ contains
call execute_command_line("ls -l "//trim(directory)//" > "//trim(tempfile), &
wait=.true.)
#endif
-
+
+ n = get_line_count_in_file(tempfile)
+ allocate(is_real_file(n))
+ is_real_file = .FALSE.
+
open(newunit=unum, file=tempfile, action='read')
- dcount = 0
- total_count = 0
-
+#ifndef WINDOWS
! Count directories first
+ i = 0
read(unum, '(A)', iostat=ierr) line
do while(ierr == 0)
-#ifndef WINDOWS
- if(line(1:1) == 'd') then
- dcount = dcount + 1
- end if
-#endif
-
- ! ls puts a nonsense entry first. harmless, but we don't want it
- if(line(1:6) /= "total " .and. len_trim(line) > 0) then
- total_count = total_count + 1
- end if
-
+ i = i + 1
+ if(len_trim(line) > 0 .and. line(1:1) == '-' .and. line(1:6) /= "total ") then
+ is_real_file(i) = .TRUE.
+ end if
read(unum, '(A)', iostat=ierr) line
end do
+ n = count(is_real_file)
close(unum)
- n = total_count - dcount
- !print *, "Total: ", total_count, "Dirs:", dcount, "Files:", n
-
- if(n > 0) then
- allocate(res(n))
- !print *, "Size: ", size(res)
-
- ! We don't need to recreate the file on Windows. It's fine.
-#ifndef WINDOWS
- ! Now call ls, but group directories first
- call execute_command_line("ls --group-directories-first "//trim(directory)//" > "//trim(tempfile), &
- wait=.true.)
+ call execute_command_line("ls "//trim(directory)//" > "//trim(tempfile), &
+ wait=.true.)
+
+ open(newunit=unum, file=tempfile, action='read')
+#else
+ is_real_file = .TRUE.
#endif
- open(newunit=unum, file=tempfile, action='read')
-
- i = 0
-
- ! First, skip directories
- do while(i < dcount)
- read(unum, '(A)', iostat=ierr) line
- if(line(1:6) /= "total ") then
- i = i + 1
- end if
- end do
+ if(n > 0) then
+ allocate(res(n))
! Now we can read files
i = 0
+ j = 0
read(unum, '(A)', iostat=ierr) line
do while(ierr == 0 .and. i < n)
- if(line(1:6) /= "total ") then
+ j = j + 1
+ if(is_real_file(j)) then
i = i + 1
res(i) = trim(line)
-
- !print *, i, trim(res(i))//"|"
end if
read(unum, '(A)', iostat=ierr) line
end do
-
- close(unum)
end if
+ close(unum)
+
call unlink(tempfile)
end function get_files_in_directory