version 1.7, 1994/05/06 18:44:25 |
version 1.8, 1994/05/07 03:03:13 |
|
|
*/ |
*/ |
|
|
#if defined(LIBC_SCCS) && !defined(lint) |
#if defined(LIBC_SCCS) && !defined(lint) |
/* static char sccsid[] = "from: @(#)popen.c 8.1 (Berkeley) 6/4/93"; */ |
static char sccsid[] = "@(#)popen.c 8.1 (Berkeley) 6/4/93"; |
static char *rcsid = "$Id$"; |
|
#endif /* LIBC_SCCS and not lint */ |
#endif /* LIBC_SCCS and not lint */ |
|
|
#include <sys/param.h> |
#include <sys/param.h> |
Line 57 static struct pid { |
|
Line 56 static struct pid { |
|
} *pidlist; |
} *pidlist; |
|
|
FILE * |
FILE * |
popen(command, type) |
popen(program, type) |
const char *command; |
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; |
|
return (NULL); |
return (NULL); |
} |
|
|
|
if ((cur = malloc(sizeof(struct pid))) == NULL) |
if ((cur = malloc(sizeof(struct pid))) == NULL) |
return (NULL); |
return (NULL); |
Line 99 popen(command, type) |
|
Line 96 popen(command, type) |
|
} |
} |
(void)close(pdes[1]); |
(void)close(pdes[1]); |
} |
} |
execl(_PATH_BSHELL, "sh", "-c", command, (char *) 0); |
execl(_PATH_BSHELL, "sh", "-c", program, NULL); |
_exit(127); |
_exit(127); |
/* NOTREACHED */ |
/* NOTREACHED */ |
} |
} |
|
|
{ |
{ |
register struct pid *cur, *last; |
register struct pid *cur, *last; |
int omask; |
int omask; |
int pstat; |
union wait pstat; |
pid_t pid; |
pid_t pid; |
|
|
|
(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) |
|
|
if (cur == NULL) |
if (cur == NULL) |
return (-1); |
return (-1); |
|
|
(void)fclose(iop); |
/* Get the status of the process. */ |
|
omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); |
do { |
do { |
pid = waitpid(cur->pid, &pstat, 0); |
pid = waitpid(cur->pid, (int *) &pstat, 0); |
} while (pid == -1 && errno == EINTR); |
} while (pid == -1 && errno == EINTR); |
|
(void)sigsetmask(omask); |
|
|
/* Remove the entry from the linked list. */ |
/* Remove the entry from the linked list. */ |
if (last == NULL) |
if (last == NULL) |
|
|
last->next = cur->next; |
last->next = cur->next; |
free(cur); |
free(cur); |
|
|
return (pid == -1 ? -1 : pstat); |
return (pid == -1 ? -1 : pstat.w_status); |
} |
} |