ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/eg/c-sample.c
Revision: 1.5
Committed: Tue Dec 20 16:09:32 2011 UTC (14 years, 9 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-9_14, rel-1_5
Changes since 1.4: +0 -2 lines
Log Message:
Do not set the pty fd non-blocking in ptytty::get.

File Contents

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