|
|
1.1 ! root 1: ! 2: ! 3: setjmp() General Function setjmp() ! 4: ! 5: ! 6: ! 7: ! 8: Perform non-local goto ! 9: ! 10: #include <setjmp.h> ! 11: iinntt sseettjjmmpp(_e_n_v) jjmmpp_bbuuff _e_n_v; ! 12: ! 13: The function call is the only mechanism that C provides to trans- ! 14: fer control between functions. This mechanism, however, is in- ! 15: adequate for some purposes, such as handling unexpected errors or ! 16: interrupts at lower levels of a program. To answer this need, ! 17: sseettjjmmpp helps to provide a non-local _g_o_t_o facility. sseettjjmmpp saves ! 18: a stack context in _e_n_v, and returns value zero. The stack con- ! 19: text can be restored with the function longjmp. The type decla- ! 20: ration for jjmmpp_bbuuff is in the header file sseettjjmmpp.hh. The context ! 21: saved includes the program counter, stack pointer, and stack ! 22: frame. ! 23: ! 24: ***** Example ***** ! 25: ! 26: The following gives a simple example of setjmp and longjmp. ! 27: ! 28: ! 29: #include <setjmp.h> ! 30: ! 31: ! 32: ! 33: jmp_buf env; /* place for setjmp to store its environment */ ! 34: ! 35: ! 36: ! 37: main() ! 38: { ! 39: int rc; ! 40: ! 41: ! 42: ! 43: if(rc = setjmp(env)) { /* we come here on return */ ! 44: printf("First char was %c\n", rc); ! 45: exit(0); ! 46: } ! 47: subfun(); /* this never returns */ ! 48: } ! 49: ! 50: ! 51: ! 52: subfun() ! 53: { ! 54: char buf[80]; ! 55: ! 56: ! 57: ! 58: ! 59: ! 60: ! 61: ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66: ! 67: ! 68: ! 69: setjmp() General Function setjmp() ! 70: ! 71: ! 72: ! 73: do { ! 74: printf("Enter some data\n"); ! 75: gets(buf); /* get data */ ! 76: } while(!buf[0]); /* retry on null line */ ! 77: ! 78: ! 79: ! 80: longjmp(env, buf[0]); /* buf[0] must be non zero */ ! 81: } ! 82: ! 83: ! 84: ***** See Also ***** ! 85: ! 86: general functions, getenv(), longjmp() ! 87: ! 88: ***** Notes ***** ! 89: ! 90: Programmers should note that many user-level routines cannot be ! 91: interrupted and reentered safely. For that reason, improper use ! 92: of setjmp and longjmp can create mysterious and irreproducible ! 93: bugs. The use of longjmp to exit interrupt exception or signal ! 94: handlers is particularly hazardous. ! 95: ! 96: ! 97: ! 98: ! 99: ! 100: ! 101: ! 102: ! 103: ! 104: ! 105: ! 106: ! 107: ! 108: ! 109: ! 110: ! 111: ! 112: ! 113: ! 114: ! 115: ! 116: ! 117: ! 118: ! 119: ! 120: ! 121: ! 122: ! 123: ! 124: ! 125: ! 126: ! 127: ! 128: ! 129: ! 130: COHERENT Lexicon Page 2 ! 131: ! 132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.