Annotation of 42BSD/ucb/Mail/popen.c, revision 1.1.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/csh", "sh", "-c", cmd, 0);
                     41:                _exit(1);
                     42:        }
                     43:        if(pid == -1)
                     44:                return NULL;
                     45:        popen_pid[myside] = pid;
                     46:        close(hisside);
                     47:        return(fdopen(myside, mode));
                     48: }
                     49: 
                     50: pclose(ptr)
                     51: FILE *ptr;
                     52: {
                     53:        register f, r;
                     54:        int status, omask;
                     55:        extern int errno;
                     56: 
                     57:        f = fileno(ptr);
                     58:        fclose(ptr);
                     59: # ifdef VMUNIX
                     60:        omask = sigblock(mask(SIGINT)|mask(SIGQUIT)|mask(SIGHUP));
                     61: # endif VMUNIX
                     62:        while((r = wait(&status)) != popen_pid[f] && r != -1 && errno != EINTR)
                     63:                ;
                     64:        if(r == -1)
                     65:                status = -1;
                     66: # ifdef VMUNIX
                     67:        sigsetmask(omask);
                     68: # endif VMUNIX
                     69:        return(status);
                     70: }

unix.superglobalmegacorp.com

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