1 #include "lib.h" 2 #include "sys9.h" 3 #include <stdlib.h> 4 #include <string.h> 5 #include <unistd.h> 6 #include <signal.h> 7 8 int _finishing = 0; 9 int _sessleader = 0; 10 11 static char exitstatus[ERRLEN]; 12 13 void 14 _exit(int status) 15 { 16 _finish(status, 0); 17 } 18 19 void 20 _finish(int status, char *term) 21 { 22 int i, nalive; 23 char *cp; 24 25 if(_finishing) 26 _EXITS(exitstatus); 27 _finishing = 1; 28 if(status){ 29 cp = _ultoa(exitstatus, status & 0xFF); 30 *cp = 0; 31 }else if(term){ 32 strncpy(exitstatus, term, ERRLEN); 33 } 34 if(_sessleader) 35 kill(0, SIGTERM); 36 _EXITS(exitstatus); 37 } 38 39 /* emulate: return p+sprintf(p, "%uld", v) */ 40 #define IDIGIT 15 41 char * 42 _ultoa(char *p, unsigned long v) 43 { 44 char s[IDIGIT]; 45 int n, i; 46 47 s[IDIGIT-1] = 0; 48 for(i = IDIGIT-2; i; i--){ 49 n = v % 10; 50 s[i] = n + '0'; 51 v = v / 10; 52 if(v == 0) 53 break; 54 } 55 strcpy(p, s+i); 56 return p + (IDIGIT-1-i); 57 } 58