setjmp(3C) setjmp(3C)
NAME
setjmp, longjmp - non-local goto
SYNOPSIS
#include <setjmp.h>
int setjmp(jmpbuf env);
void longjmp(jmpbuf env, int val);
DESCRIPTION
These functions enable errors and interrupts, encountered in a low-
level subroutine of a program, to be handled.
setjmp() saves its stack environment in env (whose type, jmpbuf, is
defined in the setjmp.h header file) for later use by longjmp(). It
returns the value 0.
longjmp() restores the environment saved by the last call of setjmp()
with the corresponding env argument. If there is no such invocation,
or if the function containing the invocation of setjmp() has ter-
minated execution in the interim, the behavior is undefined. It is
unspecified whether longjmp() restores the signal mask, leaves the
signal mask unchanged or restores it to its value at the time setjmp()
was called.
All accessible objects have values as of the time longjmp() was
called, except that the values of objects of automatic storage dura-
tion are indeterminate if they meet all the following conditions:
- They are local to the function containing the corresponding
setjmp() invocation.
- They do not have volatile-qualified type.
- They are changed between the setjmp() invocation and longjmp()
call.
As it bypasses the usual function call and return mechanisms,
longjmp() will execute correctly in contexts of interrupts, signals
and any of their associated functions. However, if longjmp() is
invoked from a nested signal handler (that is, from a function invoked
as a result of a signal raised during the handling of another signal),
the behavior is undefined.
Page 1 Reliant UNIX 5.44 Printed 11/98
setjmp(3C) setjmp(3C)
EXAMPLE
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
jmpbuf env;
int i = 0;
main ()
{
void exit();
if(setjmp(env) != 0) {
(void) printf("value of i on 2nd return from setjmp:
%d\n", i);
exit(0);
}
(void) printf("value of i on 1st return from setjmp: %d\n", i);
i = 1;
g(); /*NOTREACHED*/
}
g()
{
longjmp(env, 1); /*NOTREACHED*/
}
If the a.out resulting from this C language code is run, the output
will be:
value of i on 1st return from setjmp():0
value of i on 2nd return from setjmp():1
NOTES
Applications whose behavior depends on the value of the signal mask
should not use longjmp() and setjmp(), since their effect on the sig-
nal mask is unspecified, but should instead use the following alterna-
tives:
- The longjmp() and setjmp() functions (which never modify the sig-
nal mask)
- The siglongjmp() and sigsetjmp() functions (which can save and
restore the signal mask under application control).
It is advisable to use the sigsetjmp(3C) function to examine errors
and interrupts in a subroutine of a program.
SEE ALSO
signal(2), setjmp(3C-ucb), sigsetjmp(3C), setjmp(5).
Page 2 Reliant UNIX 5.44 Printed 11/98