Skip to content

feat: get_cwd and set_cwd #1014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions doc/specs/stdlib_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,80 @@ Subroutine

---

## `get_cwd` - Gets the current working directory

### Status

Experimental

### Description

This subroutine retrieves the current working directory the running process is executing from.
It is designed to work across multiple platforms. On Windows, paths with both forward `/` and backward `\` slashes are accepted.

### Syntax

`call [[stdlib_system(module):get_cwd(subroutine)]] (cwd [, err])`

### Class

Subroutine

### Arguments

`cwd`: Shall be a character string for receiving the path of the current working directory (cwd). It is an `intent(out)` argument.

`err`(optional): Shall be of type `state_type`, and is used for error handling. It is an `intent(out)` argument.

### Return values

`err` is an optional state return flag. On error if not requested, an `FS_ERROR` will trigger an error stop.

### Example

```fortran
{!example/system/example_cwd.f90!}
```

---

## `set_cwd` - Sets the current working directory

### Status

Experimental

### Description

This subrotine sets the current working directory the process is executing from.
It is designed to work across multiple platforms. On Windows, paths with both forward `/` and backward `\` slashes are accepted.

### Syntax

`call [[stdlib_system(module):set_cwd(subroutine)]] (path [, err])`

### Class

Subroutine

### Arguments

`path`: Shall be a character string containing the path of the directory. It is an `intent(in)` argument.

`err`(optional): Shall be of type `state_type`, and is used for error handling. It is an `intent(out)` argument.

### Return values

`err` is an optional state return flag. On error if not requested, an `FS_ERROR` will trigger an error stop.

### Example

```fortran
{!example/system/example_cwd.f90!}
```

---

## `null_device` - Return the null device file path

### Status
Expand Down Expand Up @@ -682,6 +756,8 @@ None.
{!example/system/example_null_device.f90!}
```

---

## `delete_file` - Delete a file

### Status
Expand Down Expand Up @@ -723,6 +799,8 @@ The file is removed from the filesystem if the operation is successful. If the o
{!example/system/example_delete_file.f90!}
```

---

## `join_path` - Joins the provided paths according to the OS

### Status
Expand Down Expand Up @@ -785,6 +863,8 @@ The result is an `allocatable` character string or `type(string_type)`
{!example/system/example_path_join.f90!}
```

---

## `split_path` - splits a path immediately following the last separator

### Status
Expand Down Expand Up @@ -825,6 +905,8 @@ The splitted path. `head` and `tail`.
{!example/system/example_path_split_path.f90!}
```

---

## `base_name` - The last part of a path

### Status
Expand Down Expand Up @@ -860,6 +942,8 @@ A character string or `type(string_type)`.
{!example/system/example_path_base_name.f90!}
```

---

## `dir_name` - Everything except the last part of the path

### Status
Expand Down
1 change: 1 addition & 0 deletions example/system/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ ADD_EXAMPLE(path_base_name)
ADD_EXAMPLE(path_dir_name)
ADD_EXAMPLE(make_directory)
ADD_EXAMPLE(remove_directory)
ADD_EXAMPLE(cwd)
32 changes: 32 additions & 0 deletions example/system/example_cwd.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
! Illustrate the usage of `get_cwd`, `set_cwd`
program example_cwd
use stdlib_system, only: get_cwd, set_cwd
use stdlib_error, only: state_type
implicit none

character(len=:), allocatable :: path
type(state_type) :: err

call get_cwd(path, err)

if (err%error()) then
print *, "Error getting current working directory: "//err%print()
end if

print *, "CWD: "//path

call set_cwd("./src", err)

if (err%error()) then
print *, "Error setting current working directory: "//err%print()
end if

call get_cwd(path, err)

if (err%error()) then
print *, "Error getting current working directory after using set_cwd: "//err%print()
end if

print *, "CWD: "//path
end program example_cwd

120 changes: 100 additions & 20 deletions src/stdlib_system.F90
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module stdlib_system
use, intrinsic :: iso_c_binding, only : c_int, c_long, c_ptr, c_null_ptr, c_int64_t, c_size_t, &
c_f_pointer
use stdlib_kinds, only: int64, dp, c_bool, c_char
use stdlib_strings, only: to_c_char, find
use stdlib_strings, only: to_c_char, find, to_string
use stdlib_string_type, only: string_type
use stdlib_optval, only: optval
use stdlib_error, only: state_type, STDLIB_SUCCESS, STDLIB_FS_ERROR
Expand Down Expand Up @@ -156,6 +156,32 @@ module stdlib_system
!!
public :: remove_directory

