Block until fd is ready to read, while also echoing whatever is in fd_printf to cout.
| 33 | // Block until fd is ready to read, while also echoing whatever is in fd_printf to |
| 34 | // cout. |
| 35 | int read_echoing_select(int fd, int fd_printf) |
| 36 | { |
| 37 | // run until fd has data ready |
| 38 | while(fd_printf >= 0) |
| 39 | { |
| 40 | fd_set rfds; |
| 41 | int retval; |
| 42 | |
| 43 | while(true) |
| 44 | { |
| 45 | FD_ZERO(&rfds); |
| 46 | FD_SET(fd, &rfds); |
| 47 | FD_SET(fd_printf, &rfds); |
| 48 | |
| 49 | // select times out every second just so we can check for matlab ctrl+c. |
| 50 | struct timeval tv; |
| 51 | tv.tv_sec = 1; |
| 52 | tv.tv_usec = 0; |
| 53 | |
| 54 | try{check_for_matlab_ctrl_c();} catch(...) { return 1; } |
| 55 | retval = select(std::max(fd,fd_printf)+1, &rfds, NULL, NULL, &tv); |
| 56 | try{check_for_matlab_ctrl_c();} catch(...) { return 1; } |
| 57 | if (retval == 0) // keep going if it was just a timeout. |
| 58 | continue; |
| 59 | else if (retval == -1 && errno == EINTR) |
| 60 | continue; |
| 61 | |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | if (retval == -1) |
| 66 | { |
| 67 | return 1; |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | if (FD_ISSET(fd,&rfds)) |
| 72 | { |
| 73 | return 0; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | char buf[1024]; |
| 78 | int num = read(fd_printf,buf, sizeof(buf)-1); |
| 79 | if (num == -1) |
| 80 | return 1; |
| 81 | if (num > 0) |
| 82 | { |
| 83 | buf[num] = 0; |
| 84 | cout << buf << flush; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int write_echoing_select(int fd, int fd_printf) |
no test coverage detected