Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop fortran program with non-zero exit status

Tags:

I'm adapting some Fortran code I haven't written, and without a lot of fortran experience myself. I just found a situation where some malformed input got silently ignored, and would like to change that code to do something more appropriate. If this were C, then I'd do something like

fprintf(stderr, "There was an error of kind foo");
exit(EXIT_FAILURE);

But in fortran, the best I know how to do looks like

write(*,*) 'There was an error of kind foo'
stop

which lacks the choice of output stream (minor issue) and exit status (major problem).

How can I terminate a fortran program with a non-zero exit status?

In case this is compiler-dependent, a solution which works with gfortran would be nice.

like image 895
MvG Avatar asked Jun 30 '13 15:06

MvG


People also ask

How do you stop a Fortran program?

8.101 EXIT — Exit the program with status. EXIT causes immediate termination of the program with status. If status is omitted it returns the canonical success for the system. All Fortran I/O units are closed.

What is nonzero exit code?

A non-zero exit status indicates failure. This seemingly counter-intuitive scheme is used so there is one well-defined way to indicate success and a variety of ways to indicate various failure modes. When a command terminates on a fatal signal whose number is N , Bash uses the value 128+ N as the exit status.

What is difference between stop and end statement in Fortran?

STOP tells the computer to stop running the program. In principle STOP can appear anywhere in the program and there can be any number of them. END tells the compiler (i.e. the program that takes your FORTRAN source and converts it into machine code) that this is the end of a program unit for compilation.

How do you exit a subroutine in Fortran?

Here is an example: the subroutine "division" takes an integer input value and iteratively divides it by a value that is the input value decremented by the number of steps-1. When such value reaches zero, a flag should be raised and the subroutine should be exited without performing the division by zero.


1 Answers

The stop statement allows a integer or character value. It seems likely that these will be output to stderr when that exists, but as stderr is OS dependent, it is unlikely that the Fortran language standard requires that, if it says anything at all. It is also likely that if you use the numeric option that the exit status will be set. I tried it with gfortran on a Mac, and that was the case:

program TestStop

integer :: value

write (*, '( "Input integer: " )', advance="no")
read (*, *) value

if ( value > 0 ) then
   stop 0
else
   stop 9
end if

end program TestStop

While precisely what stop with an integer or string will do is OS-dependent, the statement is part of the language and will always compile. call exit is a GNU extension and might not link on some OSes.

like image 170
M. S. B. Avatar answered Oct 14 '22 16:10

M. S. B.