-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinaterm.c
61 lines (55 loc) · 1.2 KB
/
inaterm.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
// Run a program inside a pseudoterminal. Used to work around crashes in
// 'yarn test' on Linux and crashes in 'bun test' on macOS when benchmarking
// with Hyperfine.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#if defined(__APPLE__)
#include <util.h>
#endif
#if defined(__linux__)
#include <pty.h>
#endif
int main(int argc, char** argv) {
int master_fd;
pid_t child_pid = forkpty(&master_fd, NULL, NULL, NULL);
if (child_pid == -1) {
perror("forkpty");
exit(1);
}
if (child_pid == 0) {
// child
execvp(argv[1], &argv[1]);
puts("execvp");
exit(1);
} else {
// parent
{
char buffer[1024*1024];
read_again:;
ssize_t rc = read(master_fd, buffer, sizeof(buffer));
if (rc == -1 && errno != EIO) {
perror("read");
exit(1);
}
if (rc > 0) {
(void)write(STDOUT_FILENO, buffer, rc);
goto read_again;
}
}
int status = 0;
pid_t rc = waitpid(child_pid, &status, 0);
if (rc == -1) {
perror("waitpid");
exit(1);
}
if (rc != child_pid) {
puts("???");
exit(2);
}
exit(status ? 1 : 0);
}
}