#include <lcjmp.h> int blkjmp(jmp_buf env);
blkjmp
requests interception of calls to longjmp
that could
terminate the calling function. When you call blkjmp
, it always
returns 0. If a call to longjmp
is later intercepted, the call to
blkjmp
is resumed and it then returns the integer argument that was
passed to longjmp
. The env
variable is modified to indicate
the target of the intercepted longjmp
so that it can be reissued by
the intercepting routine.
After a call to longjmp
is intercepted, blkjmp
must be reissued
if continued interception is wanted.
Because exit
is implemented as a longjmp
to the caller of
main
, you can use blkjmp
to intercept program exit.
blkjmp
normally returns 0; it returns a non-zero value if a call to
longjmp
has been intercepted (in which case, blkjmp
returns the
value of the second argument passed to longjmp
).
auto
and register
whose values are
changed between the blkjmp
and longjmp
calls have
indeterminate values on return to blkjmp
.
blkjmp
can be used to enable a function
to release resources, even if terminated by a call to longjmp
in a
function it calls:
#include <stdio.h> #include <lcjmp.h> #include <stdlib.h> jmp_buf env; static void get_resource(void), use_resource(void); int main() { int code; if (code = setjmp(env)) goto escape; get_resource(); puts("get_resource returned normally."); exit(0); escape: printf("Executing escape routine for error %dn", code); exit(code); } static void get_resource(void) { int code; jmp_buf my_env; /* Allocate resource here. */ if (code = blkjmp(my_env)) goto release; puts("Resources allocated."); /* Free resource here. */ use_resource(); puts("use_resource returned normally, " "get_resource is freeing resources."); return; /* Free resource here. */ release: printf("use_resource indicated error %dn", code); puts("Resources now freed, proceeding with longjmp."); longjmp(my_env, code); } static void use_resource(void) { puts("Entering use_resource"); /* Attempt to use resource here. */ puts("Error 3 detected, calling longjmp."); longjmp(env, 3); puts("This statement will not be executed."); }
longjmp
, setjmp
, siglongjmp
, sigsetjmp
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.