[ale] pty example

cfowler cfowler at outpostsentinel.com
Fri Jan 31 16:47:01 EST 2003


As usual I got ocmpulsive ont he subject and added more features.  This
is still very crude and hard codes the names but it now support 1 person
-> 2 pseudos.  And a command mode to quit or switch between the 2.  But
the differnce between this and screen is evident where as screen uses
buffers to switch the windows and does a good job of it.





#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






More information about the Ale mailing list