home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / emx / test / select.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-28  |  3.5 KB  |  164 lines

  1. /* select.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 <sys/ioctl.h>
  10. #include <sys/types.h>
  11. #include <sys/time.h>
  12. #include <fcntl.h>
  13. #include <signal.h>
  14.  
  15. #define min(a,b) (((a) < (b)) ? (a) : (b))
  16.  
  17. static void do_wait (void)
  18. {
  19.   int p, t;
  20.  
  21.   p = wait (&t);
  22.   if (p == -1)
  23.     perror ("wait");
  24.   else if ((t & 0xff) == 0)
  25.     fprintf (stderr, "Process %d terminated normally, rc=%d\n", p, t >> 8);
  26.   else if ((t & 0xff) == 127)
  27.     fprintf (stderr, "Process %d stopped by signal %d\n", p, t >> 8);
  28.   else
  29.     fprintf (stderr, "Process %d terminated by signal %d\n", p, t & 0xff);
  30. }
  31.  
  32.  
  33. static void handler (int sig)
  34. {
  35.   fprintf (stderr, "SIGCLD: "); fflush (stderr);
  36.   do_wait ();
  37.   fflush (stderr);
  38.   signal (SIGCLD, SIG_ACK);
  39. }
  40.  
  41.  
  42. static void my_dup2 (int from, int to)
  43. {
  44.   int rc;
  45.  
  46.   rc = dup2 (from, to);
  47.   if (rc == -1)
  48.     {
  49.       perror ("dup2");
  50.       exit (2);
  51.     }
  52.   close (from);
  53.   if (fcntl (to, F_SETFD, 0) != 0)
  54.     {
  55.       perror ("fcntl");
  56.       exit (2);
  57.     }
  58. }
  59.  
  60.  
  61. static void private (int handle)
  62. {
  63.   if (fcntl (handle, F_SETFD, 1) != 0)
  64.     {
  65.       perror ("fcntl");
  66.       exit (2);
  67.     }
  68. }
  69.  
  70.  
  71. int main (int argc, char *argv[])
  72. {
  73.   int ph[2], handles[256], pids[256];
  74.   int org_stdout;
  75.   int i, j, k, n, pid;
  76.   char *nargv[1024], buf[70], *p;
  77.   fd_set rfds;
  78.   struct timeval tv;
  79.  
  80.   if (argc < 2)
  81.     {
  82.       fputs ("Usage: select <program> ...\n", stderr);
  83.       return (1);
  84.     }
  85.   signal (SIGCLD, handler);
  86.   org_stdout = dup (STDOUT_FILENO);
  87.   if (org_stdout < 0)
  88.     {
  89.       perror ("dup stdout");
  90.       return (2);
  91.     }
  92.   private (org_stdout);
  93.   n = 0;
  94.   for (i = 1; i < argc; ++i)
  95.     {
  96.       if (pipe (ph) != 0)
  97.         {
  98.           perror ("pipe");
  99.           return (2);
  100.         }
  101.       private (ph[0]);
  102.       my_dup2 (ph[1], STDOUT_FILENO);
  103.       p = argv[i]; j = 0;
  104.       while ((nargv[j++] = strtok (p, " \t")) != NULL)
  105.         p = NULL;
  106.       pid = spawnvp (P_NOWAIT, nargv[0], (const char * const *)nargv);
  107.       if (pid == -1)
  108.         {
  109.           perror ("spawnlp");
  110.           return (2);
  111.         }
  112.       handles[n] = ph[0];
  113.       pids[n] = pid;
  114.       ++n;
  115.       dup2 (org_stdout, STDOUT_FILENO);
  116.     }
  117.   do
  118.     {
  119.       FD_ZERO (&rfds);
  120.       for (i = 0; i < n; ++i)
  121.         if (handles[i] >= 0)
  122.           FD_SET (handles[i], &rfds);
  123.       tv.tv_sec = 10;
  124.       tv.tv_usec = 0;
  125.       j = select (FD_SETSIZE, &rfds, NULL, NULL, &tv);
  126.       if (j < 0)
  127.         perror ("select");
  128.       else
  129.         printf ("j=%d\n", j);
  130.       k = 0;
  131.       if (j == 0)
  132.         {
  133.           for (i = 0; i < n; ++i)
  134.             if (pids[i] >= 0)
  135.               if (kill (pids[i], 0) >= 0)
  136.                 ++k;
  137.               else
  138.                 {
  139.                   close (handles[i]);
  140.                   pids[i] = -1;
  141.                   handles[i] = -1;
  142.                 }
  143.         }
  144.       else
  145.         for (i = 0; i < n; ++i)
  146.           {
  147.             ++k;
  148.             if (handles[i] >= 0 && FD_ISSET (handles[i], &rfds) &&
  149.                 ioctl (handles[i], FIONREAD, &j) >= 0)
  150.               {
  151.                 if (j > 0)
  152.                   {
  153.                     printf ("*");
  154.                     j = read (handles[i], buf, min (j, sizeof (buf)));
  155.                     printf ("%d: [", i);
  156.                     fwrite (buf, 1, j, stdout);
  157.                     printf ("]\n");
  158.                   }
  159.               }
  160.           }
  161.     } while (k != 0);
  162.   return (0);
  163. }
  164.