DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

setjmp(S)


setjmp, longjmp -- non-local goto

Syntax

cc . . . -lc

#include  <setjmp.h>

int setjmp (jmp_buf env);

void longjmp (jmp_buf env, int val);

Description

These functions are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program.

setjmp( ) saves its stack environment in env (whose type, jmp_buf, 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. After longjmp( ) is completed, program execution continues as if the corresponding call of setjmp( ) had just returned the value val. longjmp( ) cannot cause setjmp( ) to return the value 0. If longjmp( ) is invoked with a second argument of 0, setjmp( ) returns 1. At the time of the second return from setjmp( ), all external and static variables have values as of the time longjmp( ) is called (see example). The values of register and automatic variables are undefined.

SCO OpenServer does not assign any special meaning to the symbols _setjmp( ) and _longjmp.( ) Some other operating systems use these to differentiate between functions that save the process's signal mask (setjmp/longjmp) and functions that do not (_setjmp/_longjmp).

If the signal mask is to be saved as part of the environment, use the sigsetjmp(S) and siglongjmp(S) routines instead.

Warning

Problems will occur if longjmp( ) is called before env is primed with a call to setjmp( ).

Example

   #include  <setjmp.h>
   

jmp_buf 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 is:

See also

signal(M), sigaction(S), sigsetjmp(S)

Standards conformance

longjmp( ) and setjmp( ) conform with:

X/Open Portability Guide, Issue 3, 1989 ;
ANSI X3.159-1989 Programming Language -- C ;
IEEE POSIX Std 1003.1-1990 System Application Program Interface (API) [C Language] (ISO/IEC 9945-1) ;
and NIST FIPS 151-1 .


© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003