static int do_select(int cfd, int to_sec) { fd_set fds[1]; int maxfd; struct timeval timeout; timeout.tv_sec = to_sec; timeout.tv_usec = 0; maxfd = cfd; //maxfd = max(lfd, xxx); while (!clientshutdown) { FD_ZERO(fds); FD_SET(cfd, fds); //FD_SET(xxx, fds); if (select(maxfd + 1, fds, NULL, NULL, &timeout) < 0) { if (errno == EINTR) { continue; } print_log("do_select: select error", LOG_LEVEL_IMPORTANT); return (-1); } if (FD_ISSET(cfd, fds)) console_read_char(); else tunnel_keep_alive(&timeout, to_sec); } return 0; }
int main(int argc, char **argv) { #ifdef NOTHREADS fd_set fds; int rv; #endif if (console_init(0, 1, cmds, sizeof (cmds) / sizeof (*cmds), exitcb, "test> ") < 0) { fprintf(stderr, "console_init failed\n"); exit(1); } #ifdef NOTHREADS FD_ZERO(&fds); FD_SET(0, &fds); while ((rv = select(1, &fds, NULL, NULL, NULL)) >= 0) { if (FD_ISSET(0, &fds)) { #ifdef USE_READLINE console_read_char(); #else console_read(); #endif /* USE_READLINE */ } else { printf("fd other than stdin ready\n"); } } #else for (;;) { sleep(20); } #endif exit(0); }
int console_read_line(char *buffer, int max_length) { int current_offset = 0; for (;;) { int c = console_read_char(); if (c == '\n') { // Newline console_putc(c); break; } else if (c == '\b') { // Backspace if (current_offset > 0) { current_offset--; console_putc(c); } } else if (c < 128) { if (current_offset < max_length) { buffer[current_offset++] = c; console_putc(c); } } } buffer[current_offset] = '\0'; return current_offset; }