Summary: Add the -q option. Contributor: Alessandro Rubini <rubini@prosa.it> Index: netcat-1.10/netcat.c =================================================================== --- netcat-1.10.orig/netcat.c 2008-06-19 16:47:27.000000000 -0400 +++ netcat-1.10/netcat.c 2008-06-19 16:47:27.000000000 -0400 @@ -176,6 +176,7 @@ USHORT o_verbose = 0; unsigned int o_wait = 0; USHORT o_zero = 0; +int o_quit = -1; /* 0 == quit-now; >0 == quit after o_quit seconds */ /* o_tn in optional section */ /* Debug macro: squirt whatever message and sleep a bit so we can see it go @@ -240,6 +241,14 @@ bail (""); } +/* quit : + handler for a "-q" timeout (exit 0 instead of 1) */ +void quit() +{ + close(netfd); + exit(0); +} + /* timeout and other signal handling cruft */ void tmtravel () { @@ -1265,6 +1274,18 @@ if (rr <= 0) { /* at end, or fukt, or ... */ FD_CLR (0, ding1); /* disable and close stdin */ close (0); + /* if the user asked to exit on EOF, do it */ + if (o_quit == 0) { + shutdown(netfd, 1); + close (fd); + exit (0); + } + /* if user asked to die after a while, arrange for it */ + if (o_quit > 0) { + shutdown(netfd, 1); + signal (SIGALRM, quit); + alarm(o_quit); + } } else { rzleft = rr; zp = bigbuf_in; @@ -1439,7 +1460,7 @@ /* If your shitbox doesn't have getopt, step into the nineties already. */ /* optarg, optind = next-argv-component [i.e. flag arg]; optopt = last-char */ - while ((x = getopt (argc, argv, "abe:g:G:hi:lno:p:rs:tuvw:z")) != EOF) { + while ((x = getopt (argc, argv, "abe:g:G:hi:lno:p:q:rs:tuvw:z")) != EOF) { /* Debug (("in go: x now %c, optarg %x optind %d", x, optarg, optind)) */ switch (x) { case 'a': @@ -1495,6 +1516,8 @@ break; case 'r': /* randomize various things */ o_random++; break; + case 'q': /* quit after stdin does EOF */ + o_quit = atoi(optarg); break; case 's': /* local source address */ /* do a full lookup [since everything else goes through the same mill], unless -n was previously specified. In fact, careful placement of -n can @@ -1712,6 +1735,7 @@ -o file hex dump of traffic\n\ -p port local port number\n\ -r randomize local and remote ports\n\ + -q secs quit after EOF on stdin and delay of secs\n\ -s addr local source address"); #ifdef TELNET holler ("\