home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / DIFFPT.ZIP / PIPE.C < prev    next >
C/C++ Source or Header  |  1991-07-01  |  2KB  |  120 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <io.h>
  4. #include <string.h>
  5.  
  6. #include <extlib.h>
  7.  
  8.  
  9. typedef enum { unopened = 0, reading, writing } pipemode;
  10.  
  11. static struct
  12. {
  13.     char *name;
  14.     char *command;
  15.     pipemode pmode;
  16. }
  17. pipes[_NFILE];
  18.  
  19. static FILE *dos_popen(char *command, char *mode)
  20. {
  21.     FILE *current;
  22.     char name[128];
  23.     int cur;
  24.     pipemode curmode;
  25.  
  26.     /*
  27.     ** decide on mode.
  28.     */
  29.     if(strchr(mode, 'r') != NULL)
  30.         curmode = reading;
  31.     else if(strchr(mode, 'w') != NULL)
  32.         curmode = writing;
  33.     else
  34.         return NULL;
  35.  
  36.     /*
  37.     ** get a name to use.
  38.     */
  39.     strcpy(name, "piXXXXXX");
  40.     Mktemp(name);
  41.  
  42.     /*
  43.     ** If we're reading, just call system to get a file filled with
  44.     ** output.
  45.     */
  46.     if(curmode == reading)
  47.     {
  48.         char cmd[256];
  49.         sprintf(cmd,"%s > %s", command, name);
  50.         system(cmd);
  51.  
  52.         if((current = fopen(name, mode)) == NULL)
  53.             return NULL;
  54.     }
  55.     else
  56.     {
  57.         if((current = fopen(name, mode)) == NULL)
  58.             return NULL;
  59.     }
  60.  
  61.     cur = fileno(current);
  62.     pipes[cur].name = strdup(name);
  63.     pipes[cur].command = strdup(command);
  64.     pipes[cur].pmode = curmode;
  65.  
  66.     return current;
  67. }
  68.  
  69. static int dos_pclose(FILE * current)
  70. {
  71.     int cur = fileno(current), rval;
  72.     char command[256];
  73.  
  74.     /*
  75.     ** check for an open file.
  76.     */
  77.     if(pipes[cur].pmode == unopened)
  78.         return -1;
  79.  
  80.     if(pipes[cur].pmode == reading)
  81.     {
  82.         /*
  83.         ** input pipes are just files we're done with.
  84.         */
  85.         rval = fclose(current);
  86.         unlink(pipes[cur].name);
  87.     }
  88.     else
  89.     {
  90.         /*
  91.         ** output pipes are temporary files we have
  92.         ** to cram down the throats of programs.
  93.         */
  94.         fclose(current);
  95.         sprintf(command,"%s < %s", pipes[cur].command, pipes[cur].name);
  96.         rval = system(command);
  97.         unlink(pipes[cur].name);
  98.     }
  99.  
  100.     /*
  101.     ** clean up current pipe.
  102.     */
  103.     free(pipes[cur].name);
  104.     free(pipes[cur].command);
  105.     pipes[cur].pmode = unopened;
  106.  
  107.     return rval;
  108. }
  109.  
  110.  
  111. FILE *popen(char *cmd, char *mode)
  112. {
  113.   return (_osmode == DOS_MODE) ? dos_popen(cmd, mode) : _popen(cmd, mode);
  114. }
  115.  
  116. int pclose(FILE *ptr)
  117. {
  118.   return (_osmode == DOS_MODE) ? dos_pclose(ptr) : _pclose(ptr);
  119. }
  120.