summaryrefslogtreecommitdiff
path: root/sys/src/ape/lib/ap/plan9/_exit.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/ape/lib/ap/plan9/_exit.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/ape/lib/ap/plan9/_exit.c')
-rwxr-xr-xsys/src/ape/lib/ap/plan9/_exit.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/sys/src/ape/lib/ap/plan9/_exit.c b/sys/src/ape/lib/ap/plan9/_exit.c
new file mode 100755
index 000000000..e3886ff18
--- /dev/null
+++ b/sys/src/ape/lib/ap/plan9/_exit.c
@@ -0,0 +1,58 @@
+#include "lib.h"
+#include "sys9.h"
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+
+int _finishing = 0;
+int _sessleader = 0;
+
+static char exitstatus[ERRMAX];
+
+void
+_exit(int status)
+{
+ _finish(status, 0);
+}
+
+void
+_finish(int status, char *term)
+{
+ int i, nalive;
+ char *cp;
+
+ if(_finishing)
+ _EXITS(exitstatus);
+ _finishing = 1;
+ if(status){
+ cp = _ultoa(exitstatus, status & 0xFF);
+ *cp = 0;
+ }else if(term){
+ strncpy(exitstatus, term, ERRMAX);
+ exitstatus[ERRMAX-1] = '\0';
+ }
+ if(_sessleader)
+ kill(0, SIGTERM);
+ _EXITS(exitstatus);
+}
+
+/* emulate: return p+sprintf(p, "%uld", v) */
+#define IDIGIT 15
+char *
+_ultoa(char *p, unsigned long v)
+{
+ char s[IDIGIT];
+ int n, i;
+
+ s[IDIGIT-1] = 0;
+ for(i = IDIGIT-2; i; i--){
+ n = v % 10;
+ s[i] = n + '0';
+ v = v / 10;
+ if(v == 0)
+ break;
+ }
+ strcpy(p, s+i);
+ return p + (IDIGIT-1-i);
+}