Annotation of coherent/b/lib/libc/stdio/popen.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Standard I/O library
                      3:  * Make streams to or from a process.
                      4:  */
                      5: 
                      6: #include <stdio.h>
                      7: #include <signal.h>
                      8: 
                      9: #define        R       0       /* Pipe read descriptor index */
                     10: #define        W       1       /* Pipe write descriptor index */
                     11: 
                     12: static int     poppid[_NFILE];
                     13: 
                     14: FILE *
                     15: popen(command, type)
                     16: char *command, *type;
                     17: {
                     18:        register mode;
                     19:        register fd;
                     20:        int pd[2];
                     21: 
                     22:        if (pipe(pd) < 0)
                     23:                return (NULL);
                     24:        if (*type == 'w') {
                     25:                mode = W;
                     26:                fd = pd[W];
                     27:        } else {
                     28:                mode = R;
                     29:                fd = pd[R];
                     30:        }
                     31:        if ((poppid[fd] = fork()) < 0) {
                     32:                close(pd[R]);
                     33:                close(pd[W]);
                     34:                return (NULL);
                     35:        }
                     36:        if (poppid[fd] == 0) {          /* Child */
                     37:                if (mode == W) {
                     38:                        close(pd[W]);
                     39:                        close(fileno(stdin));
                     40:                        dup(pd[R]);
                     41:                        close(pd[R]);
                     42:                } else {
                     43:                        close(pd[R]);
                     44:                        close(fileno(stdout));
                     45:                        dup(pd[W]);
                     46:                        close(pd[W]);
                     47:                }
                     48:                execl("/bin/sh", "sh", "-c", command, NULL);
                     49:                exit(1);
                     50:        }
                     51:        if (mode == W) {
                     52:                close(pd[R]);
                     53:                return (fdopen(pd[W], "w"));
                     54:        } else {
                     55:                close(pd[W]);
                     56:                return (fdopen(pd[R], "r"));
                     57:        }
                     58: }
                     59: 
                     60: pclose(stream)
                     61: FILE *stream;
                     62: {
                     63:        register fd;
                     64:        register pid, wpid;
                     65:        int status;
                     66:        int (*hupfun)(), (*intfun)(), (*quitfun)();
                     67: 
                     68:        fd = fileno(stream);
                     69:        pid = poppid[fd];
                     70:        poppid[fd] = 0;
                     71:        if (pid==0 || fclose(stream)==EOF)
                     72:                return (-1);
                     73:        hupfun = signal(SIGHUP, SIG_IGN);
                     74:        intfun = signal(SIGINT, SIG_IGN);
                     75:        quitfun = signal(SIGQUIT, SIG_IGN);
                     76:        while ((wpid = wait(&status))!=pid && wpid>=0)
                     77:                ;
                     78:        if (wpid < 0)
                     79:                status = -1;
                     80:        signal(SIGHUP, hupfun);
                     81:        signal(SIGINT, intfun);
                     82:        signal(SIGQUIT, quitfun);
                     83:        return (status);
                     84: }

unix.superglobalmegacorp.com

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