ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/eg/c-sample.c
Revision: 1.6
Committed: Fri Jan 20 13:52:31 2012 UTC (12 years, 4 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
CVS Tags: rxvt-unicode-rel-9_26, rxvt-unicode-rel-9_25, rxvt-unicode-rel-9_22, rxvt-unicode-rel-9_20, rxvt-unicode-rel-9_21, rel-1_8, rxvt-unicode-rel-9_19, rxvt-unicode-rel-9_18, rxvt-unicode-rel-9_17, rxvt-unicode-rel-9_16, rel-1_7, rxvt-unicode-rel-9_15, rel-1_6, HEAD
Changes since 1.5: +0 -4 lines
Log Message:
Build (but not install) the sample C program.

File Contents

# Content
1 // 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
14 #include <libptytty.h>
15
16 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 execl ("/bin/sh", "-sh", (char *)0);
67 _exit (EXIT_FAILURE);
68 }
69
70 return EXIT_SUCCESS;
71 }
72
73