Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ longjmp(3c) — 4D1 2.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

signal(2)



     SETJMP(3C)                                             SETJMP(3C)



     NAME
          setjmp, longjmp - non-local goto

     SYNOPSIS
          #include <setjmp.h>

          int setjmp (env)
          jmpbuf env;

          void longjmp (env, val)
          jmpbuf 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 will
          return 1.  At the time of the second return from setjmp, all
          accessible data have values as of the time longjmp is
          called.  However, global variables will have the expected
          values, i.e. those as of the time of the longjmp (see
          example).

     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*/



     Page 1                                        (last mod. 8/20/87)





     SETJMP(3C)                                             SETJMP(3C)



          }

          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

     SEE ALSO
          signal(2).

     WARNING
          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.

     BUGS
          The values of the registers on the second return from setjmp
          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.

     ORIGIN
          AT&T V.3























     Page 2                                        (last mod. 8/20/87)



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