SETJMP(3C) SETJMP(3C) NAME setjmp, longjmp, sigsetjmp, siglongjmp, _setjmp, _longjmp - non-local gotos SYNOPSIS #include <setjmp.h> SysV: int setjmp (jmp_buf env); void longjmp (jmp_buf env, int val); POSIX: int sigsetjmp (sigjmp_buf env, int savemask); void siglongjmp (sigjmp_buf env, int val); BSD: int setjmp (jmp_buf env); void longjmp (jmp_buf env, int val); int _setjmp (jmp_buf env); void _longjmp (jmp_buf env, int val); To use the BSD versions of setjmp and longjmp, you must either 1) #define _BSD_SIGNALS or _BSD_COMPAT before including <setjmp.h>, or 2) specify one of them in the compile command or makefile: cc -D_BSD_SIGNALS -o prog prog.c DESCRIPTION These functions are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program. All varieties of setjmp save their stack environment in env (whose type, jmp_buf, is defined in the <setjmp.h> header file) for later use by all varieties of longjmp. If the return is from a direct invocation, all setjmps return the value 0. If the return is from a call to any of the longjmps, all setjmp routines return a nonzero value. All longjmps restore the environment saved by the last call of setjmp with the corresponding env argument. After the longjmp is completed, program execution continues as if the corresponding call of setjmp (which must not itself have returned in the interim) had just returned the value val. longjmps cannot cause setjmps to return the value 0. If a longjmp is invoked with a second argument of 0, all versions of setjmp will return 1. At the time of the second return from a setjmp, external and static variables have values as of the time longjmp is called (see example). The values of register and automatic variables are undefined. Register or automatic variables whose value must be relied upon must be declared as volatile. SYSV-POSIX-BSD DIFFERENCES The System V setjmp/longjmp perform identically to the 4.3BSD _setjmp/_longjmp, i.e., they manipulate only the C stack and registers. The 4.3BSD setjmp/longjmp also manipulate the C stack and registers, but additionally save and restore the process's signal mask (see sigprocmask(2), sigblock(3B), or sigsetmask(3B)). The POSIX sigsetjmp/siglongjmp calls may act in either manner: the C stack and registers are always saved and restored, but if the savemask parameter to sigsetjmp is non-zero, the signal mask is saved, and a bit in env is set to indicate that it was saved. siglongjmp checks that bit to determine if it should restore the mask or not. NOTE Prior to IRIX 6.5.15, the man page incorrectly stated that the return type for the BSD longjmp(3C) and _longjmp(3C) was int; the correct return type is void. EXAMPLE #include <setjmp.h> jmp_buf env; int i = 0; main () { if (setjmp(env) != 0) { (void) printf("2nd return from setjmp: i = %d\n", i); exit(0); } (void) printf("1st return from setjmp: i = %d\n", i); i = 1; g(); /*NOTREACHED*/ } g() { longjmp(env, 1); /*NOTREACHED*/ } The program's output is: 1st return from setjmp: i = 0 2nd return from setjmp: i = 1 SEE ALSO sigaction(2), sigprocmask(2), signal(2), sigblock(3B), sigsetmask(3B), sigvec(3B), signal(3B). WARNINGS If longjmp is called even though env was never primed by a call to setjmp, or when the last such call was in a function which has since returned, absolute chaos is guaranteed. In multithreaded processes, if longjmp is called with an env initialized in different thread, the result is also guaranteed to produce chaos. Also note that the signal mask manipulated by these interfaces is per thread. If different versions of these jmp routines are mixed, unpredictable signal masking may occur. BUGS The values of the registers on the second return from the setjmps are the register values at the time of the first call to setjmp, not those at the time of the longjmp. This means that variables in a given function may behave differently in the presence of setjmp, depending on whether they are register or stack variables. Page 3