home *** CD-ROM | disk | FTP | other *** search
- static char sccs_id[] = "@(#) pipe.c 3.1 " __DATE__ " HJR";
-
- /* pipe.c (c) Copyright 1990 H.Rogers */
-
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
-
- #include "fcntl.h"
-
- #include "sys/unix.h"
- #include "sys/os.h"
- #include "sys/syslib.h"
- #include "sys/dev.h"
- #include "sys/param.h"
-
- int
- pipe (int *p)
- {
- struct pipe *pi;
- struct file *f0, *f1;
- int fd0, fd1;
- char file[32];
- int pcnt;
-
- if ((fd0 = __fdalloc ()) < 0)
- return (-1);
- f0 = __u->file + fd0;
- f0->dup = f0;
- if ((fd1 = __fdalloc ()) < 0)
- {
- f0->dup = 0;
- return (-1);
- }
- f1 = __u->file + fd1;
- f0->dup = f1;
- f1->dup = f0;
-
- {
- register char *s = file;
- register char *s2 = "/pipe/";
- register int i;
- char n[11];
-
- while (*s++ = *s2++);
- s--;
- pcnt = i = __intenv ("UnixLib$pcnt", 0);
- s2 = n + 10;
- *s2-- = 0;
- do
- {
- *s2-- = (i % 10) + '0';
- i /= 10;
- }
- while (i);
- s2++;
- while (*s++ = *s2++);
- }
-
- f0->oflag = O_RDWR | O_CREAT | O_TRUNC | O_PIPE;
-
- {
- int i;
-
- if ((i = (*(__dev[DEV_PIPE].open)) (file, 0777, f0)) < 0)
- {
- f0->dup = f1->dup = 0;
- return (-1);
- }
- f0->dev = f1->dev = makedev (DEV_PIPE, i);
- }
-
- {
- int r[10];
- int v[1];
-
- v[0] = pcnt + 1;
- r[0] = (int) "UnixLib$pcnt";
- r[1] = (int) v;
- r[2] = 4;
- r[3] = 0;
- r[4] = 1;
- os_swi (0x24, r);
- }
-
- f0->oflag = O_RDONLY | O_PIPE;
- f1->oflag = O_WRONLY | O_PIPE;
-
- f0->pid = f1->pid = __u->pid;
-
- if (!(pi = malloc (sizeof (struct pipe))))
- {
- close (fd0);
- close (fd1);
- return (-1);
- }
- pi->p[0] = f0;
- pi->p[1] = f1;
- if (!(pi->file = __permstr (file)))
- {
- close (fd0);
- close (fd1);
- free (pi);
- return (-1);
- }
- pi->next = __pipe;
- __pipe = pi;
-
- p[0] = fd0;
- p[1] = fd1;
-
- return (0);
- }
-