system_clearenv(3f) - [M_system:ENVIRONMENT] clear environment by
calling clearenv(3c)
(LICENSE:PD)
subroutine system_clearenv(ierr)
integer,intent(out),optional :: ierr
The clearenv() procedure clears the environment of all name-value
pairs. Typically used in security-conscious applications or ones where
configuration control requires ensuring specific variables are set.
ierr returns zero on success, and a nonzero value on failure. Optional.
If not present and an error occurs the program stops.
Sample program:
program demo_system_clearenv
use M_system, only : system_clearenv
implicit none
! environment before clearing
call execute_command_line('env|wc')
! environment after clearing (not necessarily blank!)
call system_clearenv()
call execute_command_line('env')
end program demo_system_clearenv
Typical output:
89 153 7427
PWD=/home/urbanjs/V600
SHLVL=1
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(out), | optional | :: | ierr |
subroutine system_clearenv(ierr)
! emulating because not available on some platforms
! ident_24="@(#) M_system system_clearenv(3f) emulate clearenv(3c) to clear environment"
integer,intent(out),optional :: ierr
character(len=:),allocatable :: string
integer :: ierr_local1, ierr_local2
ierr_local2=0
INFINITE: do
call system_initenv() ! important -- changing table causes undefined behavior so reset after each unsetenv
string=system_readenv() ! get first name=value pair
if(string.eq.'') exit INFINITE
call system_unsetenv(string(1:index(string,'=')-1) ,ierr_local1) ! remove first name=value pair
if(ierr_local1.ne.0)ierr_local2=ierr_local1
enddo INFINITE
if(present(ierr))then
ierr=ierr_local2
elseif(ierr_local2.ne.0)then ! if error occurs and not being returned, stop
write(*,*)'*system_clearenv* error=',ierr_local2
stop
endif
end subroutine system_clearenv