summaryrefslogtreecommitdiffstats
path: root/contrib/cvs/lib/waitpid.c
diff options
context:
space:
mode:
authorpeter <peter@FreeBSD.org>1996-08-20 23:46:10 +0000
committerpeter <peter@FreeBSD.org>1996-08-20 23:46:10 +0000
commit8982e501c77217c860f79bba431f46a62b607a21 (patch)
tree70187fdf5be4cbefd0baf46bddac7e5e32c13c24 /contrib/cvs/lib/waitpid.c
parent01ee40fd6a76f6ff7ef247fc1b2cf6e337f216c5 (diff)
downloadFreeBSD-src-8982e501c77217c860f79bba431f46a62b607a21.zip
FreeBSD-src-8982e501c77217c860f79bba431f46a62b607a21.tar.gz
Import of slightly trimmed cvs-1.8 distribution. Generated files
and non-unix code has been left out.
Diffstat (limited to 'contrib/cvs/lib/waitpid.c')
-rw-r--r--contrib/cvs/lib/waitpid.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/contrib/cvs/lib/waitpid.c b/contrib/cvs/lib/waitpid.c
new file mode 100644
index 0000000..e8ddeb8
--- /dev/null
+++ b/contrib/cvs/lib/waitpid.c
@@ -0,0 +1,76 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "system.h"
+#include "wait.h"
+
+#include <stdio.h>
+
+struct unreaped {
+ pid_t pid;
+ int status;
+};
+static struct unreaped *unreaped;
+static int n;
+
+static struct unreaped *ualloc (oldptr, n)
+ struct unreaped *oldptr;
+ int n;
+{
+ n *= sizeof (struct unreaped);
+ if (n == 0)
+ n = 1;
+ if (oldptr)
+ oldptr = (struct unreaped *) realloc ((char *) oldptr, n);
+ else
+ oldptr = (struct unreaped *) malloc (n);
+ if (oldptr == 0)
+ {
+ fprintf (stderr, "cannot allocate %d bytes\n", n);
+ exit (1);
+ }
+ return oldptr;
+}
+
+pid_t waitpid (pid, status, options)
+ pid_t pid;
+ int *status;
+ int options;
+{
+ int i;
+
+ /* initialize */
+ if (unreaped == 0)
+ {
+ unreaped = ualloc (unreaped, 1);
+ unreaped[0].pid = 0;
+ n = 1;
+ }
+
+ for (i = 0; unreaped[i].pid; i++)
+ if (unreaped[i].pid == pid)
+ {
+ *status = unreaped[i].status;
+ while (unreaped[i].pid)
+ {
+ unreaped[i] = unreaped[i+1];
+ i++;
+ }
+ n--;
+ return pid;
+ }
+
+ while (1)
+ {
+ pid_t p = wait3 (status, options, (struct rusage *) 0);
+
+ if (p == 0 || p == -1 || p == pid)
+ return p;
+
+ n++;
+ unreaped = ualloc (unreaped, n);
+ unreaped[n-1].pid = p;
+ unreaped[n-1].status = *status;
+ }
+}
OpenPOWER on IntegriCloud