projects
/
oweals
/
musl.git
/ blob
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
history
|
raw
|
HEAD
yet another ugly legacy syscall rename...
[oweals/musl.git]
/
src
/
stdio
/
popen.c
1
#include "stdio_impl.h"
2
3
FILE *popen(const char *cmd, const char *mode)
4
{
5
int p[2];
6
int op;
7
pid_t pid;
8
FILE *f;
9
const char *modes = "rw", *mi = strchr(modes, *mode);
10
11
if (mi) {
12
op = mi-modes;
13
} else {
14
errno = EINVAL;
15
return 0;
16
}
17
18
if (pipe(p)) return NULL;
19
f = fdopen(p[op], mode);
20
if (!f) {
21
close(p[0]);
22
close(p[1]);
23
return NULL;
24
}
25
26
pid = fork();
27
switch (pid) {
28
case -1:
29
fclose(f);
30
close(p[0]);
31
close(p[1]);
32
return NULL;
33
case 0:
34
dup2(p[1-op], 1-op);
35
close(p[0]);
36
close(p[1]);
37
execl("/bin/sh", "sh", "-c", cmd, (char *)0);
38
_exit(127);
39
}
40
close(p[1-op]);
41
f->pipe_pid = pid;
42
return f;
43
}