Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/select.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <pty.h>
- #define BUFFSIZE 8
- int main()
- {
- fd_set rfds;
- fd_set wfds;
- fd_set efds;
- struct timeval tv;
- int retval;
- char rbuf[BUFFSIZE] = {0};
- int bytes = 0;
- const char *msg = "hello world\n";
- const unsigned msg_size = strlen(msg);
- int amaster;
- int aslave;
- char filename[512];
- int rc = openpty(&amaster, &aslave, filename, NULL, NULL);
- if (rc < 0) {
- perror("openpty()");
- exit(1);
- }
- printf("PTY: %s\n", filename);
- printf("Hit [Enter] when ready: ");
- getchar();
- int fd = amaster;
- while (1) {
- FD_ZERO(&rfds);
- FD_SET(fd, &rfds);
- FD_ZERO(&wfds);
- FD_SET(fd, &wfds);
- FD_ZERO(&efds);
- FD_SET(fd, &efds);
- tv.tv_sec = 1;
- tv.tv_usec = 0;
- retval = select(fd + 1, &rfds, &wfds, &efds, &tv);
- if (retval == -1) {
- perror("select()");
- }
- else if (retval) {
- if (FD_ISSET(fd, &efds)) {
- printf("EXCEPTIONAL STATE:");
- break;
- }
- if (FD_ISSET(fd, &rfds)) {
- printf("READ: Ready\n");
- bytes = read(fd, rbuf, BUFFSIZE);
- printf("READ %d bytes. %s", bytes, rbuf);
- memset(&rbuf, 0, BUFFSIZE);
- FD_CLR(fd, &rfds);
- }
- if (FD_ISSET(fd, &wfds)) {
- printf("WRITE: Ready.");
- bytes = write(fd, msg, msg_size);
- printf("WRITTEN %d bytes.", bytes);
- FD_CLR(fd, &wfds);
- }
- }
- else {
- printf("timeout()\n");
- /* bytes = write(fd, msg, msg_size); */
- /* break; */
- }
- }
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment