home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume8 / cutup / part01 / fifo.c < prev    next >
C/C++ Source or Header  |  1989-09-18  |  2KB  |  121 lines

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/fcntl.h>
  4. #include <errno.h>
  5. #include <curses.h>
  6. #include "consts.h"
  7. #include "types.h"
  8. #include "funcs.h"
  9. #include "macros.h"
  10.  
  11. #define FIFO_MODE (S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO)
  12.  
  13. extern int StopIO;
  14.  
  15. static char Fifo1[FN_MAX];
  16. static char Fifo2[FN_MAX];
  17.  
  18. int PipeOpen();
  19.  
  20. int GetFIFOName(old_w, pipe_w, pipe_r, you, hom, us, them)
  21.  
  22. WINDOW *old_w;
  23. int *pipe_w;
  24. int *pipe_r;
  25. char *you;
  26. char *hom;
  27. coords_t *us;
  28. coords_t *them;
  29.  
  30.     {
  31.     int err, cmask;
  32.     WINDOW *fifo_w;
  33.     char mesg[256];
  34.     int len;
  35.  
  36.     cmask = umask(0);
  37.     if (! StopIO)
  38.         {
  39.         sprintf(mesg, "waiting for %s...", hom);
  40.         len = strlen(mesg) + 3;
  41.         fifo_w = CreateWindow(old_w, 3, len, 1, 1);
  42.         mvwaddstr(fifo_w, 1, 1, mesg);
  43.         wrefresh(fifo_w);
  44.         }
  45.     sprintf(Fifo1, "/tmp/%s%s", you, hom);
  46.     sprintf(Fifo2, "/tmp/%s%s", hom, you);
  47.     if (mknod(Fifo1, FIFO_MODE, 0))
  48.         if (mknod(Fifo2, FIFO_MODE, 0))
  49.             {
  50.             if (err = PipeOpen(fifo_w, Fifo1, Fifo2, pipe_r, pipe_w,
  51.                 O_RDONLY, O_WRONLY, us, them))
  52.                 return err;
  53.             }
  54.         else
  55.             {
  56.             if (err = PipeOpen(fifo_w, Fifo2, Fifo1, pipe_r, pipe_w,
  57.                 O_RDONLY, O_WRONLY, them, us))
  58.                 return err;
  59.             }
  60.     else
  61.         if (mknod(Fifo2, FIFO_MODE, 0))
  62.             {
  63.             if (err = PipeOpen(fifo_w, Fifo1, Fifo2, pipe_w, pipe_r,
  64.                 O_WRONLY, O_RDONLY, us, them))
  65.                 return err;
  66.             }
  67.         else
  68.             {
  69.             if (err = PipeOpen(fifo_w, Fifo2, Fifo1, pipe_w, pipe_r,
  70.                 O_WRONLY, O_RDONLY, them, us))
  71.                 return err;
  72.             }
  73.     (void)umask(cmask);
  74.     if (! StopIO)
  75.         RemoveWindow(old_w, fifo_w);
  76.     return 0;
  77.     }
  78.  
  79. static int PipeOpen(old_w, fifo1, fifo2, pipe1, pipe2, mode1, mode2, c1, c2)
  80.  
  81. WINDOW *old_w;
  82. char *fifo1;
  83. char *fifo2;
  84. int *pipe1;
  85. int *pipe2;
  86. int mode1;
  87. int mode2;
  88. coords_t *c1;
  89. coords_t *c2;
  90.  
  91.     {
  92.     int status;
  93.  
  94.     c2->y = 10;
  95.     c2->x = 20;
  96.     c2->dir = S_WEST;
  97.     c1->y = 10;
  98.     c1->x = 57;
  99.     c1->dir = S_EAST;
  100.     if ((*pipe1 = open(fifo1, mode1)) == -1)
  101.         {
  102.         status = errno;
  103.         DISPLAY_ERROR(old_w, "Open failure 1.");
  104.         return status;
  105.         }
  106.     if ((*pipe2 = open(fifo2, mode2)) == -1)
  107.         {
  108.         status = errno;
  109.         DISPLAY_ERROR(old_w, "Open failure 2.");
  110.         return status;
  111.         }
  112.     return 0;
  113.     }
  114.  
  115. void EndFIFO()
  116.  
  117.     {
  118.     (void)unlink(Fifo1);
  119.     (void)unlink(Fifo2);
  120.     }
  121.