Commit Diff


commit - ed972677f1082fa945479f3f3f877f97a3f43732
commit + 4070540ca2491cdbe09ad5ec39f602e34883f7aa
blob - d1618cdf6e9a6ccd70afe84a8df0f821c006c7ed
blob + efd320241a1163f5806321964b08e8f60b4737ed
--- icbirc.c
+++ icbirc.c
@@ -40,6 +40,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <netdb.h>
+#include <poll.h>
 #include <signal.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -72,6 +73,7 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
+	struct pollfd pfds[2], *pirc, *picb;
 	const char *addr_connect = NULL;
 	char *port, *room;
 	unsigned port_connect = 7326;
@@ -157,59 +159,59 @@ main(int argc, char *argv[])
 	irc_send_notice(1, "*** Connected");
 	icb_init();
 
+	memset(&pfds, 0, sizeof(pfds));
+	pirc = &pfds[0];
+	picb = &pfds[1];
+
+	pirc->fd = 0;
+	pirc->events = POLLIN;
+
+	picb->fd = server_fd;
+	picb->events = POLLIN;
+
 	for (;;) {
-		fd_set readfds;
-		struct timeval tv;
-		int r;
+		char buf[65535];
+		int len;
 
-		FD_ZERO(&readfds);
-		FD_SET(server_fd, &readfds);
-		FD_SET(0, &readfds);
-		memset(&tv, 0, sizeof(tv));
-                tv.tv_sec = 10;
-                r = select(server_fd + 1, &readfds, NULL, NULL, &tv);
-                if (r < 0) {
-			if (errno != EINTR)
-				err(1, "select");
-			continue;
+		if (poll(pfds, 2, 10) == -1) {
+			if (errno == EINTR)
+				continue;
+			err(1, "poll");
 		}
-		if (r > 0) {
-			char buf[65535];
-			int len;
 
-			if (FD_ISSET(server_fd, &readfds)) {
-				len = read(server_fd, buf, sizeof(buf));
-				if (len < 0) {
-					if (errno == EINTR)
-						continue;
-					warnx("read");
-					len = 0;
-				}
-				if (len == 0) {
-					warnx("connection closed by server");
-					irc_send_notice(1, "*** Connection "
-					    "closed by server");
-					break;
-				}
-				icb_recv(buf, len, 0, server_fd);
-				bytes_in += len;
+		if (picb->revents & (POLLIN|POLLNVAL|POLLHUP)) {
+			len = read(server_fd, buf, sizeof(buf));
+			if (len < 0) {
+				if (errno == EINTR)
+					continue;
+				warnx("read");
+				len = 0;
 			}
-			if (FD_ISSET(0, &readfds)) {
-				len = read(0, buf, sizeof(buf));
-				if (len < 0) {
-					if (errno == EINTR)
-						continue;
-					warnx("read");
-					len = 0;
-				}
-				if (len == 0) {
-					warnx("connection closed by client");
-					break;
-				}
-				irc_recv(buf, len, 1, server_fd);
-				bytes_out += len;
+			if (len == 0) {
+				warnx("connection closed by server");
+				irc_send_notice(1, "*** Connection "
+				    "closed by server");
+				break;
 			}
+			icb_recv(buf, len, 0, server_fd);
+			bytes_in += len;
 		}
+
+		if (pirc->revents & (POLLIN|POLLNVAL|POLLHUP)) {
+			len = read(0, buf, sizeof(buf));
+			if (len < 0) {
+				if (errno == EINTR)
+					continue;
+				warnx("read");
+				len = 0;
+			}
+			if (len == 0) {
+				warnx("connection closed by client");
+				break;
+			}
+			irc_recv(buf, len, 1, server_fd);
+			bytes_out += len;
+		}
 	}
 
 	icbirc_quit();