-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpushfilter.c
More file actions
84 lines (73 loc) · 1.23 KB
/
pushfilter.c
File metadata and controls
84 lines (73 loc) · 1.23 KB
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
77
78
79
80
81
82
83
84
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
#include "pushfilter.h"
enum {
Maxarg = 1024,
};
pid_t
pushfilter(int fd, char *cmd, ...)
{
va_list ap;
pid_t ret;
va_start(ap, cmd);
ret = pushfilterv(fd, cmd, ap);
va_end(ap);
return ret;
}
pid_t
pushfilterv(int fd, char *cmd, va_list ap)
{
char *args[Maxarg];
size_t i;
args[0] = cmd;
for (i = 1; i < Maxarg; i++) {
args[i] = va_arg(ap, char*);
if (!args[i])
break;
}
/* overflowed list */
if (i == Maxarg) {
errno = E2BIG;
return -1;
}
return pushfilterl(fd, args);
}
pid_t
pushfilterl(int fd, char **cmd)
{
int outfd[2];
pid_t pid;
if (pipe(outfd) != 0)
return -1;
pid = fork();
/* error */
if (pid == -1){
return -1;
/* child */
} else if (pid == 0) {
/* input becomes child stdin */
if (dup2(fd, 0) == -1)
exit(1);
/* output becomes child stdout */
if (dup2(outfd[1], 1) == -1)
exit(1);
/* close unused fds */
close(outfd[0]);
close(outfd[1]);
/* don't double close */
if (fd != 0)
close(fd);
execvp(cmd[0], cmd);
/* if exec returns, we have an error */
exit(1);
/* parent */
} else {
if (dup2(outfd[0], fd) == -1)
return -1;
close(outfd[0]);
close(outfd[1]);
}
return pid;
}