home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXTST8F.ZIP / EMX / TEST / PIPE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  3.2 KB  |  163 lines

  1. /* pipe.c (emx+gcc) */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <io.h>
  7. #include <process.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <signal.h>
  11.  
  12. static int alive;
  13.  
  14. static void do_wait (void)
  15. {
  16.   int p, t;
  17.  
  18.   p = wait (&t);
  19.   if (p == -1)
  20.     perror ("wait");
  21.   else
  22.     {
  23.       --alive;
  24.       if ((t & 0xff) == 0)
  25.         fprintf (stderr, "Process %d terminated normally, rc=%d\n",
  26.                  p, t >> 8);
  27.       else if ((t & 0xff) == 127)
  28.         fprintf (stderr, "Process %d stopped by signal %d\n",
  29.                  p, t >> 8);
  30.       else
  31.         fprintf (stderr, "Process %d terminated by signal %d\n",
  32.                  p, t & 0xff);
  33.     }
  34. }
  35.  
  36.  
  37. static void handler (int sig)
  38. {
  39.   fprintf (stderr, "SIGCLD: "); fflush (stderr);
  40.   do_wait ();
  41.   fflush (stderr);
  42.   signal (SIGCLD, SIG_ACK);
  43. }
  44.  
  45.  
  46. static void my_dup2 (int from, int to)
  47. {
  48.   int rc;
  49.  
  50.   close (to);
  51.   rc = dup (from);
  52.   if (rc == -1)
  53.     {
  54.       perror ("dup");
  55.       exit (2);
  56.     }
  57.   if (rc != to)
  58.     {
  59.       fputs ("dup failed\n", stderr);
  60.       exit (2);
  61.     }
  62.   close (from);
  63.   if (fcntl (to, F_SETFD, 0) != 0)
  64.     {
  65.       perror ("fcntl");
  66.       exit (2);
  67.     }
  68. }
  69.  
  70.  
  71. static void private (int handle)
  72. {
  73.   if (fcntl (handle, F_SETFD, 1) != 0)
  74.     {
  75.       perror ("fcntl");
  76.       exit (2);
  77.     }
  78. }
  79.  
  80.  
  81. int main (int argc, char *argv[])
  82. {
  83.   int ph[2];
  84.   int org_stdin, org_stdout, next_stdin, next_stdout;
  85.   int i, j, verbose, sig;
  86.   char *nargv[1024], *p;
  87.  
  88.   i = 1; verbose = 0; sig = 0; alive = 0;
  89.   while (i < argc)
  90.     if (strcmp (argv[i], "-s") == 0)
  91.       {
  92.         sig = 1;
  93.         ++i;
  94.       }
  95.     else if (strcmp (argv[i], "-v") == 0)
  96.       {
  97.         verbose = 1;
  98.         ++i;
  99.       }
  100.     else
  101.       break;
  102.   if (i >= argc)
  103.     {
  104.       fputs ("Usage: pipe [-s] [-v] <program1> <program2> ...\n", stderr);
  105.       return (1);
  106.     }
  107.   if (sig)
  108.     signal (SIGCLD, handler);
  109.   org_stdin = dup (STDIN_FILENO);
  110.   if (org_stdin < 0)
  111.     {
  112.       perror ("dup stdin");
  113.       return (2);
  114.     }
  115.   org_stdout = dup (STDOUT_FILENO);
  116.   if (org_stdout < 0)
  117.     {
  118.       perror ("dup stdout");
  119.       return (2);
  120.     }
  121.   private (STDIN_FILENO);
  122.   private (STDOUT_FILENO);
  123.   ph[0] = org_stdin;
  124.   while (i < argc)
  125.     {
  126.       next_stdin = ph[0];
  127.       if (i < argc-1)
  128.         {
  129.           if (pipe (ph) != 0)
  130.             {
  131.               perror ("pipe");
  132.               return (2);
  133.             }
  134.           private (ph[0]);
  135.           private (ph[1]);
  136.           next_stdout = ph[1];
  137.         }
  138.       else
  139.         next_stdout = org_stdout;
  140.       my_dup2 (next_stdin,  STDIN_FILENO);
  141.       my_dup2 (next_stdout, STDOUT_FILENO);
  142.       p = argv[i]; j = 0;
  143.       while ((nargv[j++] = strtok (p, " \t")) != NULL)
  144.         p = NULL;
  145.       if (spawnvp (P_NOWAIT, nargv[0], (const char * const *)nargv) == -1)
  146.         {
  147.           perror ("spawnlp");
  148.           return (2);
  149.         }
  150.       ++i; ++alive;
  151.     }
  152.   while (alive > 0)
  153.     if (sig)
  154.       sleep (10);
  155.     else if (verbose)
  156.       do_wait ();
  157.     else if (wait (NULL) < 0)
  158.       perror ("wait");
  159.     else
  160.       --alive;
  161.   return (0);
  162. }
  163.