Skip to content

Commit 7eec954

Browse files
committed
add keyboardhit() function
1 parent e9042a7 commit 7eec954

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

kirc.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include <fcntl.h>
1111
#include <poll.h>
1212
#include <errno.h>
13+
#include <termios.h>
1314
#include <sys/socket.h>
15+
#include <sys/ioctl.h>
1416

1517
#define MSG_MAX 512 /* guaranteed max message length */
1618
#define CHA_MAX 200 /* guaranteed max channel length */
@@ -258,6 +260,21 @@ handle_user_input(void) {
258260
}
259261
}
260262

263+
static int
264+
keyboardhit() {
265+
struct termios save, tp;
266+
int byteswaiting;
267+
268+
tcgetattr(STDIN_FILENO, &tp);
269+
save = tp;
270+
tp.c_lflag &= ~ICANON;
271+
tcsetattr(STDIN_FILENO, TCSANOW, &tp);
272+
ioctl(STDIN_FILENO, FIONREAD, &byteswaiting);
273+
tcsetattr(STDIN_FILENO, TCSANOW, &save);
274+
275+
return byteswaiting;
276+
}
277+
261278
int
262279
main(int argc, char **argv) {
263280

@@ -310,7 +327,7 @@ main(int argc, char **argv) {
310327
if (fds[0].revents & POLLIN) {
311328
handle_user_input();
312329
}
313-
if (fds[1].revents & POLLIN) {
330+
if (fds[1].revents & POLLIN && (keyboardhit() < 1)) {
314331
int rc = handle_server_message();
315332
if (rc != 0) {
316333
if (rc == -2) return EXIT_FAILURE;

0 commit comments

Comments
 (0)