-
Notifications
You must be signed in to change notification settings - Fork 3
/
pty.cpp
51 lines (42 loc) · 887 Bytes
/
pty.cpp
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
#include <string>
#include <cstring>
#include "pty.hpp"
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <pty.h>
#include <termios.h>
pty_t::pty_t () {
}
void pty_t::spawn() {
struct winsize winp;
winp.ws_col = 80;
winp.ws_row = 24;
winp.ws_xpixel = 0;
winp.ws_ypixel = 0;
pid_t pid = forkpty(&master_fd, nullptr, nullptr, &winp);
if (pid == -1) {
perror("forkpty");
return;
} else if (pid == 0) { // master
char path[1024];
char *params[] = {
path,
nullptr
};
getcwd(path, sizeof (path));
strcat(path, "/exec.sh");
execvp(path, params);
perror("execvp");
} else {
int flags = fcntl(master_fd, F_GETFL, 0);
fcntl(master_fd, F_SETFL, flags | O_NONBLOCK);
puts("pts created.");
ready = true;
}
}
pty_t::~pty_t() {
close(master_fd);
puts("pts closed.");
}