!! version: experimental
!!
!! Gets the current working directory of the process
!! ([Specification](../page/specs/stdlib_system.html#get_cwd))
!!
!! ### Summary
!! Gets the current working directory.
!!
!! ### Description
!! This subroutine gets the current working directory the process is executing from.
!!
public :: get_cwd

!! version: experimental
!!
!! Sets the current working directory of the process
!! ([Specification](../page/specs/stdlib_system.html#set_cwd))
!!
!! ### Summary
!! Changes the current working directory to the one specified.
!!
!! ### Description
!! This subroutine sets the current working directory the process is executing from.
!!
public :: set_cwd

!! version: experimental
!!
!! Deletes a specified file from the filesystem.
Expand Down Expand Up @@ -896,6 +922,25 @@ end function stdlib_is_directory

end function is_directory

! A Helper function to convert C character arrays to Fortran character strings
function to_f_char(c_str_ptr, len) result(f_str)
type(c_ptr), intent(in) :: c_str_ptr
! length of the string excluding the null character
integer(kind=c_size_t), intent(in) :: len
character(:), allocatable :: f_str

integer :: i
character(kind=c_char), pointer :: c_str(:)

call c_f_pointer(c_str_ptr, c_str, [len])

allocate(character(len=len) :: f_str)

do concurrent (i=1:len)
f_str(i:i) = c_str(i)
end do
end function to_f_char

! A helper function to get the result of the C function `strerror`.
! `strerror` is a function provided by `<string.h>`.
! It returns a string describing the meaning of `errno` in the C header `<errno.h>`
Expand All @@ -911,18 +956,11 @@ end function strerror
end interface

type(c_ptr) :: c_str_ptr
integer(c_size_t) :: len, i
character(kind=c_char), pointer :: c_str(:)
integer(c_size_t) :: len

c_str_ptr = strerror(len)

call c_f_pointer(c_str_ptr, c_str, [len])

allocate(character(len=len) :: str)

do concurrent (i=1:len)
str(i:i) = c_str(i)
end do
str = to_f_char(c_str_ptr, len)
end function c_get_strerror

!! makes an empty directory
Expand Down Expand Up @@ -1024,6 +1062,56 @@ end function stdlib_remove_directory

end subroutine remove_directory

subroutine get_cwd(cwd, err)
character(:), allocatable, intent(out) :: cwd
type(state_type), optional, intent(out) :: err
type(state_type) :: err0

interface
type(c_ptr) function stdlib_get_cwd(len, stat) bind(C, name='stdlib_get_cwd')
import c_ptr, c_size_t
integer(c_size_t), intent(out) :: len
integer :: stat
end function stdlib_get_cwd
end interface

type(c_ptr) :: c_str_ptr
integer(c_size_t) :: len
integer :: stat

c_str_ptr = stdlib_get_cwd(len, stat)

if (stat /= 0) then
err0 = FS_ERROR_CODE(stat, c_get_strerror())
call err0%handle(err)
end if

cwd = to_f_char(c_str_ptr, len)

end subroutine get_cwd

subroutine set_cwd(path, err)
character(len=*), intent(in) :: path
type(state_type), optional, intent(out) :: err
type(state_type) :: err0

interface
integer function stdlib_set_cwd(path) bind(C, name='stdlib_set_cwd')
import c_char
character(kind=c_char), intent(in) :: path(*)
end function stdlib_set_cwd
end interface

integer :: code

code = stdlib_set_cwd(to_c_char(trim(path)))

if (code /= 0) then
err0 = FS_ERROR_CODE(code, c_get_strerror())
call err0%handle(err)
end if
end subroutine set_cwd

!> Returns the file path of the null device for the current operating system.
!>
!> Version: Helper function.
Expand All @@ -1042,21 +1130,13 @@ end function process_null_device

end interface

integer(c_size_t) :: i, len
integer(c_size_t) :: len
type(c_ptr) :: c_path_ptr
character(kind=c_char), pointer :: c_path(:)

! Call the C function to get the null device path and its length
c_path_ptr = process_null_device(len)
call c_f_pointer(c_path_ptr,c_path,[len])

! Allocate the Fortran string with the length returned from C
allocate(character(len=len) :: path)

do concurrent (i=1:len)
path(i:i) = c_path(i)
end do

path = to_f_char(c_path_ptr, len)
end function null_device

!> Delete a file at the given path.
Expand Down
47 changes: 47 additions & 0 deletions src/stdlib_system.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
Expand Down Expand Up @@ -44,3 +46,48 @@ int stdlib_remove_directory(const char* path){

return (!code) ? 0 : errno;
}

// Wrapper to the platform's `getcwd`(get current working directory) call.
// Uses `getcwd` on unix, `_getcwd` on windows.
// Returns the cwd, sets the length of cwd and the `stat` of the operation.
char* stdlib_get_cwd(size_t* len, int* stat){
*stat = 0;
#ifdef _WIN32
char* buffer;
buffer = _getcwd(NULL, 0);

if (buffer == NULL) {
*stat = errno;
return NULL;
}

*len = strlen(buffer);
return buffer;
#else
char buffer[PATH_MAX + 1];
if (!getcwd(buffer, sizeof(buffer))) {
*stat = errno;
}

*len = strlen(buffer);

char* res = malloc(*len);
strncpy(res, buffer, *len);

return res;
#endif /* ifdef _WIN32 */
}

// Wrapper to the platform's `chdir`(change directory) call.
// Uses `chdir` on unix, `_chdir` on windows.
// Returns 0 if successful, otherwise returns the `errno`.
int stdlib_set_cwd(char* path) {
int code;
#ifdef _WIN32
code = _chdir(path);
#else
code = chdir(path);
#endif /* ifdef _WIN32 */

return (code == -1) ? errno : 0;
}
Loading
Loading