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

Merged
merged 10 commits into from
Aug 10, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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

It gets the current working directory associated with the process calling this subroutine.
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 containing the path of the current working directory (cwd). It is an `intent(out)` argument.

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

### Return values

The `err` is set accordingly.

### Example

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

---

## `set_cwd` - Sets the current working directory

### Status

Experimental

### Description

It sets the current working directory associated with the process calling this subroutine.
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`: Shall be of type `state_type`, for error handling. It is an `intent(out)` argument.

### Return values

The `err` is set accordingly.

### 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)
33 changes: 33 additions & 0 deletions example/system/example_cwd.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
! 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()
return
end if

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

84 changes: 83 additions & 1 deletion 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 of the process calling this function.
!!
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 of the process calling this function to the one specified.
!!
public :: set_cwd

!! version: experimental
!!
!! Deletes a specified file from the filesystem.
Expand Down Expand Up @@ -1024,6 +1050,62 @@ end function stdlib_remove_directory

end subroutine remove_directory

subroutine get_cwd(cwd, err)
character(:), allocatable, intent(out) :: cwd
type(state_type), 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, i
integer :: stat
character(kind=c_char), pointer :: c_str(:)

c_str_ptr = stdlib_get_cwd(len, stat)

if (stat /= 0) then
err0 = state_type(STDLIB_FS_ERROR, "code: ", to_string(stat)//",", c_get_strerror())
call err0%handle(err)
end if

call c_f_pointer(c_str_ptr, c_str, [len])

allocate(character(len=len) :: cwd)

do concurrent (i=1:len)
cwd(i:i) = c_str(i)
end do
end subroutine get_cwd

subroutine set_cwd(path, err)
character(len=*), intent(in) :: path
type(state_type), 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 = state_type(STDLIB_FS_ERROR, "code: ", to_string(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 Down
42 changes: 42 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,43 @@ int stdlib_remove_directory(const char* path){

return (!code) ? 0 : errno;
}

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 */
}

int stdlib_set_cwd(char* path) {
int code;
#ifdef _WIN32
code = _chdir(path);
#else
code = chdir(path);
#endif /* ifdef _WIN32 */

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