[ale] ptr example
cfowler
cfowler at outpostsentinel.com
Fri Jan 31 15:48:29 EST 2003
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
More information about the Ale
mailing list