home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / emx / test / popen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-29  |  3.3 KB  |  151 lines

  1. /* popen.c (emx+gcc) */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7.  
  8. const char *text[] =
  9. {
  10.   "one", "two", "three", "four", "five", "six", "seven", NULL
  11.   };
  12.  
  13.  
  14. static void usage (void)
  15. {
  16.   fputs ("Usage: popen -i[1] <command>\n"
  17.          "       popen -o <command>\n"
  18.          "       popen -f <file> <command>\n", stderr);
  19.   exit (1);
  20. }
  21.  
  22. int main (int argc, char *argv[])
  23. {
  24.   FILE *f, *feed;
  25.   int c, i, j, mode, one;
  26.   char buf[2048], *feed_fname;
  27.  
  28.   mode = 0; one = 0;
  29.   while ((c = getopt (argc, argv, "fio1")) != EOF)
  30.     switch (c)
  31.       {
  32.       case 'f':
  33.       case 'i':
  34.       case 'o':
  35.         if (mode != 0)
  36.           usage ();
  37.         mode = c;
  38.         break;
  39.       case '1':
  40.         one = 1;
  41.         break;
  42.       default:
  43.         usage ();
  44.       }
  45.   if (mode == 0 || (mode != 'i' && one) || argc - optind < 1)
  46.     usage ();
  47.   if (mode == 'f')
  48.     {
  49.       if (argc - optind < 2)
  50.         usage ();
  51.       feed_fname = argv[optind];
  52.       feed = fopen (feed_fname, "rt");
  53.       if (feed == NULL)
  54.         {
  55.           perror (feed_fname);
  56.           return (2);
  57.         }
  58.       ++optind;
  59.     }
  60.   buf[0] = 0;
  61.   for (i = optind; i < argc; ++i)
  62.     {
  63.       if (buf[0] != 0)
  64.         strcat (buf, " ");
  65.       strcat (buf, argv[i]);
  66.     }
  67.   if (mode == 'i')
  68.     {
  69.       f = popen (buf, "rt");
  70.       if (f == NULL)
  71.         {
  72.           perror ("popen");
  73.           return (2);
  74.         }
  75.       i = 1;
  76.       while (fgets (buf, sizeof (buf), f) != NULL)
  77.         {
  78.           printf ("%4d: %s", i++, buf);
  79.           if (one)
  80.             break;
  81.         }
  82.     }
  83.   else
  84.     {
  85.       f = popen (buf, "wt");
  86.       if (f == NULL)
  87.         {
  88.           perror ("popen");
  89.           return (2);
  90.         }
  91.       if (mode == 'f')
  92.         {
  93.           for (;;)
  94.             {
  95.               i = fread (buf, 1, sizeof (buf), feed);
  96.               if (ferror (feed))
  97.                 {
  98.                   perror (feed_fname);
  99.                   return (2);
  100.                 }
  101.               if (i == 0)
  102.                 break;
  103.               j = fwrite (buf, 1, i, f);
  104.               if (ferror (f))
  105.                 {
  106.                   perror ("fwrite");
  107.                   return (2);
  108.                 }
  109.               if (j != i)
  110.                 {
  111.                   fputs ("Pipe full\n", stderr);
  112.                   break;
  113.                 }
  114.             }
  115.           fclose (feed);
  116.         }
  117.       else
  118.         for (i = 0; text[i] != NULL; ++i)
  119.           {
  120.             if (fputs (text[i], f) != 0)
  121.               {
  122.                 perror ("fputs");
  123.                 return (2);
  124.               }
  125.             if (fputc ('\n', f) == EOF)
  126.               {
  127.                 perror ("fputc");
  128.                 return (2);
  129.               }
  130.             if (fflush (f) != 0)
  131.               {
  132.                 perror ("fflush");
  133.                 return (2);
  134.               }
  135.           }
  136.     }
  137.   i = pclose (f);
  138.   if (i < 0)
  139.     {
  140.       perror ("pclose");
  141.       return (2);
  142.     }
  143.   if ((i & 0xff) == 0)
  144.     printf ("Normal process termination, rc=%d\n", i >> 8);
  145.   else if ((i & 0xff) == 127)
  146.     printf ("Process stopped by signal %d\n", i >> 8);
  147.   else
  148.     printf ("Process terminated by signal %d\n", i & 0xff);
  149.   return (0);
  150. }
  151.