home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / pipe.c < prev    next >
C/C++ Source or Header  |  1992-01-14  |  3KB  |  122 lines

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