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 11 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 @@ -535,6 +535,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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It gets the current working directory associated with the process calling this subroutine.
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)`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`call [[stdlib_system(module):get_cwd(subroutine)]] (cwd, err)`
`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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


### Return values

The `err` is set accordingly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The `err` is set accordingly.
`err` is an optional state return flag. On error if not requested, a `FS_ERROR` will trigger an error stop.


### 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
6 changes: 4 additions & 2 deletions example/system/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ 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)
ADD_EXAMPLE(fs_error)
ADD_EXAMPLE(path_join)
ADD_EXAMPLE(path_split_path)
ADD_EXAMPLE(path_base_name)
ADD_EXAMPLE(path_dir_name)

ADD_EXAMPLE(path_dir_name)
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
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ set(SRC
stdlib_sorting_radix_sort.f90
stdlib_system_subprocess.c
stdlib_system_subprocess.F90
stdlib_system.c
stdlib_system_path.f90
stdlib_system.F90
stdlib_sparse.f90
Expand Down
Loading
Loading