system_unsetenv Subroutine

public subroutine system_unsetenv(name, ierr)

NAME

system_unsetenv(3f) - [M_system:ENVIRONMENT] delete an environment
                      variable by calling unsetenv(3c)
(LICENSE:PD)

SYNOPSIS

subroutine system_unsetenv(name,ierr)

character(len=*),intent(in)  :: name
integer,intent(out),optional :: ierr

DESCRIPTION

The system_unsetenv(3f) function deletes the variable name from the
environment.

OPTIONS

name   name of variable to delete.
       If name does not exist in the environment, then the
       function succeeds, and the environment is unchanged.

ierr   The system_unsetenv(3f) function returns zero on success,
       or -1 on error. name is NULL, points to a string of length 0,
       or contains an '=' character. Insufficient memory to add a
       new variable to the environment.

EXAMPLE

Sample program:

  program demo_system_unsetenv
  use M_system, only : system_unsetenv, system_putenv
  implicit none
  call system_putenv('GRU=this is the value')
  write(*,'(a)')'The variable GRU should be set'
  call execute_command_line('env|grep GRU')
  call system_unsetenv('GRU')
  write(*,'(a)')'The variable GRU should not be set'
  call execute_command_line('env|grep GRU')
  end program demo_system_unsetenv

AUTHOR

John S. Urban

LICENSE

Public Domain

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: name
integer, intent(out), optional :: ierr

Contents

Source Code


Source Code

subroutine system_unsetenv(name,ierr)

! ident_25="@(#) M_system system_unsetenv(3f) call unsetenv(3c) to remove variable from environment"

character(len=*),intent(in)  :: name
integer,intent(out),optional :: ierr
integer                      :: ierr_local
character(kind=c_char,len=1),allocatable :: temp(:)

! int unsetenv(void)
interface
   integer(kind=c_int) function c_unsetenv(c_name) bind(C,NAME="unsetenv")
   import c_int, c_char
   character(len=1,kind=c_char) :: c_name(*)
   end function
end interface

   temp = str2_carr(trim(name)) ! kludge for bug in ifort (IFORT) 2021.3.0 20210609
   ierr_local =  c_unsetenv(temp)

   if(present(ierr))then
      ierr=ierr_local
   elseif(ierr_local.ne.0)then ! if error occurs and not being returned, stop
      write(*,*)'*system_unsetenv* error=',ierr_local
      stop
   endif

end subroutine system_unsetenv