home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / snews-20.zip / PIPE.C < prev    next >
C/C++ Source or Header  |  1992-04-05  |  3KB  |  129 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. 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.     char *tmp = getenv("TMP");
  24.     int cur;
  25.     pipemode curmode;
  26.  
  27.     /*
  28.     ** decide on mode.
  29.     */
  30.     if(strchr(mode, 'r') != NULL)
  31.         curmode = reading;
  32.     else if(strchr(mode, 'w') != NULL)
  33.         curmode = writing;
  34.     else
  35.         return NULL;
  36.  
  37.     /*
  38.     ** get a name to use.
  39.     */
  40.  
  41.     strcpy(name, tmp ? tmp : "\\");
  42.     if ( name[strlen(name) - 1] != '\\' )
  43.       strcat(name, "\\");
  44.     strcat(name, "piXXXXXX");
  45.     mktemp(name);
  46.  
  47.     /*
  48.     ** If we're reading, just call system to get a file filled with
  49.     ** output.
  50.     */
  51.     if(curmode == reading)
  52.     {
  53.         char cmd[256];
  54.         sprintf(cmd,"%s > %s", command, name);
  55.         system(cmd);
  56.  
  57.         if((current = fopen(name, mode)) == NULL)
  58.             return NULL;
  59.     }
  60.     else
  61.     {
  62.         if((current = fopen(name, mode)) == NULL)
  63.             return NULL;
  64.     }
  65.  
  66.     cur = fileno(current);
  67.     pipes[cur].name = strdup(name);
  68.     pipes[cur].command = strdup(command);
  69.     pipes[cur].pmode = curmode;
  70.  
  71.     return current;
  72. }
  73.  
  74. static int dos_pclose(FILE * current)
  75. {
  76.     int cur = fileno(current), rval;
  77.     char command[256];
  78.  
  79.     /*
  80.     ** check for an open file.
  81.     */
  82.     if(pipes[cur].pmode == unopened)
  83.         return -1;
  84.  
  85.     if(pipes[cur].pmode == reading)
  86.     {
  87.         /*
  88.         ** input pipes are just files we're done with.
  89.         */
  90.         rval = fclose(current);
  91.         unlink(pipes[cur].name);
  92.     }
  93.     else
  94.     {
  95.         /*
  96.         ** output pipes are temporary files we have
  97.         ** to cram down the throats of programs.
  98.         */
  99.         fclose(current);
  100.         sprintf(command,"%s < %s", pipes[cur].command, pipes[cur].name);
  101.         rval = system(command);
  102.         unlink(pipes[cur].name);
  103.     }
  104.  
  105.     /*
  106.     ** clean up current pipe.
  107.     */
  108.     free(pipes[cur].name);
  109.     free(pipes[cur].command);
  110.     pipes[cur].pmode = unopened;
  111.  
  112.     return rval;
  113. }
  114.  
  115. int pipe(int *handles)
  116. {
  117.   return _pipe(handles, 4096, O_BINARY);
  118. }
  119.  
  120. FILE *popen(char *cmd, char *mode)
  121. {
  122.   return (_osmode == DOS_MODE) ? dos_popen(cmd, mode) : _popen(cmd, mode);
  123. }
  124.  
  125. int pclose(FILE *ptr)
  126. {
  127.   return (_osmode == DOS_MODE) ? dos_pclose(ptr) : _pclose(ptr);
  128. }
  129.