set_environment_variable Subroutine

public subroutine set_environment_variable(NAME, VALUE, STATUS)

NAME

set_environment_variable(3f) - [M_system:ENVIRONMENT] call setenv(3c)
                               to set environment variable
(LICENSE:PD)

SYNOPSIS

subroutine set_environment_variable(NAME, VALUE, STATUS)

character(len=*)               :: NAME
character(len=*)               :: VALUE
integer, optional, intent(out) :: STATUS

DESCRIPTION

The set_environment_variable() procedure adds or changes the value
of environment variables.

OPTIONS

NAME    If name does not already exist in the environment, then string
        is added to the environment. If name does exist, then the
        value of name in the environment is changed to value.
VALUE   Value to assign to environment variable NAME
STATUS  returns zero on success, or nonzero if an error occurs.
        A non-zero error usually indicates sufficient memory does
        not exist to store the variable.

EXAMPLE

Sample setting an environment variable from Fortran:

program demo_set_environment_variable
use M_system, only : set_environment_variable
use iso_c_binding
implicit none
integer :: ierr
   !x!
   write(*,'(a)')'no environment variables containing "GRU":'
   call execute_command_line('env|grep GRU')
   !x!
   call set_environment_variable('GRU','this is the value',ierr)
   write(*,'(a,i0)')'now "GRU" should be defined, status=',ierr
   call execute_command_line('env|grep GRU')
   !x!
   call set_environment_variable('GRU2','this is the second value',ierr)
   write(*,'(a,i0)')'now "GRU" and "GRU2" should be defined, status =',ierr
   !x!
   call execute_command_line('env|grep GRU')
end program demo_set_environment_variable

Results:

no environment variables containing "GRU":
now "GRU" should be defined, status=0
GRU=this is the value
now "GRU" and "GRU2" should be defined, status =0
GRU2=this is the second value
GRU=this is the value

AUTHOR

John S. Urban

LICENSE

Public Domain

Arguments

Type IntentOptional Attributes Name
character(len=*) :: NAME
character(len=*) :: VALUE
integer, intent(out), optional :: STATUS

Contents


Source Code

subroutine set_environment_variable(NAME, VALUE, STATUS)

! ident_24="@(#)M_system::set_environment_variable(3f): call setenv(3c) to set environment variable"

character(len=*)               :: NAME
character(len=*)               :: VALUE
integer, optional, intent(out) :: STATUS
integer                        :: loc_err
character(kind=c_char,len=1),allocatable :: temp1(:)
character(kind=c_char,len=1),allocatable :: temp2(:)
integer,parameter              :: flag=1

interface
   integer(kind=c_int) function c_setenv(c_name,c_VALUE,flag) bind(C,NAME="setenv")
      !USE iso_c_binding
      import c_int, c_char
      character(kind=c_char),intent(in)    :: c_name(*)
      character(kind=c_char),intent(in)    :: c_VALUE(*)
      integer(kind=c_int),intent(in),value :: flag
   end function
end interface

   temp1 = str2_carr(trim(NAME)) ! kludge for bug in ifort (IFORT) 2021.3.0 20210609
   temp2 = str2_carr(trim(VALUE)) ! kludge for bug in ifort (IFORT) 2021.3.0 20210609
   loc_err =  c_setenv(temp1,temp2,flag)
   if (present(STATUS)) STATUS = loc_err
end subroutine set_environment_variable