|
1 | 1 | module stdlib_system
|
2 |
| -use, intrinsic :: iso_c_binding, only : c_int, c_long, c_null_ptr, c_int64_t |
3 |
| -use stdlib_kinds, only: int64, dp |
| 2 | +use, intrinsic :: iso_c_binding, only : c_int, c_long, c_ptr, c_null_ptr, c_int64_t, c_size_t, & |
| 3 | + c_f_pointer |
| 4 | +use stdlib_kinds, only: int64, dp, c_char |
4 | 5 | implicit none
|
5 | 6 | private
|
6 | 7 | public :: sleep
|
@@ -81,6 +82,23 @@ module stdlib_system
|
81 | 82 | public :: elapsed
|
82 | 83 | public :: is_windows
|
83 | 84 |
|
| 85 | +!! version: experimental |
| 86 | +!! |
| 87 | +!! Returns the file path of the null device, which discards all data written to it. |
| 88 | +!! ([Specification](../page/specs/stdlib_system.html#null_device-return-the-null-device-file-path)) |
| 89 | +!! |
| 90 | +!! ### Summary |
| 91 | +!! Function that provides the file path of the null device appropriate for the current operating system. |
| 92 | +!! |
| 93 | +!! ### Description |
| 94 | +!! |
| 95 | +!! The null device is a special file that discards all data written to it and always reads as |
| 96 | +!! an empty file. This function returns the null device path, adapted for the operating system in use. |
| 97 | +!! |
| 98 | +!! On Windows, this is `NUL`. On UNIX-like systems, this is `/dev/null`. |
| 99 | +!! |
| 100 | +public :: null_device |
| 101 | + |
84 | 102 | ! CPU clock ticks storage
|
85 | 103 | integer, parameter, private :: TICKS = int64
|
86 | 104 | integer, parameter, private :: RTICKS = dp
|
@@ -618,4 +636,39 @@ pure function OS_NAME(os)
|
618 | 636 | end select
|
619 | 637 | end function OS_NAME
|
620 | 638 |
|
| 639 | +!> Returns the file path of the null device for the current operating system. |
| 640 | +!> |
| 641 | +!> Version: Helper function. |
| 642 | +function null_device() result(path) |
| 643 | + !> File path of the null device |
| 644 | + character(:), allocatable :: path |
| 645 | + |
| 646 | + interface |
| 647 | + |
| 648 | + ! No-overhead return path to the null device |
| 649 | + type(c_ptr) function process_null_device(len) bind(C,name='process_null_device') |
| 650 | + import c_ptr, c_size_t |
| 651 | + implicit none |
| 652 | + integer(c_size_t), intent(out) :: len |
| 653 | + end function process_null_device |
| 654 | + |
| 655 | + end interface |
| 656 | + |
| 657 | + integer(c_size_t) :: i, len |
| 658 | + type(c_ptr) :: c_path_ptr |
| 659 | + character(kind=c_char), pointer :: c_path(:) |
| 660 | + |
| 661 | + ! Call the C function to get the null device path and its length |
| 662 | + c_path_ptr = process_null_device(len) |
| 663 | + call c_f_pointer(c_path_ptr,c_path,[len]) |
| 664 | + |
| 665 | + ! Allocate the Fortran string with the length returned from C |
| 666 | + allocate(character(len=len) :: path) |
| 667 | + |
| 668 | + do concurrent (i=1:len) |
| 669 | + path(i:i) = c_path(i) |
| 670 | + end do |
| 671 | + |
| 672 | +end function null_device |
| 673 | + |
621 | 674 | end module stdlib_system
|
0 commit comments