More than 4k of input can be queued up in a pipe, so loop needs to continue reading...
authorRob Landley <rob@landley.net>
Wed, 19 Nov 2008 04:17:43 +0000 (22:17 -0600)
committerRob Landley <rob@landley.net>
Wed, 19 Nov 2008 04:17:43 +0000 (22:17 -0600)
toys/netcat.c

index 0ed2a36..82039a9 100644 (file)
@@ -93,6 +93,7 @@ void netcat_main(void)
        int sockfd=-1, pollcount=2;
        struct pollfd pollfds[2];
 
+       memset(pollfds, 0, 2*sizeof(struct pollfd));
        pollfds[0].events = pollfds[1].events = POLLIN;
        set_alarm(TT.wait);
 
@@ -108,8 +109,6 @@ void netcat_main(void)
                int temp;
                struct sockaddr_in address;
 
-               pollfds[1].fd = 0;
-
                // Setup socket
                sockfd = socket(AF_INET, SOCK_STREAM, 0);
                if (-1 == sockfd) perror_exit("socket");
@@ -212,8 +211,7 @@ void netcat_main(void)
                                int len = read(pollfds[i].fd, toybuf, sizeof(toybuf));
                                if (len<1) goto dohupnow;
                                xwrite(i ? pollfds[0].fd : 1, toybuf, len);
-                       }
-                       if (pollfds[i].revents & POLLHUP) {
+                       } else if (pollfds[i].revents & POLLHUP) {
 dohupnow:
                                // Close half-connection.  This is needed for things like
                                // "echo GET / | netcat landley.net 80"