-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimpletty.c
133 lines (95 loc) · 2.07 KB
/
simpletty.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <sys/ioctl.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <poll.h>
#include <termios.h>
#include <fcntl.h>
#include <limits.h>
#define __USE_XOPEN_EXTENDED
#include <stdlib.h>
#include <errno.h>
pid_t
_forkpty(int *master, char *name, struct termios *tio, struct winsize *ws)
{
int slave = -1;
char *path;
pid_t pid;
if ((*master = open("/dev/ptmx", O_RDWR|O_NOCTTY)) == -1)
return (-1);
if (grantpt(*master) != 0)
goto out;
if (unlockpt(*master) != 0)
goto out;
if ((path = ptsname(*master)) == NULL)
goto out;
if (name != NULL)
strncpy(name, path, TTY_NAME_MAX);
if ((slave = open(path, O_RDWR|O_NOCTTY)) == -1)
goto out;
switch (pid = fork()) {
case -1:
goto out;
case 0:
close(*master);
setsid();
if (tio != NULL && tcsetattr(slave, TCSAFLUSH, tio) == -1)
fprintf( stderr, "tcsetattr failed");
dup2(slave, 0);
dup2(slave, 1);
dup2(slave, 2);
if (slave > 2)
close(slave);
return (0);
}
close(slave);
return (pid);
out:
if (*master != -1)
close(*master);
if (slave != -1)
close(slave);
return (-1);
}
int main(int argc, char *argv[]) {
struct pollfd fds[2];
char buffer[1024];
int masterfd;
ssize_t bytes_read;
int i;
ssize_t written;
char shell[1024];
if(argc != 2) {
printf("Usage: %s <shell>\n", argv[0]);
return -1;
}
strncpy(shell, argv[1], 1024);
pid_t pid = _forkpty(&masterfd, NULL, NULL, NULL);
if(pid < 0) {
printf("Error forking pty\n");
return -1;
} else if(pid == 0){ //Child process
execlp(shell, shell, (char*)NULL);
} else { //Parent process
fds[0].fd = masterfd;
fds[0].events = POLLIN;
fds[1].fd = 0;
fds[1].events = POLLIN;
while(1) {
if(poll(fds, 2, 100)>0){
for(i=0;i<2;i++){
if(fds[i].revents &POLLIN){
if(fds[i].fd == masterfd) {
bytes_read = read(masterfd, buffer, 1024);
written = write(1, buffer, bytes_read);
} if(fds[i].fd == 0) {
bytes_read = read(0, buffer, 1024);
written = write(masterfd, buffer, bytes_read);
}
}
}
}
}
}
return 0;
}