Annotation of researchv10dc/cmd/library/dospopen.c, revision 1.1

1.1     ! root        1: /*     library:dospopen.c      1.2     */
        !             2: #include "sccsid.h"
        !             3: VERSION(@(#)library:dospopen.c 1.2)
        !             4: 
        !             5: /* popen/pclose: simple MS-DOS piping scheme to imitate UNIX pipes */
        !             6: 
        !             7: /* information needed between popen and pclose */
        !             8: #include <stdio.h>
        !             9: 
        !            10: static char *prgname[32];          /* program name if write pipe */
        !            11: static int pipetype[32];           /* 1=read 2=write */
        !            12: 
        !            13: /* open a pipe */
        !            14: FILE *popen(prg,type)
        !            15: char *prg,*type;
        !            16: { FILE *p;
        !            17:   int ostdout, ostdin;
        !            18: 
        !            19:   switch(*type) {
        !            20: 
        !            21:     /* for write style pipe, pclose handles program execution */
        !            22:     case 'w' :
        !            23:       if ((p= fopen("\\pipe.tmp","w")) != NULL) {
        !            24:         pipetype[fileno(p)]= 1;
        !            25:         prgname[fileno(p)]= prg;
        !            26:       }
        !            27:       return(p);
        !            28: 
        !            29: /* read pipe must create tmp file, set up stdout to point to the temp
        !            30:  * file, and run the program.  note that if the pipe file cannot be
        !            31:  * opened, it'll return a condition indicating pipe failure, which is
        !            32:  * fine.
        !            33:  */
        !            34: 
        !            35:     case 'r' :
        !            36:       if ((p= fopen("\\pipe.tmp","w")) != NULL) {
        !            37:         pipetype[fileno(p)]= 2;
        !            38:         ostdout= dup(fileno(stdout));   /* we need this later */
        !            39:         dup2(fileno(stdout),fileno(p)); /* substitute for stdout */
        !            40:         system(prg);                    /* run the program */
        !            41:         dup2(fileno(stdout),ostdout);   /* repair stdout */
        !            42:         fclose(p);                      /* close redirected stdout */
        !            43:         return(fopen("\\pipe.tmp","r"));     /* return pointer to tmp file */
        !            44:       }
        !            45:       return(NULL);                     /* everyone has their problems */
        !            46: 
        !            47:     /* screwy call or unsupported type */
        !            48:     default :
        !            49:       printf("popen: unknown pipe style\n");
        !            50:       exit(1);
        !            51:   }
        !            52: }
        !            53: 
        !            54: /* close a pipe */
        !            55: void pclose(p)
        !            56: FILE *p;
        !            57: { int n;
        !            58:   int ostdout, ostdin;
        !            59:   FILE *p2;
        !            60: 
        !            61:   switch(pipetype[fileno(p)]) {
        !            62: 
        !            63: /* close the temp file, open again as read, redirect stdin from that
        !            64:  * file, run the program, then clean up.
        !            65:  */
        !            66: 
        !            67:     case 1 :
        !            68:       n= fileno(p);
        !            69:       fclose(p);
        !            70:       if ((p2= fopen("\\pipe.tmp","r")) != NULL) {
        !            71:         ostdin= dup(fileno(stdin));     /* save stdin for later */
        !            72:         dup2(fileno(stdin),fileno(p2)); /* redirect to tmp file */
        !            73:         system(prgname[n]);             /* run the program */
        !            74:         dup2(fileno(stdin),ostdin);     /* repair stdin */
        !            75:         fclose(p2);
        !            76:         unlink("\\pipe.tmp");            /* erase tmp file */
        !            77:         return;
        !            78:       }
        !            79:       printf("pclose: could not reopen temporary file\n");
        !            80:       exit(1);
        !            81: 
        !            82:     /* close the temp file and remove it */
        !            83:     case 2 :
        !            84:       n= fileno(p);        /* get file number for unlink */
        !            85:       fclose(p);           /* close the file */
        !            86:       unlink("\\pipe.tmp"); /* erase the file */
        !            87:       return;
        !            88: 
        !            89:     /* if we're neither read nor write, we have problems */
        !            90:     default :
        !            91:       printf("pclose: internal error\n");
        !            92:       exit(1);
        !            93:   }
        !            94: }
        !            95: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.