http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59484

--- Comment #2 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Thu, Dec 12, 2013 at 02:27:19PM +0000, kargl at gcc dot gnu.org wrote:
> --- Comment #1 from kargl at gcc dot gnu.org ---
> (In reply to Reinhold Straub from comment #0)
> > Please compile:
> > 
> > program test
> >   implicit none
> > 
> >   character(len=11) :: cmdmsg,str
> >   integer :: exitstat,cmdstat,status,length
> >   logical :: wait
> >   wait   = .true.
> >   cmdmsg = "ok"
> > 
> >   call execute_command_line("export newvar='some text'")
> >   print *,"result 1:", exitstat, cmdstat, cmdmsg
> >   call get_environment_variable("newvar",str,length,status)
> >   print *,"1:     ",str,length,status
> >   print *,"expected:","some text",9,0; print *,""
> > 
> 
> Looks like an invalid program to me.  exitstat, cmdstat, and cmdmsg
> are undefined in the 1st print statement.

Actually, cmdmsg is defined.  exitstat and cmdstat are not defined.

> execute_command_line(command) executes the 'command' in a child
> process.  You've managed to export newvar in the child's environment.
> By the time you use get_environment_variable, the child process has
> completed and more importantly it has not changed its parent 
> environment.
> 
> If you want to change the parent's environment, you'll most likely
> need to use ISO C interop and the setenv function from the C lib.

If you want to change the environment of the parent you can do

program foo

   use iso_c_binding

   implicit none

   integer status
   character(len=20) var

   interface
      function putenv(str) bind(c,name="putenv")
         use iso_c_binding
         integer(c_int) putenv
         character(len=1,kind=c_char) str
      end function putenv
   end interface

   status = putenv('OHMY=Farmer tan' // C_NULL_CHAR)
   print '(A,I0)', 'status = ', status

   call get_environment_variable('OHMY', var)
   print '(A)', trim(var)

end program foo

gfortran -o z foo.f90
./z
status = 0
Farmer tan

Reply via email to