set_env Function

public function set_env(name, value, overwrite)

Set an environment variable for the current environment using the C standard library

Overwrite setting C strings Call setenv

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: name

Variable name

character(len=*), intent(in) :: value

Variable value

logical, intent(in), optional :: overwrite

Should a former value be overwritten? default = .true.

Return Value logical


Source Code

logical function set_env(name,value,overwrite)

   !> Variable name
   character(*), intent(in) :: name
   
   !> Variable value
   character(*), intent(in) :: value
   
   !> Should a former value be overwritten? default = .true.
   logical, optional, intent(in) :: overwrite
   
   ! Local variables
   logical :: can_overwrite
   integer(c_int) :: cover,cerr
   character(kind=c_char,len=1), allocatable :: c_value(:),c_name(:)
   
   interface
      integer(c_int) function c_setenv(envname, envval, overwrite) &
                     bind(C,name="c_setenv")
         import c_int, c_char
         implicit none
         !> Pointer to the name string
         character(kind=c_char,len=1), intent(in) :: envname(*)
         !> Pointer to the value string 
         character(kind=c_char,len=1), intent(in) :: envval(*)
         !> Overwrite option
         integer(c_int), intent(in), value :: overwrite
      end function c_setenv 
   end interface
   
   !> Overwrite setting
   can_overwrite = .true.
   if (present(overwrite)) can_overwrite = overwrite
   cover = merge(1_c_int,0_c_int,can_overwrite)
   
   !> C strings
   call f2cs(name,c_name)
   call f2cs(value,c_value)
   
   !> Call setenv
#ifndef FPM_BOOTSTRAP   
   cerr = c_setenv(c_name,c_value,cover)
#endif
   set_env = cerr==0_c_int
   
end function set_env