# HG changeset patch # User Rob Landley # Date 1226822226 21600 # Node ID fa0a95ae4303b7ddbe378a0e249d034d07a98713 # Parent dfbfbaeb69c9f2b6c3dfb981a1d4d23d24d97ce5 Second attempt at making netcat -l work properly. diff -r dfbfbaeb69c9 -r fa0a95ae4303 toys/netcat.c --- a/toys/netcat.c Sat Nov 15 05:17:23 2008 -0600 +++ b/toys/netcat.c Sun Nov 16 01:57:06 2008 -0600 @@ -147,26 +147,19 @@ printf("%d\n", SWAP_BE16(address.sin_port)); fflush(stdout); } - // Do we need to defer calling accept() to the new thread - // because -l has arguments and we want it to return immediately? - temp = 0; - if ((toys.optflags&FLAG_l) && toys.optc) temp++; + // Do we need to return immediately because -l has arguments? + + if ((toys.optflags&FLAG_l) && toys.optc) + if (fork()) goto cleanup; for (;;) { pid_t child = 0; // For -l, call accept from the _new_ thread. - if (temp != 1) { - pollfds[0].fd = accept(sockfd, (struct sockaddr *)&address, - &len); - if (pollfds[0].fd<0) perror_exit("accept"); - - if (temp==2) { - close(sockfd); - break; - } - } else temp++; + pollfds[0].fd = accept(sockfd, (struct sockaddr *)&address, + &len); + if (pollfds[0].fd<0) perror_exit("accept"); // Do we need a tty? @@ -176,7 +169,7 @@ // Do we need to fork and/or redirect for exec? else { - if (temp || (toys.optflags&FLAG_L)) child = fork(); + if (toys.optflags&FLAG_L) child = fork(); if (!child && toys.optc) { int fd = pollfds[0].fd; @@ -189,11 +182,7 @@ } if (child<0) error_msg("Fork failed\n"); - if (child<1) { - if (!temp) break; - continue; - } - if (temp) exit(0); + if (child<1) break; close(pollfds[0].fd); } }