1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
25 #include <sys/types.h>
30 #define SH_PATH "/bin/sh" /* Shell to run. */
31 #define SH_NAME "sh" /* Name to give it. */
33 /* Structure describing a popen child. */
36 /* It is important that the first member of this structure be an `int' that
37 is the file descriptor. This is because the `fileno' function assumes
38 that __cookie(STREAM) points to the file descriptor. */
47 /* Open a new stream that is a one-way pipe to a
48 child process running the given shell command. */
50 DEFUN(popen, (command, mode), CONST char *command AND CONST char *mode)
57 if (command == NULL || mode == NULL || (*mode != 'r' && *mode != 'w'))
63 /* Create the pipe. */
64 if (pipe(pipedes) < 0)
67 /* Fork off the child. */
69 if (pid == (pid_t) -1)
71 /* The fork failed. */
72 (void) close(pipedes[0]);
73 (void) close(pipedes[1]);
76 else if (pid == (pid_t) 0)
78 /* We are the child side. Make the write side of
79 the pipe be stdin or the read side be stdout. */
81 CONST char *new_argv[4];
83 if ((*mode == 'r' ? dup2(pipedes[STDIN_FILENO], STDOUT_FILENO) :
84 dup2(pipedes[STDOUT_FILENO], STDIN_FILENO)) < 0)
87 /* Close the pipe descriptors. */
88 (void) close(pipedes[STDIN_FILENO]);
89 (void) close(pipedes[STDOUT_FILENO]);
92 new_argv[0] = SH_NAME;
94 new_argv[2] = command;
96 (void) execve(SH_PATH, (char *CONST *) new_argv, environ);
97 /* Die if it failed. */
101 /* We are the parent side. */
103 /* Close the irrelevant side of the pipe and open the relevant side as a
104 new stream. Mark our side of the pipe to close on exec, so new popen
105 children won't see it. */
108 (void) close(pipedes[STDOUT_FILENO]);
109 (void) fcntl (pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
110 stream = fdopen(pipedes[STDIN_FILENO], mode);
114 (void) close(pipedes[STDIN_FILENO]);
115 (void) fcntl (pipedes[STDOUT_FILENO], F_SETFD, FD_CLOEXEC);
116 stream = fdopen(pipedes[STDOUT_FILENO], mode);
122 child = (struct child *) malloc(sizeof(struct child));
125 child->fd = fileno(stream);
127 stream->__cookie = (PTR) child;
128 stream->__ispipe = 1;
133 /* The stream couldn't be opened or the child structure couldn't be
134 allocated. Kill the child and close the other side of the pipe. */
136 (void) kill(pid, SIGKILL);
138 (void) close(pipedes[*mode == 'r' ? STDOUT_FILENO : STDIN_FILENO]);
140 (void) fclose(stream);
141 (void) waitpid(pid, (int *) NULL, 0);
147 /* Close a stream opened by popen and return its status.
148 Returns -1 if the stream was not opened by popen. */
150 DEFUN(pclose, (stream), register FILE *stream)
155 if (!__validfp(stream) || !stream->__ispipe)
161 pid = ((struct child *) stream->__cookie)->pid;
162 free(stream->__cookie);
163 stream->__cookie = (PTR) &stream->__fileno;
164 stream->__ispipe = 0;
168 if (waitpid(pid, &status, 0) != pid)