1 #include "lib.h" 2 #include <stdlib.h> 3 #include <sys/wait.h> 4 #include <sys/stat.h> 5 #include <unistd.h> 6 #include <errno.h> 7 #include <stdio.h> 8 #include "sys9.h" 9 10 pid_t 11 wait(int *stat_loc) 12 { 13 return waitpid(-1, stat_loc, 0); 14 } 15 16 pid_t 17 waitpid(int pid, int *stat_loc, int options) 18 { 19 int n, i, wfd, r, t, wpid; 20 char *bp, *ep, pname[50]; 21 struct stat buf; 22 Waitmsg w; 23 24 if(options&WNOHANG){ 25 sprintf(pname, "/proc/%d/wait", getpid()); 26 i = stat(pname, &buf); 27 if(i >=0 && buf.st_size==0) 28 return 0; 29 } 30 n = 0; 31 while(n==0){ 32 n = _WAIT(&w); 33 if(n < 0){ 34 _syserrno(); 35 }else{ 36 wpid = strtol(w.pid, 0, 0); 37 if(pid>0 && wpid!=pid) 38 continue; 39 n = wpid; 40 if(stat_loc){ 41 r = 0; 42 t = 0; 43 if(w.msg[0]){ 44 /* message is 'prog pid:string' */ 45 bp = w.msg; 46 while(*bp){ 47 if(*bp++ == ':') 48 break; 49 } 50 if(*bp == 0) 51 bp = w.msg; 52 r = strtol(bp, &ep, 10); 53 if(*ep == 0){ 54 if(r < 0 || r >= 256) 55 r = 1; 56 }else{ 57 t = _stringsig(bp); 58 if(t == 0) 59 r = 1; 60 } 61 } 62 *stat_loc = (r << 8) | t; 63 } 64 } 65 } 66 return n; 67 } 68 69