home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d156 / flex.lha / Flex / Flex2 / gnu.lib.src / popen.c < prev    next >
C/C++ Source or Header  |  1988-10-02  |  3KB  |  119 lines

  1. /*
  2.  * popen() -- open a pipe to a subprocess. It can be either a "read"
  3.  *            pipe or a "write" pipe. Since AmigaDog does not come
  4.  *            standard with pipes, but they are available from 3rd
  5.  *            parties, popen() will first try to open the device "PIPE:"
  6.  *            with the subprocess RUNning asynchronously. If it does not
  7.  *            succeed, it will open a temporary file in RAM: and run
  8.  *            the processes sequentially, using Execute.
  9.  *
  10.  *            Author: Scott Henry,  22 Feb 88
  11.  */
  12.  
  13. #ifndef PIPE_DEVICE
  14. #define PIPE_DEVICE "PIPE:Q"
  15. #endif
  16.  
  17. #ifdef DEBUG
  18. #define DPRINTF1(s,x) fprintf(stderr,s,x)
  19. #else
  20. #define DPRINTF1(s,x)
  21. #endif
  22.  
  23. #include <libraries/dos.h>
  24. #include <stdio.h>
  25.  
  26. FILE *
  27. popen( command, type)
  28.         char *command;
  29.         char *type;
  30. {
  31.    struct FileHandle *FH=NULL, *Open();
  32.    FILE   *fp=NULL, *fopen();
  33.    char   buffer[128];
  34.    char   *malloc();
  35.    int    i;
  36.  
  37.    setmem( buffer, 128, '\0');
  38.    if (*type == 'w')        /* write pipe */
  39.     {
  40.       if ((fp = fopen(PIPE_DEVICE, "w")) != NULL)
  41.        {
  42.          strcpy( buffer, "c:run ");
  43.          for (i=6; *command && *command != ' ' && *command != '\t'; ++i)
  44.           {
  45.             buffer[i] = *command++;
  46.           }
  47.          strcat( buffer, " <");
  48.          strcat( buffer, PIPE_DEVICE);
  49.          strcat( buffer, " ");
  50.          strcat( buffer, command);
  51.          DPRINTF1( "popen(\"%s\", \"w\")\n", buffer);
  52.          if (Execute( buffer, 0L, 0L) == 0L)
  53.           {
  54.             fclose( fp);
  55.             return NULL;
  56.           }
  57.          return fp;
  58.  
  59.        }
  60.       else
  61.        {
  62.          return NULL;    /* can't use a temp file on popen() for write */
  63.             /* (think about it) */
  64.        }
  65.     }
  66.    else if (*type == 'r')
  67.     {
  68.       if ((fp = fopen(PIPE_DEVICE, "r")) != NULL)
  69.        {
  70.          strcpy( buffer, "c:run ");
  71.          for (i=6; *command && *command != ' ' && *command != '\t'; ++i)
  72.           {
  73.             buffer[i] = *command++;
  74.           }
  75.          strcat( buffer, " >");
  76.          strcat( buffer, PIPE_DEVICE);
  77.          strcat( buffer, " ");
  78.          strcat( buffer, command);
  79.          DPRINTF1( "popen(\"%s\", \"r\")\n", buffer);
  80.          if (Execute( buffer, 0L, 0L) == 0L)
  81.           {
  82.             fclose( fp);
  83.             return NULL;
  84.           }
  85.          return fp;
  86.  
  87.        }
  88.       else
  89.        {    /* can't open PIPE:, use a temporary file instead */
  90.          strcpy( buffer, "RAM:");
  91.          tmpnam( &buffer[4]);
  92.          DPRINTF1( "popen: tempfile=\"%s\"\n", buffer);
  93.          if ((FH = Open( buffer, MODE_NEWFILE)) == NULL)
  94.           {
  95.             return NULL;
  96.           }
  97.          if (Execute( command, 0L, FH) != 0)   /* implied wait for command... */
  98.           {
  99.             Close( FH);
  100.             return NULL;
  101.           }
  102.          Close( FH);
  103.          if ((fp = fopen( buffer, "r")) == NULL)
  104.           {
  105.             DeleteFile( buffer);
  106.             return NULL;
  107.           }
  108.          fp->_tmpname = (char *)malloc( strlen(buffer)+1);
  109.          strcpy( fp->_tmpname, buffer);
  110.          fp->_flags |= _TEMP;
  111.          return fp;
  112.        }
  113.     }
  114.    else
  115.     {
  116.       return NULL;    /* invalid mode */
  117.     }
  118. }
  119.