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
150 changes: 150 additions & 0 deletions doc/specs/stdlib_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,156 @@ The function returns a `logical` value:

---

## `make_directory` - Creates an empty directory

### Status

Experimental

### Description

It creates an empty directory.
It is designed to work across multiple platforms. On Windows, paths with both forward `/` and backward `\` slashes are accepted.

### Syntax

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

### Class

Subroutine

### Arguments

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

`mode`: Shall be a scalar integer indicating the permission bits required (Not applicable to Windows). It is an `optional, intent(in)` argument.

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

### Return values

The `err` is set accordingly.

### Example

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

---

## `remove_directory` - Removes an empty directory

### Status

Experimental

### Description

It deletes an empty directory.
It is designed to work across multiple platforms. On Windows, paths with both forward `/` and backward `\` slashes are accepted.

### Syntax

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

### Class

Subroutine

### Arguments

`path`: Shall be a character string containing the path of the directory to create. 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_remove_directory.f90!}
```

---

## `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
3 changes: 3 additions & 0 deletions example/system/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ ADD_EXAMPLE(process_5)
ADD_EXAMPLE(process_6)
ADD_EXAMPLE(process_7)
ADD_EXAMPLE(sleep)
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

17 changes: 17 additions & 0 deletions example/system/example_make_directory.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
! Illustrate the usage of make_directory
program example_make_directory
use stdlib_system, only: make_directory, is_directory
use stdlib_error, only: state_type
implicit none

type(state_type) :: err

call make_directory("test", err=err)

if (err%error()) then
print *, err%print()
else
print *, "directory created sucessfully"
end if

end program example_make_directory
17 changes: 17 additions & 0 deletions example/system/example_remove_directory.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
! Illustrate the usage of remove_directory
program example_remove_directory
use stdlib_system, only: make_directory, is_directory, remove_directory
use stdlib_error, only: state_type
implicit none

type(state_type) :: err

call remove_directory("directory_to_be_removed", err)

if (err%error()) then
print *, err%print()
else
print *, "directory removed successfully"
end if

end program example_remove_directory
5 changes: 3 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ set(fppFiles
stdlib_linalg_kronecker.fypp
stdlib_linalg_cross_product.fypp
stdlib_linalg_eigenvalues.fypp
stdlib_linalg_solve.fypp
stdlib_linalg_solve.fypp
stdlib_linalg_determinant.fypp
stdlib_linalg_qr.fypp
stdlib_linalg_inverse.fypp
stdlib_linalg_pinv.fypp
stdlib_linalg_norms.fypp
stdlib_linalg_state.fypp
stdlib_linalg_svd.fypp
stdlib_linalg_svd.fypp
stdlib_linalg_cholesky.fypp
stdlib_linalg_schur.fypp
stdlib_optval.fypp
Expand Down Expand Up @@ -116,6 +116,7 @@ set(SRC
stdlib_sorting_radix_sort.f90
stdlib_system_subprocess.c
stdlib_system_subprocess.F90
stdlib_system.c
stdlib_system.F90
stdlib_sparse.f90
stdlib_specialfunctions_legendre.f90
Expand Down
Loading
Loading