[ale] ptr example

John Wells jb at sourceillustrated.com
Fri Jan 31 16:01:17 EST 2003


Chris,

Thanks again.  On my way out the door but I'll look this over tonight.

John

cfowler said:
> In case it was not obvious, the reason for the parent was so that the
> ttyp0 could communicate via ptyp0 to the tty you are running the program
> on.
>
>
>
> [cfowler at cfowler tmp]$ tty
> /dev/pts/7
> [cfowler at cfowler tmp]$ ./pshell
> sh-2.05a$ tty
> /dev/ttyp0
> sh-2.05a$ exit
> exit
> [cfowler at cfowler tmp]$
>
>
> On Fri, 2003-01-31 at 15:48, cfowler wrote:
>> Compile with gcc -o pshell pshell.c
>>
>> If ttyp0 is free, this will show you how it works.
>>
>> One thing to remeber is that Linux now uses pty98 as the new standard.
>>  These are pseudos found in /dev/pts.  This code supports the old
>> names but it is still the same.
>>
>>
>>
>>
>> #include <fcntl.h>
>> #include <unistd.h>
>> #include <stdio.h>
>> #include <termios.h>
>> #include <sys/select.h>
>>
>> #define SHELL "/bin/sh"
>>
>>
>> int
>> main(int argc, char *argv[]) {
>>         int mfd, sfd;
>>         int pid;
>>         fd_set fds;
>>         struct termios old, new;
>>
>>
>>         if((mfd = open("/dev/ptyp0", O_RDWR)) == -1) {
>>                 perror("Open master");
>>                 exit(1);
>>         }
>>
>>         if((sfd = open("/dev/ttyp0", O_RDWR)) == -1) {
>>                 perror("Open slave");
>>                 exit(1);
>>         }
>>
>>         switch(pid = fork()) {
>>                 case -1:
>>                         perror("fork");
>>                         exit(1);
>>                 case 0: // Child
>>                         setsid();
>>                         close(0); close(1); close(2);
>>                         dup2(sfd, 0);
>>                         dup2(sfd, 1);
>>                         dup2(sfd, 2);
>>                         execl(SHELL, SHELL, 0);
>>                         perror("exec");
>>                         exit(1);
>>                 default: // parent
>>                         break;
>>
>>         }
>>
>>         close(sfd);
>>
>>         // Make or terminal raw!
>>         tcgetattr(1, &old);
>>         new = old;
>>         cfmakeraw(&new);
>>         tcsetattr(1, TCSANOW, &new);
>>
>>         while(1) {
>>                 char b;
>>
>>                 FD_ZERO(&fds);
>>                 FD_SET(mfd, &fds);
>>                 FD_SET(0, &fds);
>>
>>                 select(mfd + 1, &fds, NULL, NULL, NULL);
>>
>>                 if(FD_ISSET(mfd, &fds)) {
>>                         if(read(mfd, &b, 1) <=0) {
>>                                         tcsetattr(0, TCSANOW, &old);
>> exit(0);
>>                         }
>>                         write(0, &b, 1);
>>                 }
>>
>>                 if(FD_ISSET(0, &fds)) {
>>                         read(0, &b, 1);
>>                         write(mfd, &b, 1);
>>                 }
>>         }
>>
>>
>> }
>> /* vi: set ts=4 sw=4:*/
>>
>> _______________________________________________
>> Ale mailing list
>> Ale at ale.org
>> http://www.ale.org/mailman/listinfo/ale
>
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale



_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale






More information about the Ale mailing list