@@ -1,6 +1,5 @@ |
| 1 | 1 | module filesystem_ops |
| 2 | 2 | use iso_c_binding |
| 3 | | - use filesystem_c_interface |
| 4 | 3 | implicit none |
| 5 | 4 | private |
| 6 | 5 | |
@@ -21,12 +20,9 @@ contains |
| 21 | 20 | function list_directory(path) result(entries) |
| 22 | 21 | character(len=*), intent(in) :: path |
| 23 | 22 | type(file_entry), dimension(MAX_FILES) :: entries |
| 24 | | - type(c_ptr) :: dir_handle, entry_ptr |
| 25 | | - type(c_dirent), pointer :: dir_entry |
| 26 | | - type(c_stat_buf), target :: stat_buf |
| 27 | | - character(kind=c_char, len=MAX_PATH), target :: c_path |
| 28 | | - character(len=MAX_PATH) :: full_path |
| 29 | | - integer :: count, i, stat_result |
| 23 | + character(len=MAX_PATH) :: temp_file |
| 24 | + character(len=MAX_PATH) :: line, full_path |
| 25 | + integer :: unit, ios, count, i |
| 30 | 26 | |
| 31 | 27 | ! Initialize entries |
| 32 | 28 | do i = 1, MAX_FILES |
@@ -35,43 +31,41 @@ contains |
| 35 | 31 | entries(i)%size = 0 |
| 36 | 32 | end do |
| 37 | 33 | |
| 38 | | - ! Convert Fortran string to C string |
| 39 | | - c_path = trim(path) // c_null_char |
| 34 | + ! Create a temporary file to store ls output |
| 35 | + temp_file = "/tmp/fortress_ls_temp.txt" |
| 40 | 36 | |
| 41 | | - ! Open directory |
| 42 | | - dir_handle = c_opendir(c_loc(c_path)) |
| 43 | | - if (.not. c_associated(dir_handle)) return |
| 37 | + ! Use ls -1 for simpler parsing |
| 38 | + call execute_command_line("ls -1a '" // trim(path) // "' > " // trim(temp_file), wait=.true.) |
| 39 | + |
| 40 | + ! Open and read the temp file |
| 41 | + open(newunit=unit, file=temp_file, status='old', action='read', iostat=ios) |
| 42 | + if (ios /= 0) return |
| 44 | 43 | |
| 45 | 44 | count = 0 |
| 45 | + |
| 46 | 46 | do |
| 47 | | - entry_ptr = c_readdir(dir_handle) |
| 48 | | - if (.not. c_associated(entry_ptr)) exit |
| 47 | + read(unit, '(a)', iostat=ios) line |
| 48 | + if (ios /= 0) exit |
| 49 | + if (count >= MAX_FILES) exit |
| 50 | + if (len_trim(line) == 0) cycle |
| 49 | 51 | |
| 50 | 52 | count = count + 1 |
| 51 | | - if (count > MAX_FILES) exit |
| 52 | | - |
| 53 | | - call c_f_pointer(entry_ptr, dir_entry) |
| 54 | 53 | |
| 55 | | - ! Convert C string to Fortran string |
| 56 | | - entries(count)%name = "" |
| 57 | | - do i = 1, 255 |
| 58 | | - if (dir_entry%d_name(i) == c_null_char) exit |
| 59 | | - entries(count)%name(i:i) = dir_entry%d_name(i) |
| 60 | | - end do |
| 54 | + ! Store the filename |
| 55 | + entries(count)%name = trim(adjustl(line)) |
| 61 | 56 | |
| 62 | | - ! Get file stats |
| 57 | + ! Check if it's a directory using the is_directory function |
| 63 | 58 | full_path = trim(path) // "/" // trim(entries(count)%name) |
| 64 | | - c_path = trim(full_path) // c_null_char |
| 65 | | - stat_result = c_stat(c_loc(c_path), c_loc(stat_buf)) |
| 59 | + entries(count)%is_dir = is_directory(full_path) |
| 66 | 60 | |
| 67 | | - if (stat_result == 0) then |
| 68 | | - entries(count)%is_dir = S_ISDIR(stat_buf%st_mode) |
| 69 | | - entries(count)%size = stat_buf%st_size |
| 70 | | - end if |
| 61 | + ! For now, set size to 0 (could add stat later) |
| 62 | + entries(count)%size = 0 |
| 71 | 63 | end do |
| 72 | 64 | |
| 73 | | - ! Close directory |
| 74 | | - i = c_closedir(dir_handle) |
| 65 | + close(unit) |
| 66 | + |
| 67 | + ! Clean up temp file |
| 68 | + call execute_command_line("rm -f " // trim(temp_file), wait=.false.) |
| 75 | 69 | end function list_directory |
| 76 | 70 | |
| 77 | 71 | function get_parent_dir(path) result(parent) |
@@ -95,40 +89,37 @@ contains |
| 95 | 89 | function is_directory(path) result(is_dir) |
| 96 | 90 | character(len=*), intent(in) :: path |
| 97 | 91 | logical :: is_dir |
| 98 | | - type(c_stat_buf), target :: stat_buf |
| 99 | | - character(kind=c_char, len=MAX_PATH), target :: c_path |
| 100 | | - integer :: stat_result |
| 92 | + integer :: stat |
| 101 | 93 | |
| 102 | 94 | is_dir = .false. |
| 103 | | - c_path = trim(path) // c_null_char |
| 104 | | - stat_result = c_stat(c_loc(c_path), c_loc(stat_buf)) |
| 105 | 95 | |
| 106 | | - if (stat_result == 0) then |
| 107 | | - is_dir = S_ISDIR(stat_buf%st_mode) |
| 108 | | - end if |
| 96 | + ! Use test command to check if it's a directory |
| 97 | + call execute_command_line("test -d '" // trim(path) // "'", & |
| 98 | + exitstat=stat, wait=.true.) |
| 99 | + is_dir = (stat == 0) |
| 109 | 100 | end function is_directory |
| 110 | 101 | |
| 111 | 102 | function get_current_dir() result(cwd) |
| 112 | 103 | character(len=MAX_PATH) :: cwd |
| 113 | | - character(kind=c_char, len=MAX_PATH), target :: c_buffer |
| 114 | | - type(c_ptr) :: result_ptr |
| 115 | | - |
| 116 | | - c_buffer = "" |
| 117 | | - result_ptr = c_getcwd(c_loc(c_buffer), int(MAX_PATH, c_size_t)) |
| 118 | | - |
| 119 | | - if (c_associated(result_ptr)) then |
| 120 | | - ! Convert C string to Fortran string |
| 121 | | - cwd = "" |
| 122 | | - block |
| 123 | | - integer :: i |
| 124 | | - do i = 1, MAX_PATH |
| 125 | | - if (c_buffer(i:i) == c_null_char) exit |
| 126 | | - cwd(i:i) = c_buffer(i:i) |
| 127 | | - end do |
| 128 | | - end block |
| 129 | | - else |
| 104 | + character(len=MAX_PATH) :: temp_file |
| 105 | + integer :: unit, ios |
| 106 | + |
| 107 | + ! Create a temporary file to store pwd output |
| 108 | + temp_file = "/tmp/fortress_pwd_temp.txt" |
| 109 | + call execute_command_line("pwd > " // trim(temp_file), wait=.true.) |
| 110 | + |
| 111 | + ! Read the current directory |
| 112 | + open(newunit=unit, file=temp_file, status='old', action='read', iostat=ios) |
| 113 | + if (ios /= 0) then |
| 130 | 114 | cwd = "." |
| 115 | + else |
| 116 | + read(unit, '(a)', iostat=ios) cwd |
| 117 | + if (ios /= 0) cwd = "." |
| 118 | + close(unit) |
| 131 | 119 | end if |
| 120 | + |
| 121 | + ! Clean up temp file |
| 122 | + call execute_command_line("rm -f " // trim(temp_file), wait=.false.) |
| 132 | 123 | end function get_current_dir |
| 133 | 124 | |
| 134 | 125 | end module filesystem_ops |