home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / code / unixlib / !UnixLib / src / unix / c / pipe < prev    next >
Encoding:
Text File  |  1994-09-30  |  1.8 KB  |  114 lines

  1. static char sccs_id[] = "@(#) pipe.c 3.1 " __DATE__ " HJR";
  2.  
  3. /* pipe.c (c) Copyright 1990 H.Rogers */
  4.  
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <string.h>
  8.  
  9. #include "fcntl.h"
  10.  
  11. #include "sys/unix.h"
  12. #include "sys/os.h"
  13. #include "sys/syslib.h"
  14. #include "sys/dev.h"
  15. #include "sys/param.h"
  16.  
  17. int
  18. pipe (int *p)
  19. {
  20.   struct pipe *pi;
  21.   struct file *f0, *f1;
  22.   int fd0, fd1;
  23.   char file[32];
  24.   int pcnt;
  25.  
  26.   if ((fd0 = __fdalloc ()) < 0)
  27.     return (-1);
  28.   f0 = __u->file + fd0;
  29.   f0->dup = f0;
  30.   if ((fd1 = __fdalloc ()) < 0)
  31.     {
  32.       f0->dup = 0;
  33.       return (-1);
  34.     }
  35.   f1 = __u->file + fd1;
  36.   f0->dup = f1;
  37.   f1->dup = f0;
  38.  
  39.   {
  40.     register char *s = file;
  41.     register char *s2 = "/pipe/";
  42.     register int i;
  43.     char n[11];
  44.  
  45.     while (*s++ = *s2++);
  46.     s--;
  47.     pcnt = i = __intenv ("UnixLib$pcnt", 0);
  48.     s2 = n + 10;
  49.     *s2-- = 0;
  50.     do
  51.       {
  52.     *s2-- = (i % 10) + '0';
  53.     i /= 10;
  54.       }
  55.     while (i);
  56.     s2++;
  57.     while (*s++ = *s2++);
  58.   }
  59.  
  60.   f0->oflag = O_RDWR | O_CREAT | O_TRUNC | O_PIPE;
  61.  
  62.   {
  63.     int i;
  64.  
  65.     if ((i = (*(__dev[DEV_PIPE].open)) (file, 0777, f0)) < 0)
  66.       {
  67.     f0->dup = f1->dup = 0;
  68.     return (-1);
  69.       }
  70.     f0->dev = f1->dev = makedev (DEV_PIPE, i);
  71.   }
  72.  
  73.   {
  74.     int r[10];
  75.     int v[1];
  76.  
  77.     v[0] = pcnt + 1;
  78.     r[0] = (int) "UnixLib$pcnt";
  79.     r[1] = (int) v;
  80.     r[2] = 4;
  81.     r[3] = 0;
  82.     r[4] = 1;
  83.     os_swi (0x24, r);
  84.   }
  85.  
  86.   f0->oflag = O_RDONLY | O_PIPE;
  87.   f1->oflag = O_WRONLY | O_PIPE;
  88.  
  89.   f0->pid = f1->pid = __u->pid;
  90.  
  91.   if (!(pi = malloc (sizeof (struct pipe))))
  92.     {
  93.       close (fd0);
  94.       close (fd1);
  95.       return (-1);
  96.     }
  97.   pi->p[0] = f0;
  98.   pi->p[1] = f1;
  99.   if (!(pi->file = __permstr (file)))
  100.     {
  101.       close (fd0);
  102.       close (fd1);
  103.       free (pi);
  104.       return (-1);
  105.     }
  106.   pi->next = __pipe;
  107.   __pipe = pi;
  108.  
  109.   p[0] = fd0;
  110.   p[1] = fd1;
  111.  
  112.   return (0);
  113. }
  114.