Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

signal(2)

SETJMP(3C)  —  Kubota Pacfic Computer Inc. (C Programming Language Utilities)

NAME

setjmp, longjmp − non-local goto

SYNOPSIS

#include <setjmp.h>

int setjmp (env)
jmp_buf env;

void longjmp (env, val)
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 (which must not itself have returned in the interim) 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 variables (local and global) contain the last values they had before the call of longjmp (see example). 

This behavior is enforced by a pragma hidden in the definition of setjmp in the file setjmp.h Accordingly, setjmp is really a macro, and (for example) its address cannot be passed to another routine, and it cannot safely be invoked from an assembly language routine. 

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:

value of i on 1st return from setjmp:0 value of i on 2nd return from setjmp:1

SEE ALSO

signal(2)

WARNING

Absolute chaos is guaranteed in either of the following cases:

1.  If longjmp is called even though env was never primed by a call to setjmp.

2.  If the last such call was in a function which has since returned. 

 

March 13, 1992

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026