| 90 | } |
| 91 | |
| 92 | int write_echoing_select(int fd, int fd_printf) |
| 93 | { |
| 94 | // run until fd has data ready |
| 95 | while(fd_printf >= 0) |
| 96 | { |
| 97 | fd_set rfds, wfds; |
| 98 | int retval; |
| 99 | while(true) |
| 100 | { |
| 101 | FD_ZERO(&rfds); |
| 102 | FD_ZERO(&wfds); |
| 103 | FD_SET(fd, &wfds); |
| 104 | FD_SET(fd_printf, &rfds); |
| 105 | |
| 106 | // select times out every second just so we can check for matlab ctrl+c. |
| 107 | struct timeval tv; |
| 108 | tv.tv_sec = 1; |
| 109 | tv.tv_usec = 0; |
| 110 | |
| 111 | try{check_for_matlab_ctrl_c();} catch(...) { return 1; } |
| 112 | retval = select(std::max(fd,fd_printf)+1, &rfds, &wfds, NULL, &tv); |
| 113 | try{check_for_matlab_ctrl_c();} catch(...) { return 1; } |
| 114 | if (retval == 0) // keep going if it was just a timeout. |
| 115 | continue; |
| 116 | else if (retval == -1 && errno == EINTR) |
| 117 | continue; |
| 118 | |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | if (retval == -1) |
| 123 | { |
| 124 | return 1; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | if (FD_ISSET(fd,&wfds)) |
| 129 | { |
| 130 | return 0; |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | char buf[1024]; |
| 135 | int num = read(fd_printf,buf, sizeof(buf)-1); |
| 136 | if (num == -1) |
| 137 | return 1; |
| 138 | if (num > 0) |
| 139 | { |
| 140 | buf[num] = 0; |
| 141 | cout << buf << flush; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | // ---------------------------------------------------------------------------------------- |
no test coverage detected