Annotation of researchv10no/lbin/Mail/popen.c, revision 1.1

1.1     ! root        1: #ifndef lint
        !             2: static char *sccsid = "@(#)popen.c     1.7 (Berkeley) 8/13/83";
        !             3: #endif
        !             4: 
        !             5: #include <stdio.h>
        !             6: #include <signal.h>
        !             7: #include <errno.h>
        !             8: #define        tst(a,b)        (*mode == 'r'? (b) : (a))
        !             9: #define        RDR     0
        !            10: #define        WTR     1
        !            11: static int     popen_pid[20];
        !            12: 
        !            13: #ifdef VMUNIX
        !            14: #define        mask(s) (1<<((s)-1))
        !            15: #else
        !            16: #define vfork  fork
        !            17: #endif VMUNIX
        !            18: #ifndef        SIGRETRO
        !            19: #define        sigchild()
        !            20: #endif
        !            21: 
        !            22: FILE *
        !            23: popen(cmd,mode)
        !            24: char   *cmd;
        !            25: char   *mode;
        !            26: {
        !            27:        int p[2];
        !            28:        register myside, hisside, pid;
        !            29: 
        !            30:        if(pipe(p) < 0)
        !            31:                return NULL;
        !            32:        myside = tst(p[WTR], p[RDR]);
        !            33:        hisside = tst(p[RDR], p[WTR]);
        !            34:        if((pid = vfork()) == 0) {
        !            35:                /* myside and hisside reverse roles in child */
        !            36:                sigchild();
        !            37:                close(myside);
        !            38:                dup2(hisside, tst(0, 1));
        !            39:                close(hisside);
        !            40:                execl("/bin/sh", "sh", "-c", cmd, 0);   /* mjm: csh --> sh */
        !            41:                perror("/bin/sh");                      /* mjm */
        !            42:                _exit(1);
        !            43:        }
        !            44:        if(pid == -1)
        !            45:                return NULL;
        !            46:        popen_pid[myside] = pid;
        !            47:        close(hisside);
        !            48:        return(fdopen(myside, mode));
        !            49: }
        !            50: 
        !            51: pclose(ptr)
        !            52: FILE *ptr;
        !            53: {
        !            54:        register f, r;
        !            55:        int status, omask;
        !            56:        extern int errno;
        !            57: 
        !            58:        f = fileno(ptr);
        !            59:        fclose(ptr);
        !            60:        while((r = wait(&status)) != popen_pid[f] && r != -1 && errno != EINTR)
        !            61:                ;
        !            62:        if(r == -1)
        !            63:                status = -1;
        !            64:        return(status);
        !            65: }

unix.superglobalmegacorp.com

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