@@ -2,7 +2,7 @@ module stdlib_system
22use , intrinsic :: iso_c_binding, only : c_int, c_long, c_ptr, c_null_ptr, c_int64_t, c_size_t, &
33 c_f_pointer
44use stdlib_kinds, only: int64, dp, c_bool, c_char
5- use stdlib_strings, only: to_c_char, find
5+ use stdlib_strings, only: to_c_char, find, to_string
66use stdlib_string_type, only: string_type
77use stdlib_optval, only: optval
88use stdlib_error, only: state_type, STDLIB_SUCCESS, STDLIB_FS_ERROR
@@ -156,6 +156,32 @@ module stdlib_system
156156! !
157157public :: remove_directory
158158
159+ ! ! version: experimental
160+ ! !
161+ ! ! Gets the current working directory of the process
162+ ! ! ([Specification](../page/specs/stdlib_system.html#get_cwd))
163+ ! !
164+ ! ! ### Summary
165+ ! ! Gets the current working directory.
166+ ! !
167+ ! ! ### Description
168+ ! ! This subroutine gets the current working directory the process is executing from.
169+ ! !
170+ public :: get_cwd
171+ 172+ ! ! version: experimental
173+ ! !
174+ ! ! Sets the current working directory of the process
175+ ! ! ([Specification](../page/specs/stdlib_system.html#set_cwd))
176+ ! !
177+ ! ! ### Summary
178+ ! ! Changes the current working directory to the one specified.
179+ ! !
180+ ! ! ### Description
181+ ! ! This subroutine sets the current working directory the process is executing from.
182+ ! !
183+ public :: set_cwd
184+ 159185! ! version: experimental
160186! !
161187! ! Deletes a specified file from the filesystem.
@@ -896,6 +922,25 @@ end function stdlib_is_directory
896922
897923end function is_directory
898924
925+ ! A Helper function to convert C character arrays to Fortran character strings
926+ function to_f_char (c_str_ptr , len ) result(f_str)
927+ type (c_ptr), intent (in ) :: c_str_ptr
928+ ! length of the string excluding the null character
929+ integer (kind= c_size_t), intent (in ) :: len
930+ character (:), allocatable :: f_str
931+ 932+ integer :: i
933+ character (kind= c_char), pointer :: c_str(:)
934+ 935+ call c_f_pointer(c_str_ptr, c_str, [len])
936+ 937+ allocate (character (len= len) :: f_str)
938+ 939+ do concurrent (i= 1 :len)
940+ f_str(i:i) = c_str(i)
941+ end do
942+ end function to_f_char
943+ 899944! A helper function to get the result of the C function `strerror`.
900945! `strerror` is a function provided by `<string.h>`.
901946! It returns a string describing the meaning of `errno` in the C header `<errno.h>`
@@ -911,18 +956,11 @@ end function strerror
911956 end interface
912957
913958 type (c_ptr) :: c_str_ptr
914- integer (c_size_t) :: len, i
915- character (kind= c_char), pointer :: c_str(:)
959+ integer (c_size_t) :: len
916960
917961 c_str_ptr = strerror(len)
918962
919- call c_f_pointer(c_str_ptr, c_str, [len])
920- 921- allocate (character (len= len) :: str)
922- 923- do concurrent (i= 1 :len)
924- str(i:i) = c_str(i)
925- end do
963+ str = to_f_char(c_str_ptr, len)
926964end function c_get_strerror
927965
928966! ! makes an empty directory
@@ -1024,6 +1062,56 @@ end function stdlib_remove_directory
10241062
10251063end subroutine remove_directory
10261064
1065+ subroutine get_cwd (cwd , err )
1066+ character (:), allocatable , intent (out ) :: cwd
1067+ type (state_type), optional , intent (out ) :: err
1068+ type (state_type) :: err0
1069+ 1070+ interface
1071+ type (c_ptr) function stdlib_get_cwd(len, stat) bind(C, name= ' stdlib_get_cwd' )
1072+ import c_ptr, c_size_t
1073+ integer (c_size_t), intent (out ) :: len
1074+ integer :: stat
1075+ end function stdlib_get_cwd
1076+ end interface
1077+ 1078+ type (c_ptr) :: c_str_ptr
1079+ integer (c_size_t) :: len
1080+ integer :: stat
1081+ 1082+ c_str_ptr = stdlib_get_cwd(len, stat)
1083+ 1084+ if (stat /= 0 ) then
1085+ err0 = FS_ERROR_CODE(stat, c_get_strerror())
1086+ call err0% handle(err)
1087+ end if
1088+ 1089+ cwd = to_f_char(c_str_ptr, len)
1090+ 1091+ end subroutine get_cwd
1092+ 1093+ subroutine set_cwd (path , err )
1094+ character (len=* ), intent (in ) :: path
1095+ type (state_type), optional , intent (out ) :: err
1096+ type (state_type) :: err0
1097+ 1098+ interface
1099+ integer function stdlib_set_cwd (path ) bind(C, name= ' stdlib_set_cwd' )
1100+ import c_char
1101+ character (kind= c_char), intent (in ) :: path(* )
1102+ end function stdlib_set_cwd
1103+ end interface
1104+ 1105+ integer :: code
1106+ 1107+ code = stdlib_set_cwd(to_c_char(trim (path)))
1108+ 1109+ if (code /= 0 ) then
1110+ err0 = FS_ERROR_CODE(code, c_get_strerror())
1111+ call err0% handle(err)
1112+ end if
1113+ end subroutine set_cwd
1114+ 10271115! > Returns the file path of the null device for the current operating system.
10281116! >
10291117! > Version: Helper function.
@@ -1042,21 +1130,13 @@ end function process_null_device
10421130
10431131 end interface
10441132
1045- integer (c_size_t) :: i, len
1133+ integer (c_size_t) :: len
10461134 type (c_ptr) :: c_path_ptr
1047- character (kind= c_char), pointer :: c_path(:)
10481135
10491136 ! Call the C function to get the null device path and its length
10501137 c_path_ptr = process_null_device(len)
1051- call c_f_pointer(c_path_ptr,c_path,[len])
10521138
1053- ! Allocate the Fortran string with the length returned from C
1054- allocate (character (len= len) :: path)
1055- 1056- do concurrent (i= 1 :len)
1057- path(i:i) = c_path(i)
1058- end do
1059- 1139+ path = to_f_char(c_path_ptr, len)
10601140end function null_device
10611141
10621142! > Delete a file at the given path.
0 commit comments