fortrangoingonforty/fortress / 2c7777f

Browse files

fixes, running first pass

Authored by espadonne
SHA
2c7777f5d5773e1bd17021a524279aaec5576aca
Parents
8cf6144
Tree
dafde70

2 changed files

StatusFile+-
M src/filesystem/operations.f90 48 57
M src/terminal/screen.f90 24 8
src/filesystem/operations.f90modified
@@ -1,6 +1,5 @@
11
 module filesystem_ops
22
     use iso_c_binding
3
-    use filesystem_c_interface
43
     implicit none
54
     private
65
 
@@ -21,12 +20,9 @@ contains
2120
     function list_directory(path) result(entries)
2221
         character(len=*), intent(in) :: path
2322
         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
3026
 
3127
         ! Initialize entries
3228
         do i = 1, MAX_FILES
@@ -35,43 +31,41 @@ contains
3531
             entries(i)%size = 0
3632
         end do
3733
 
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"
4036
 
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
4443
 
4544
         count = 0
45
+
4646
         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
4951
 
5052
             count = count + 1
51
-            if (count > MAX_FILES) exit
52
-
53
-            call c_f_pointer(entry_ptr, dir_entry)
5453
 
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))
6156
 
62
-            ! Get file stats
57
+            ! Check if it's a directory using the is_directory function
6358
             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)
6660
 
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
7163
         end do
7264
 
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.)
7569
     end function list_directory
7670
 
7771
     function get_parent_dir(path) result(parent)
@@ -95,40 +89,37 @@ contains
9589
     function is_directory(path) result(is_dir)
9690
         character(len=*), intent(in) :: path
9791
         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
10193
 
10294
         is_dir = .false.
103
-        c_path = trim(path) // c_null_char
104
-        stat_result = c_stat(c_loc(c_path), c_loc(stat_buf))
10595
 
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)
109100
     end function is_directory
110101
 
111102
     function get_current_dir() result(cwd)
112103
         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
130114
             cwd = "."
115
+        else
116
+            read(unit, '(a)', iostat=ios) cwd
117
+            if (ios /= 0) cwd = "."
118
+            close(unit)
131119
         end if
120
+
121
+        ! Clean up temp file
122
+        call execute_command_line("rm -f " // trim(temp_file), wait=.false.)
132123
     end function get_current_dir
133124
 
134125
 end module filesystem_ops
src/terminal/screen.f90modified
@@ -1,6 +1,6 @@
11
 module terminal_screen
22
     use iso_c_binding
3
-    use iso_fortran_env, only: output_unit
3
+    use iso_fortran_env, only: output_unit, input_unit
44
     implicit none
55
     private
66
 
@@ -44,19 +44,35 @@ contains
4444
 
4545
     subroutine get_terminal_size(rows, cols)
4646
         integer, intent(out) :: rows, cols
47
-        character(len=100) :: output
48
-        integer :: stat
47
+        character(len=20) :: env_lines, env_cols
48
+        integer :: ios
4949
 
5050
         ! Default values
5151
         rows = 24
5252
         cols = 80
5353
 
54
-        ! Try to get actual terminal size
55
-        call execute_command_line("tput lines", wait=.true., cmdstat=stat, cmdmsg=output)
56
-        if (stat == 0) read(output, *) rows
54
+        ! Try environment variables first
55
+        call get_environment_variable("LINES", env_lines)
56
+        call get_environment_variable("COLUMNS", env_cols)
5757
 
58
-        call execute_command_line("tput cols", wait=.true., cmdstat=stat, cmdmsg=output)
59
-        if (stat == 0) read(output, *) cols
58
+        if (len_trim(env_lines) > 0) then
59
+            read(env_lines, *, iostat=ios) rows
60
+            if (ios /= 0) rows = 24
61
+        end if
62
+
63
+        if (len_trim(env_cols) > 0) then
64
+            read(env_cols, *, iostat=ios) cols
65
+            if (ios /= 0) cols = 80
66
+        end if
67
+
68
+        ! If environment variables not set, try using ANSI escape sequence
69
+        ! This is a more complex method but works on most terminals
70
+        if (len_trim(env_lines) == 0 .or. len_trim(env_cols) == 0) then
71
+            ! For now, use reasonable defaults that work on most terminals
72
+            ! Could implement CSI DSR (Device Status Report) later
73
+            rows = 40  ! Common terminal height
74
+            cols = 100 ! Common terminal width
75
+        end if
6076
     end subroutine get_terminal_size
6177
 
6278
 end module terminal_screen