OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libc/] [posix/] [popen.c] - Diff between revs 40 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 40 Rev 1765
/*      $NetBSD: popen.c,v 1.11 1995/06/16 07:05:33 jtc Exp $   */
/*      $NetBSD: popen.c,v 1.11 1995/06/16 07:05:33 jtc Exp $   */
 
 
/*
/*
 * Copyright (c) 1988, 1993
 * Copyright (c) 1988, 1993
 *      The Regents of the University of California.  All rights reserved.
 *      The Regents of the University of California.  All rights reserved.
 *
 *
 * This code is derived from software written by Ken Arnold and
 * This code is derived from software written by Ken Arnold and
 * published in UNIX Review, Vol. 6, No. 8.
 * published in UNIX Review, Vol. 6, No. 8.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * modification, are permitted provided that the following conditions
 * are met:
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    must display the following acknowledgement:
 *      This product includes software developed by the University of
 *      This product includes software developed by the University of
 *      California, Berkeley and its contributors.
 *      California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *    without specific prior written permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * SUCH DAMAGE.
 */
 */
 
 
#if defined(LIBC_SCCS) && !defined(lint)
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
#if 0
static char sccsid[] = "@(#)popen.c     8.1 (Berkeley) 6/4/93";
static char sccsid[] = "@(#)popen.c     8.1 (Berkeley) 6/4/93";
#else
#else
static char rcsid[] = "$NetBSD: popen.c,v 1.11 1995/06/16 07:05:33 jtc Exp $";
static char rcsid[] = "$NetBSD: popen.c,v 1.11 1995/06/16 07:05:33 jtc Exp $";
#endif
#endif
#endif /* LIBC_SCCS and not lint */
#endif /* LIBC_SCCS and not lint */
 
 
#include <sys/param.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/wait.h>
 
 
#include <signal.h>
#include <signal.h>
#include <errno.h>
#include <errno.h>
#include <unistd.h>
#include <unistd.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <paths.h>
#include <paths.h>
 
 
static struct pid {
static struct pid {
        struct pid *next;
        struct pid *next;
        FILE *fp;
        FILE *fp;
        pid_t pid;
        pid_t pid;
} *pidlist;
} *pidlist;
 
 
FILE *
FILE *
popen(program, type)
popen(program, type)
        const char *program;
        const char *program;
        const char *type;
        const char *type;
{
{
        struct pid *cur;
        struct pid *cur;
        FILE *iop;
        FILE *iop;
        int pdes[2], pid;
        int pdes[2], pid;
 
 
        if (*type != 'r' && *type != 'w' || type[1]) {
        if (*type != 'r' && *type != 'w' || type[1]) {
                errno = EINVAL;
                errno = EINVAL;
                return (NULL);
                return (NULL);
        }
        }
 
 
        if ((cur = malloc(sizeof(struct pid))) == NULL)
        if ((cur = malloc(sizeof(struct pid))) == NULL)
                return (NULL);
                return (NULL);
 
 
        if (pipe(pdes) < 0) {
        if (pipe(pdes) < 0) {
                free(cur);
                free(cur);
                return (NULL);
                return (NULL);
        }
        }
 
 
        switch (pid = vfork()) {
        switch (pid = vfork()) {
        case -1:                        /* Error. */
        case -1:                        /* Error. */
                (void)close(pdes[0]);
                (void)close(pdes[0]);
                (void)close(pdes[1]);
                (void)close(pdes[1]);
                free(cur);
                free(cur);
                return (NULL);
                return (NULL);
                /* NOTREACHED */
                /* NOTREACHED */
        case 0:                          /* Child. */
        case 0:                          /* Child. */
                if (*type == 'r') {
                if (*type == 'r') {
                        if (pdes[1] != STDOUT_FILENO) {
                        if (pdes[1] != STDOUT_FILENO) {
                                (void)dup2(pdes[1], STDOUT_FILENO);
                                (void)dup2(pdes[1], STDOUT_FILENO);
                                (void)close(pdes[1]);
                                (void)close(pdes[1]);
                        }
                        }
                        (void) close(pdes[0]);
                        (void) close(pdes[0]);
                } else {
                } else {
                        if (pdes[0] != STDIN_FILENO) {
                        if (pdes[0] != STDIN_FILENO) {
                                (void)dup2(pdes[0], STDIN_FILENO);
                                (void)dup2(pdes[0], STDIN_FILENO);
                                (void)close(pdes[0]);
                                (void)close(pdes[0]);
                        }
                        }
                        (void)close(pdes[1]);
                        (void)close(pdes[1]);
                }
                }
                execl(_PATH_BSHELL, "sh", "-c", program, NULL);
                execl(_PATH_BSHELL, "sh", "-c", program, NULL);
#ifdef __CYGWIN32__
#ifdef __CYGWIN32__
                /* On cygwin32, we may not have /bin/sh.  In that
                /* On cygwin32, we may not have /bin/sh.  In that
                   case, try to find sh on PATH.  */
                   case, try to find sh on PATH.  */
                execlp("sh", "sh", "-c", program, NULL);
                execlp("sh", "sh", "-c", program, NULL);
#endif
#endif
                _exit(127);
                _exit(127);
                /* NOTREACHED */
                /* NOTREACHED */
        }
        }
 
 
        /* Parent; assume fdopen can't fail. */
        /* Parent; assume fdopen can't fail. */
        if (*type == 'r') {
        if (*type == 'r') {
                iop = fdopen(pdes[0], type);
                iop = fdopen(pdes[0], type);
                (void)close(pdes[1]);
                (void)close(pdes[1]);
        } else {
        } else {
                iop = fdopen(pdes[1], type);
                iop = fdopen(pdes[1], type);
                (void)close(pdes[0]);
                (void)close(pdes[0]);
        }
        }
 
 
        /* Link into list of file descriptors. */
        /* Link into list of file descriptors. */
        cur->fp = iop;
        cur->fp = iop;
        cur->pid =  pid;
        cur->pid =  pid;
        cur->next = pidlist;
        cur->next = pidlist;
        pidlist = cur;
        pidlist = cur;
 
 
        return (iop);
        return (iop);
}
}
 
 
/*
/*
 * pclose --
 * pclose --
 *      Pclose returns -1 if stream is not associated with a `popened' command,
 *      Pclose returns -1 if stream is not associated with a `popened' command,
 *      if already `pclosed', or waitpid returns an error.
 *      if already `pclosed', or waitpid returns an error.
 */
 */
