home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Product / Product.zip / sc621_3.zip / src / popen.c < prev    next >
C/C++ Source or Header  |  1993-11-09  |  3KB  |  113 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.         char cmd[256];
  59.         sprintf(cmd,"%s > %s",command,name);
  60.         system(cmd);
  61.         if((current = fopen(name,"r")) == NULL)
  62.             return NULL;
  63.     } else {
  64.         if((current = fopen(name,"w")) == NULL)
  65.             return NULL;
  66.     }
  67.     cur = fileno(current);
  68.     pipes[cur].name = name;
  69.     pipes[cur].pmode = curmode;
  70.     pipes[cur].command = strdup(command);
  71.     return current;
  72. }
  73.  
  74. int
  75. os_pclose( FILE * current) {
  76.     int cur = fileno(current),rval;
  77.  
  78. #if defined(__OS2__) && (_MSC_VER != 510)
  79.     if (_osmode == OS2_MODE)
  80.       return(pclose(current));
  81. #endif
  82.  
  83.     /*
  84.     ** check for an open file.
  85.     */
  86.     if(pipes[cur].pmode == unopened)
  87.         return -1;
  88.     if(pipes[cur].pmode == reading) {
  89.         /*
  90.         ** input pipes are just files we're done with.
  91.         */
  92.         rval = fclose(current);
  93.         unlink(pipes[cur].name);
  94.     } else {
  95.         /*
  96.         ** output pipes are temporary files we have
  97.         ** to cram down the throats of programs.
  98.         */
  99.         char command[256];
  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.     ** clean up current pipe.
  107.     */
  108.     pipes[cur].pmode = unopened;
  109.     free(pipes[cur].name);
  110.     free(pipes[cur].command);
  111.     return rval;
  112. }
  113.