aboutsummaryrefslogtreecommitdiff
path: root/toys/netcat.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2008-11-16 01:57:06 -0600
committerRob Landley <rob@landley.net>2008-11-16 01:57:06 -0600
commit053b42c935707c8eba12ebb1b7f60dbc8c387b09 (patch)
tree4af3a3500b12744e492d615a1fe1bbcb9f5da4d3 /toys/netcat.c
parentae2e4b743aee024f61793cc66ecf2be2115da250 (diff)
downloadtoybox-053b42c935707c8eba12ebb1b7f60dbc8c387b09.tar.gz
Second attempt at making netcat -l work properly.
Diffstat (limited to 'toys/netcat.c')
-rw-r--r--toys/netcat.c29
1 files changed, 9 insertions, 20 deletions
diff --git a/toys/netcat.c b/toys/netcat.c
index 9e27669c..e1cfc07f 100644
--- a/toys/netcat.c
+++ b/toys/netcat.c
@@ -147,26 +147,19 @@ void netcat_main(void)
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 @@ void netcat_main(void)
// 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 @@ void netcat_main(void)
}
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);
}
}