home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / lhaxenix.zoo / lharc.xenix / pipes.c < prev    next >
C/C++ Source or Header  |  1990-04-02  |  1KB  |  61 lines

  1. /* a simulation for the Unix popen() and pclose() calls on MS-DOS */
  2. /* only one pipe can be open at a time */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. static char pipename[128], command[128];
  9. static int wrpipe;
  10.  
  11. extern void Mktemp(char *);
  12.  
  13. FILE *popen(char *cmd, char *flags)
  14. {
  15.   wrpipe = (strchr(flags, 'w') != NULL);
  16.  
  17.   if ( wrpipe )
  18.   {
  19.     strcpy(command, cmd);
  20.     strcpy(pipename, "~WXXXXXX");
  21.     Mktemp(pipename);
  22.     return fopen(pipename, flags);  /* ordinary file */
  23.   }
  24.   else
  25.   {
  26.     strcpy(pipename, "~RXXXXXX");
  27.     Mktemp(pipename);
  28.     strcpy(command, cmd);
  29.     strcat(command, ">");
  30.     strcat(command, pipename);
  31.     system(command);
  32.     return fopen(pipename, flags);  /* ordinary file */
  33.   }
  34. }
  35.  
  36. int pclose(FILE *pipe)
  37. {
  38.   int rc;
  39.  
  40.   if ( fclose(pipe) == EOF )
  41.     return EOF;
  42.  
  43.   if ( wrpipe )
  44.   {
  45.     if ( command[strlen(command) - 1] == '!' )
  46.       command[strlen(command) - 1] = 0;
  47.     else
  48.       strcat(command, "<");
  49.  
  50.     strcat(command, pipename);
  51.     rc = system(command);
  52.     unlink(pipename);
  53.     return rc;
  54.   }
  55.   else
  56.   {
  57.     unlink(pipename);
  58.     return 0;
  59.   }
  60. }
  61.