|
1 | 1 | module stdlib_system
|
2 |
| -use, intrinsic :: iso_c_binding, only : c_int, c_long, c_null_ptr, c_int64_t |
| 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 |
3 | 4 | use stdlib_kinds, only: int64, dp, c_bool, c_char
|
4 | 5 | use stdlib_strings, only: to_c_char
|
5 | 6 | implicit none
|
@@ -97,6 +98,23 @@ module stdlib_system
|
97 | 98 | !! Windows, and various UNIX-like environments. On unsupported operating systems, the function will return `.false.`.
|
98 | 99 | !!
|
99 | 100 | public :: is_directory
|
| 101 | + |
| 102 | +!! version: experimental |
| 103 | +!! |
| 104 | +!! Returns the file path of the null device, which discards all data written to it. |
| 105 | +!! ([Specification](../page/specs/stdlib_system.html#null_device-return-the-null-device-file-path)) |
| 106 | +!! |
| 107 | +!! ### Summary |
| 108 | +!! Function that provides the file path of the null device appropriate for the current operating system. |
| 109 | +!! |
| 110 | +!! ### Description |
| 111 | +!! |
| 112 | +!! The null device is a special file that discards all data written to it and always reads as |
| 113 | +!! an empty file. This function returns the null device path, adapted for the operating system in use. |
| 114 | +!! |
| 115 | +!! On Windows, this is `NUL`. On UNIX-like systems, this is `/dev/null`. |
| 116 | +!! |
| 117 | +public :: null_device |
100 | 118 |
|
101 | 119 | ! CPU clock ticks storage
|
102 | 120 | integer, parameter, private :: TICKS = int64
|
@@ -654,4 +672,39 @@ end function stdlib_is_directory
|
654 | 672 |
|
655 | 673 | end function is_directory
|
656 | 674 |
|
| 675 | +!> Returns the file path of the null device for the current operating system. |
| 676 | +!> |
| 677 | +!> Version: Helper function. |
| 678 | +function null_device() result(path) |
| 679 | + !> File path of the null device |
| 680 | + character(:), allocatable :: path |
| 681 | + |
| 682 | + interface |
| 683 | + |
| 684 | + ! No-overhead return path to the null device |
| 685 | + type(c_ptr) function process_null_device(len) bind(C,name='process_null_device') |
| 686 | + import c_ptr, c_size_t |
| 687 | + implicit none |
| 688 | + integer(c_size_t), intent(out) :: len |
| 689 | + end function process_null_device |
| 690 | + |
| 691 | + end interface |
| 692 | + |
| 693 | + integer(c_size_t) :: i, len |
| 694 | + type(c_ptr) :: c_path_ptr |
| 695 | + character(kind=c_char), pointer :: c_path(:) |
| 696 | + |
| 697 | + ! Call the C function to get the null device path and its length |
| 698 | + c_path_ptr = process_null_device(len) |
| 699 | + call c_f_pointer(c_path_ptr,c_path,[len]) |
| 700 | + |
| 701 | + ! Allocate the Fortran string with the length returned from C |
| 702 | + allocate(character(len=len) :: path) |
| 703 | + |
| 704 | + do concurrent (i=1:len) |
| 705 | + path(i:i) = c_path(i) |
| 706 | + end do |
| 707 | + |
| 708 | +end function null_device |
| 709 | + |
657 | 710 | end module stdlib_system
|
0 commit comments