#include #include #include #include #include #include int main() { int master, slave; struct termios tt; int val; char buf[1024]; int len; if (openpty(&master, &slave, NULL, NULL, NULL) < 0) { perror("openpty()"); exit(EXIT_FAILURE); } write(STDOUT_FILENO, "----------\n", 11); /* Write two messages */ write(master, "01234\n", 6); write(master, "56789\n", 6); /* Read (Canonical Mode) */ len = read(slave, buf, sizeof(buf)); if (len>0) write(STDOUT_FILENO, buf, len); /* Rrset Canonical Mode */ tcgetattr(slave, &tt); tt.c_lflag &= ~ICANON; tcsetattr(slave, TCSANOW, &tt); write(STDOUT_FILENO, "----------\n", 11); write(master, "ABCDE\n", 6); /* Read (Non-Canonical Mode) */ len = read(slave, buf, sizeof(buf)); if (len>0) write(STDOUT_FILENO, buf, len); write(STDOUT_FILENO, "----------\n", 11); /* Write two messages */ write(master, "FGHIJ\n", 6); write(master, "KLMNO\n", 6); /* Set Canonical Mode */ tcgetattr(slave, &tt); tt.c_lflag |= ICANON; tcsetattr(slave, TCSANOW, &tt); /* Flush buffer */ tcflush(slave, TCIFLUSH); val = 1; ioctl(slave, FIONBIO, &val); /* Read */ len = read(slave, buf, sizeof(buf)); if (len>0) write(STDOUT_FILENO, buf, len); write(STDOUT_FILENO, "----------\n", 11); close(slave); close(master); return EXIT_SUCCESS; }