home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuawk.zip / pc / popen.c < prev    next >
C/C++ Source or Header  |  1995-12-15  |  3KB  |  125 lines

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