[BACK]Return to popen.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / gen

Annotation of src/lib/libc/gen/popen.c, Revision 1.5

1.1       cgd         1: /*
                      2:  * Copyright (c) 1988 The Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * This code is derived from software written by Ken Arnold and
                      6:  * published in UNIX Review, Vol. 6, No. 8.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #if defined(LIBC_SCCS) && !defined(lint)
1.5     ! jtc        38: /*static char *sccsid = "from: @(#)popen.c     5.15 (Berkeley) 2/23/91";*/
        !            39: static char *rcsid = "$Id: popen.c,v 1.4 1993/08/23 21:56:31 jtc Exp $";
1.1       cgd        40: #endif /* LIBC_SCCS and not lint */
                     41:
                     42: #include <sys/param.h>
                     43: #include <sys/wait.h>
                     44: #include <errno.h>
                     45: #include <stdio.h>
                     46: #include <stdlib.h>
                     47: #include <string.h>
                     48: #include <unistd.h>
                     49: #include <paths.h>
                     50:
                     51: static pid_t *pids;
                     52:
                     53: FILE *
1.3       jtc        54: popen(command, type)
                     55:        const char *command;
1.1       cgd        56:        const char *type;
                     57: {
                     58:        FILE *iop;
                     59:        int pdes[2], fds, pid;
                     60:
1.3       jtc        61:        if (*type != 'r' && *type != 'w' || type[1]) {
                     62:                errno = EINVAL;
1.1       cgd        63:                return (NULL);
1.3       jtc        64:        }
1.1       cgd        65:
                     66:        if (pids == NULL) {
                     67:                if ((fds = getdtablesize()) <= 0)
                     68:                        return (NULL);
                     69:                if ((pids = (pid_t *)malloc((u_int)(fds * sizeof(int)))) == NULL)
                     70:                        return (NULL);
                     71:                bzero((char *)pids, fds * sizeof(pid_t));
                     72:        }
                     73:        if (pipe(pdes) < 0)
                     74:                return (NULL);
                     75:        switch (pid = vfork()) {
                     76:        case -1:                        /* error */
                     77:                (void) close(pdes[0]);
                     78:                (void) close(pdes[1]);
                     79:                return (NULL);
                     80:                /* NOTREACHED */
                     81:        case 0:                         /* child */
                     82:                if (*type == 'r') {
                     83:                        if (pdes[1] != STDOUT_FILENO) {
                     84:                                (void) dup2(pdes[1], STDOUT_FILENO);
                     85:                                (void) close(pdes[1]);
                     86:                        }
                     87:                        (void) close(pdes[0]);
                     88:                } else {
                     89:                        if (pdes[0] != STDIN_FILENO) {
                     90:                                (void) dup2(pdes[0], STDIN_FILENO);
                     91:                                (void) close(pdes[0]);
                     92:                        }
                     93:                        (void) close(pdes[1]);
                     94:                }
1.3       jtc        95:                execl(_PATH_BSHELL, "sh", "-c", command, (char *) 0);
1.1       cgd        96:                _exit(127);
                     97:                /* NOTREACHED */
                     98:        }
                     99:        /* parent; assume fdopen can't fail...  */
                    100:        if (*type == 'r') {
                    101:                iop = fdopen(pdes[0], type);
                    102:                (void) close(pdes[1]);
                    103:        } else {
                    104:                iop = fdopen(pdes[1], type);
                    105:                (void) close(pdes[0]);
                    106:        }
                    107:        pids[fileno(iop)] = pid;
                    108:        return (iop);
                    109: }
                    110:
                    111: int
                    112: pclose(iop)
                    113:        FILE *iop;
                    114: {
                    115:        register int fdes;
                    116:        int omask;
1.3       jtc       117:        int pstat;
1.1       cgd       118:        pid_t pid;
                    119:
                    120:        /*
                    121:         * pclose returns -1 if stream is not associated with a
                    122:         * `popened' command, if already `pclosed', or waitpid
                    123:         * returns an error.
                    124:         */
                    125:        if (pids == NULL || pids[fdes = fileno(iop)] == 0)
                    126:                return (-1);
                    127:        (void) fclose(iop);
                    128:        do {
1.3       jtc       129:                pid = waitpid(pids[fdes], &pstat, 0);
1.1       cgd       130:        } while (pid == -1 && errno == EINTR);
                    131:        pids[fdes] = 0;
1.3       jtc       132:        return (pid == -1 ? -1 : pstat);
1.1       cgd       133: }

CVSweb <webmaster@jp.NetBSD.org>