-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopen2.c
76 lines (61 loc) · 1.54 KB
/
popen2.c
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#define READ 0
#define WRITE 1
pid_t popen2(const char *command, int *infp, int *outfp, int *errfp)
{
int p_stdin[2], p_stdout[2], p_stderr[2];
pid_t pid;
if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0 || pipe(p_stderr) != 0)
return -1;
pid = fork();
if (pid < 0)
return pid;
else if (pid == 0)
{
dup2(p_stdin[READ], STDIN_FILENO);
dup2(p_stdout[WRITE], STDOUT_FILENO);
dup2(p_stderr[WRITE], STDERR_FILENO);
close(p_stdin[READ]);
close(p_stdin[WRITE]);
close(p_stdout[READ]);
close(p_stdout[WRITE]);
close(p_stderr[READ]);
close(p_stderr[WRITE]);
char* params[100];
memset(params, 0, sizeof(params));
params[0] = strtok(command, "|");
int ctr=1;
do {
params[ctr] = strtok(NULL, "|");
ctr++;
} while (params[ctr-1] != NULL);
execv("/usr/local/bin/ffmpeg", params);
exit(1);
}
close(p_stdin[READ]);
close(p_stdout[WRITE]);
if (infp == NULL)
close(p_stdin[WRITE]);
else
*infp = p_stdin[WRITE];
if (outfp == NULL)
close(p_stdout[READ]);
else
*outfp = p_stdout[READ];
if (errfp == NULL)
close(p_stderr[READ]);
else
*errfp = p_stderr[READ];
return pid;
}
int pclose2(pid_t pid) {
int internal_stat;
waitpid(pid, &internal_stat, 0);
return WEXITSTATUS(internal_stat);
}