changeset 322:fa0a95ae4303

Second attempt at making netcat -l work properly.
author Rob Landley <rob@landley.net>
date Sun, 16 Nov 2008 01:57:06 -0600
parents dfbfbaeb69c9
children dd0b130edf6d
files toys/netcat.c
diffstat 1 files changed, 9 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- 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);
 			}
 		}