home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / rcs567s.zip / rcsfront / pipe.c < prev   
C/C++ Source or Header  |  1994-04-08  |  3KB  |  149 lines

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