home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / rcs567s.zip / diff16 / pipe.c < prev    next >
C/C++ Source or Header  |  1991-09-18  |  3KB  |  134 lines

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