1 |
root |
1.2 |
// on my system, this program outputs: |
2 |
|
|
// |
3 |
|
|
// who; echo 'Hello, World!'; exit |
4 |
|
|
// # root pts/9 Jan 25 12:12 (libptytty.example.net) |
5 |
|
|
// Hello, World! |
6 |
|
|
// |
7 |
|
|
|
8 |
|
|
#include <stdio.h> |
9 |
|
|
#include <stdlib.h> |
10 |
|
|
|
11 |
|
|
#include <sys/types.h> |
12 |
|
|
#include <unistd.h> |
13 |
root |
1.1 |
|
14 |
|
|
#include <libptytty.h> |
15 |
|
|
|
16 |
root |
1.2 |
int main (void) |
17 |
|
|
{ |
18 |
|
|
ptytty_init (); |
19 |
|
|
|
20 |
|
|
PTYTTY pty = ptytty_create (); |
21 |
|
|
|
22 |
|
|
if (!ptytty_get (pty)) |
23 |
|
|
printf ("unable to open pty\n"), exit (EXIT_FAILURE); |
24 |
|
|
|
25 |
|
|
pid_t pid = fork (); |
26 |
|
|
|
27 |
|
|
if (pid < 0) |
28 |
|
|
printf ("fork error\n"), exit (EXIT_FAILURE); |
29 |
|
|
|
30 |
|
|
int pty_fd = ptytty_pty (pty); |
31 |
|
|
int tty_fd = ptytty_tty (pty); |
32 |
|
|
|
33 |
|
|
if (pid) |
34 |
|
|
{ |
35 |
|
|
ptytty_close_tty (pty); |
36 |
|
|
ptytty_login (pty, pid, 1, "libptytty.example.net"); |
37 |
|
|
|
38 |
|
|
char s[] = "who; echo 'Hello, World!'; exit\015"; |
39 |
|
|
|
40 |
|
|
write (pty_fd, s, sizeof (s) - 1); |
41 |
|
|
|
42 |
|
|
char buf[1024]; |
43 |
|
|
|
44 |
|
|
for (;;) |
45 |
|
|
{ |
46 |
|
|
int len = read (pty_fd, buf, 1024); |
47 |
|
|
|
48 |
|
|
if (len <= 0) |
49 |
|
|
break; |
50 |
|
|
|
51 |
|
|
write (STDOUT_FILENO, buf, len); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
ptytty_delete (pty); |
55 |
|
|
} |
56 |
|
|
else |
57 |
|
|
{ |
58 |
|
|
ptytty_make_controlling_tty (pty); |
59 |
|
|
|
60 |
|
|
close (pty_fd); |
61 |
|
|
dup2 (tty_fd, STDIN_FILENO ); |
62 |
|
|
dup2 (tty_fd, STDOUT_FILENO); |
63 |
|
|
dup2 (tty_fd, STDERR_FILENO); |
64 |
|
|
close (tty_fd); |
65 |
|
|
|
66 |
sf-exg |
1.4 |
execl ("/bin/sh", "-sh", (char *)0); |
67 |
root |
1.2 |
_exit (EXIT_FAILURE); |
68 |
|
|
} |
69 |
ayin |
1.3 |
|
70 |
root |
1.2 |
return EXIT_SUCCESS; |
71 |
|
|
} |
72 |
root |
1.1 |
|
73 |
|
|
|