summaryrefslogtreecommitdiff
path: root/libc/stdio/popen.c
blob: a07c411ebfb8072f26ece254f4400e1862961ffa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


FILE *popen(command, rw)
char *command;
char *rw;
{
	int pipe_fd[2];
	int pid, reading;

	if (pipe(pipe_fd) < 0)
		return NULL;
	reading = (rw[0] == 'r');

	pid = vfork();
	if (pid < 0) {
		close(pipe_fd[0]);
		close(pipe_fd[1]);
		return NULL;
	}
	if (pid == 0) {
		close(pipe_fd[!reading]);
		close(reading);
		if (pipe_fd[reading] != reading) {
			dup2(pipe_fd[reading], reading);
			close(pipe_fd[reading]);
		}

		execl("/bin/sh", "sh", "-c", command, (char *) 0);
		_exit(255);
	}

	close(pipe_fd[reading]);
	return fdopen(pipe_fd[!reading], rw);
}

int pclose(fd)
FILE *fd;
{
	int waitstat;

	if (fclose(fd) != 0)
		return EOF;
	wait(&waitstat);
	return waitstat;
}