popen.c\stdio\src - musl - musl - an implementation of the standard library for Linux-based systems

index : musl
musl - an implementation of the standard library for Linux-based systems
summary refs log tree commit diff
path: root/src/stdio/popen.c
blob: 1d33e9d692fae195b41af7ca8179e972c155c710 (plain) (blame)
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
#include "stdio_impl.h"
FILE *popen(const char *cmd, const char *mode)
{
	int p[2];
	int op;
	pid_t pid;
	FILE *f;
	const char *modes = "rw", *mi = strchr(modes, *mode);
	if (mi) {
		op = mi-modes;
	} else {
		errno = EINVAL;
		return 0;
	}
	
	if (pipe(p)) return NULL;
	f = fdopen(p[op], mode);
	if (!f) {
		close(p[0]);
		close(p[1]);
		return NULL;
	}
	
	pid = fork();
	switch (pid) {
	case -1:
		fclose(f);
		close(p[0]);
		close(p[1]);
		return NULL;
	case 0:
		dup2(p[1-op], 1-op);
		close(p[0]);
		close(p[1]);
		execl("/bin/sh", "sh", "-c", cmd, (char *)0);
		_exit(127);
	}
	close(p[1-op]);
	f->pipe_pid = pid;
	return f;
}
generated by cgit v1.2.1 (git 2.18.0) at 2025年09月10日 02:32:04 +0000

AltStyle によって変換されたページ (->オリジナル) /