int
int
pclose(iop)
pclose(iop)
        FILE *iop;
        FILE *iop;
{
{
        register struct pid *cur, *last;
        register struct pid *cur, *last;
        int pstat;
        int pstat;
        pid_t pid;
        pid_t pid;
 
 
        (void)fclose(iop);
        (void)fclose(iop);
 
 
        /* Find the appropriate file pointer. */
        /* Find the appropriate file pointer. */
        for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
        for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
                if (cur->fp == iop)
                if (cur->fp == iop)
                        break;
                        break;
        if (cur == NULL)
        if (cur == NULL)
                return (-1);
                return (-1);
 
 
        do {
        do {
                pid = waitpid(cur->pid, &pstat, 0);
                pid = waitpid(cur->pid, &pstat, 0);
        } while (pid == -1 && errno == EINTR);
        } while (pid == -1 && errno == EINTR);
 
 
        /* Remove the entry from the linked list. */
        /* Remove the entry from the linked list. */
        if (last == NULL)
        if (last == NULL)
                pidlist = cur->next;
                pidlist = cur->next;
        else
        else
                last->next = cur->next;
                last->next = cur->next;
        free(cur);
        free(cur);
 
 
        return (pid == -1 ? -1 : pstat);
        return (pid == -1 ? -1 : pstat);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.