ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/eg/c-sample.c
Revision: 1.3
Committed: Wed Jun 13 17:30:54 2007 UTC (19 years, 3 months ago) by ayin
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_3, rel-1_2
Changes since 1.2: +1 -1 lines
Log Message:
Remove trailing whitespace.

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     #include <fcntl.h>
18 root 1.1
19     #include <libptytty.h>
20    
21 root 1.2 int main (void)
22     {
23     ptytty_init ();
24    
25     PTYTTY pty = ptytty_create ();
26    
27     if (!ptytty_get (pty))
28     printf ("unable to open pty\n"), exit (EXIT_FAILURE);
29    
30     pid_t pid = fork ();
31    
32     if (pid < 0)
33     printf ("fork error\n"), exit (EXIT_FAILURE);
34    
35     int pty_fd = ptytty_pty (pty);
36     int tty_fd = ptytty_tty (pty);
37    
38     if (pid)
39     {
40     fcntl (pty_fd, F_SETFL, 0); // might be in non-blocking mode
41     ptytty_close_tty (pty);
42     ptytty_login (pty, pid, 1, "libptytty.example.net");
43    
44     char s[] = "who; echo 'Hello, World!'; exit\015";
45    
46     write (pty_fd, s, sizeof (s) - 1);
47    
48     char buf[1024];
49    
50     for (;;)
51     {
52     int len = read (pty_fd, buf, 1024);
53    
54     if (len <= 0)
55     break;
56    
57     write (STDOUT_FILENO, buf, len);
58     }
59    
60     ptytty_delete (pty);
61     }
62     else
63     {
64     ptytty_make_controlling_tty (pty);
65    
66     close (pty_fd);
67     dup2 (tty_fd, STDIN_FILENO );
68     dup2 (tty_fd, STDOUT_FILENO);
69     dup2 (tty_fd, STDERR_FILENO);
70     close (tty_fd);
71    
72     execl ("/bin/sh", "-sh", 0);
73     _exit (EXIT_FAILURE);
74     }
75 ayin 1.3
76 root 1.2 return EXIT_SUCCESS;
77     }
78 root 1.1
79