diff --git a/MD_Getc.c b/MD_Getc.c index 05d1b33..9d26548 100644 --- a/MD_Getc.c +++ b/MD_Getc.c @@ -2,9 +2,9 @@ #include #if defined(_MSC_VER) || defined(WIN32) #include -#define STDIN_FILENO 1 #else #include +#include #include #endif #include @@ -19,23 +19,31 @@ extern time_t mdtimeremaining; char md_getc() { char c = 'x'; int ret; + int rs; int stage = 0; + struct timeval tv; + while (1) { + fd_set rfd; + FD_ZERO(&rfd); if (mdcontrol.socket == -1) { - ret = read(STDIN_FILENO, &c, 1); + FD_SET(STDIN_FILENO, &rfd); } else { - ret = recv(mdcontrol.socket, &c, 1, 0); + FD_SET(mdcontrol.socket, &rfd); } - if (ret == 0) { - md_exit(0); + + tv.tv_sec = 60; + tv.tv_usec = 0; + + if (mdcontrol.socket == -1) { + rs = select(STDIN_FILENO + 1, &rfd, NULL, NULL, &tv); + } else { + rs = select(mdcontrol.socket + 1, &rfd, NULL, NULL, &tv); } -#if defined(_MSC_VER) || defined(WIN32) - if (ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK) { -#else - if (ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { -#endif + + if (rs == 0) { if (mdtimeout <= time(NULL)) { md_printf("\r\nIdle timeout!\r\n"); md_exit(0); @@ -44,18 +52,13 @@ char md_getc() { md_printf("\r\nOut of time!\r\n"); md_exit(0); } -#if defined(_MSC_VER) - Sleep(1); -#else - usleep(100); -#endif - continue; - } - else if (ret < 0) { + } else if (rs == -1 && errno != EINTR) { md_exit(0); - } - - if (mdcontrol.socket != -1) { + } else if (mdcontrol.socket != -1 && FD_ISSET(mdcontrol.socket, &rfd)) { + ret = recv(mdcontrol.socket, &c, 1, 0); + if (ret <= 0) { + md_exit(0); + } if ((unsigned char)c == IAC && stage == 0) { stage = 1; continue; @@ -81,14 +84,21 @@ char md_getc() { } continue; } - } - mdtimeout = time(NULL) + 900; - return c; + mdtimeout = time(NULL) + 900; + return c; + } else if (mdcontrol.socket == -1 && FD_ISSET(STDIN_FILENO, &rfd)) { + ret = read(STDIN_FILENO, &c, 1); + if (ret <= 0) { + md_exit(0); + } + mdtimeout = time(NULL) + 900; + return c; + } } } -char md_get_answer(char *options) { +char md_get_answer(const char *options) { char c; c = md_getc(); while (strchr(options, c) == NULL) { diff --git a/MD_Printf.c b/MD_Printf.c index c1ced2f..44d9a9b 100644 --- a/MD_Printf.c +++ b/MD_Printf.c @@ -4,9 +4,6 @@ #if defined(_MSC_VER) || defined(WIN32) #include #include "MD_AnsiCons.h" - -#define STDOUT_FILENO 0 - #else #include #include diff --git a/MagiDoor.h b/MagiDoor.h index 071f506..edba239 100644 --- a/MagiDoor.h +++ b/MagiDoor.h @@ -4,7 +4,7 @@ #include #define MAGIDOOR_VERSION_MAJOR 1 -#define MAGIDOOR_VERSION_MINOR 2 +#define MAGIDOOR_VERSION_MINOR 3 #define TRUE 1 #define FALSE 0 @@ -34,5 +34,5 @@ extern int md_getstring(char* ptr, int maxlen, char minchar, char maxchar); extern void md_sendfile(const char* filename, int pause); extern void md_clr_scr(); extern void md_set_cursor(int y, int x); -extern char md_get_answer(char* options); +extern char md_get_answer(const char* options); #endif