[Bug libfortran/62296] EXECUTE_COMMAND_LINE not F2008 conforming

anlauf at gmx dot de gcc-bugzilla@gcc.gnu.org
Sun Aug 31 21:08:00 GMT 2014


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62296

--- Comment #1 from Harald Anlauf <anlauf at gmx dot de> ---
There was some confusion on my side regarding the semantics of
the CMDSTAT argument on my side.  I asked at

https://groups.google.com/forum/?hl=en#!topic/comp.lang.fortran/6ymZIb6flDg

where there was consensus that CMDSTAT should be zero if the command line
could be executed, even if the exit status is non-zero.

However, if the command could *not* be executed, there should be a
non-zero value for CMDSTAT.  However, modifying the code to

  integer :: stat, cstat
  character(len=255) :: cmdmsg
  cmdmsg = ""
  call execute_command_line ("/bin/true",  exitstat=stat, cmdstat=cstat,
cmdmsg=cmdmsg)
  print *, stat, cstat, "'", trim (cmdmsg), "'"
  call execute_command_line ("/bin/false", exitstat=stat, cmdstat=cstat,
cmdmsg=cmdmsg)
  print *, stat, cstat, "'", trim (cmdmsg), "'"
  call execute_command_line ("/nosuchfile",exitstat=stat, cmdstat=cstat,
cmdmsg=cmdmsg)
  print *, stat, cstat, "'", trim (cmdmsg), "'"
  call execute_command_line ("/bin/true",  exitstat=stat)
  print *, stat
  call execute_command_line ("/bin/false", exitstat=stat)
  print *, stat
  call execute_command_line ("/nosuchfile",exitstat=stat)
  print *, stat
end

produces:

           0           0 ''
           1           0 ''
sh: /nosuchfile: No such file or directory
         127           0 ''
           0
           1
sh: /nosuchfile: No such file or directory
         127

Thus the attempt to execute a non-existing command does not lead
to an appropriate error.  Looking at execute_command_line.c, the
execution uses system().  The Linux man page system(3) says:

       It is possible for the shell command to return 127, so that code is not
       a sure indication that the execve(2) call failed.

Unfortunately the man page does not say whether errno is set,
as it is done for execve(2).  So it might be necessary to replace
system(3) by something like fork/exec to get the error status.



More information about the Gcc-bugs mailing list