nc: support "-<other_opts>e PROG" form of -e option

function                                             old     new   delta
nc_main                                              975    1033     +58
doexec                                                31      45     +14
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 72/0)               Total: 72 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2012-03-19 01:17:36 +01:00
parent 7fe1e3f161
commit 5e896481eb

View File

@ -115,6 +115,7 @@ struct globals {
unsigned wrote_out; /* total stdout bytes */ unsigned wrote_out; /* total stdout bytes */
unsigned wrote_net; /* total net bytes */ unsigned wrote_net; /* total net bytes */
#endif #endif
char *proggie0saved;
/* ouraddr is never NULL and goes through three states as we progress: /* ouraddr is never NULL and goes through three states as we progress:
1 - local address before bind (IP/port possibly zero) 1 - local address before bind (IP/port possibly zero)
2 - local address after bind (port is nonzero) 2 - local address after bind (port is nonzero)
@ -127,7 +128,6 @@ struct globals {
jmp_buf jbuf; /* timer crud */ jmp_buf jbuf; /* timer crud */
/* will malloc up the following globals: */
fd_set ding1; /* for select loop */ fd_set ding1; /* for select loop */
fd_set ding2; fd_set ding2;
char bigbuf_in[BIGSIZ]; /* data buffers */ char bigbuf_in[BIGSIZ]; /* data buffers */
@ -159,17 +159,16 @@ struct globals {
/* Must match getopt32 call! */ /* Must match getopt32 call! */
enum { enum {
OPT_h = (1 << 0), OPT_n = (1 << 0),
OPT_n = (1 << 1), OPT_p = (1 << 1),
OPT_p = (1 << 2), OPT_s = (1 << 2),
OPT_s = (1 << 3), OPT_u = (1 << 3),
OPT_u = (1 << 4), OPT_v = (1 << 4),
OPT_v = (1 << 5), OPT_w = (1 << 5),
OPT_w = (1 << 6), OPT_l = (1 << 6) * ENABLE_NC_SERVER,
OPT_l = (1 << 7) * ENABLE_NC_SERVER, OPT_i = (1 << (6+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
OPT_i = (1 << (7+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA, OPT_o = (1 << (7+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
OPT_o = (1 << (8+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA, OPT_z = (1 << (8+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
OPT_z = (1 << (9+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
}; };
#define o_nflag (option_mask32 & OPT_n) #define o_nflag (option_mask32 & OPT_n)
@ -263,6 +262,8 @@ Debug("findline returning whole thing: %d", siz);
static int doexec(char **proggie) NORETURN; static int doexec(char **proggie) NORETURN;
static int doexec(char **proggie) static int doexec(char **proggie)
{ {
if (G.proggie0saved)
proggie[0] = G.proggie0saved;
xmove_fd(netfd, 0); xmove_fd(netfd, 0);
dup2(0, 1); dup2(0, 1);
/* dup2(0, 2); - do we *really* want this? NO! /* dup2(0, 2); - do we *really* want this? NO!
@ -726,7 +727,7 @@ int nc_main(int argc UNUSED_PARAM, char **argv)
{ {
char *str_p, *str_s; char *str_p, *str_s;
IF_NC_EXTRA(char *str_i, *str_o;) IF_NC_EXTRA(char *str_i, *str_o;)
char *themdotted = themdotted; /* gcc */ char *themdotted = themdotted; /* for compiler */
char **proggie; char **proggie;
int x; int x;
unsigned o_lport = 0; unsigned o_lport = 0;
@ -754,13 +755,27 @@ int nc_main(int argc UNUSED_PARAM, char **argv)
proggie++; proggie++;
goto e_found; goto e_found;
} }
/* -<other_opts>e PROG [ARGS] ? */
/* (aboriginal linux uses this form) */
if (proggie[0][0] == '-') {
char *optpos = *proggie + 1;
/* Skip all valid opts w/o params */
optpos = optpos + strspn(optpos, "nuv"IF_NC_SERVER("l")IF_NC_EXTRA("z"));
if (*optpos == 'e' && !optpos[1]) {
*optpos = '\0';
proggie++;
G.proggie0saved = *proggie;
*proggie = NULL; /* terminate argv for getopt32 */
goto e_found;
}
}
} }
proggie = NULL; proggie = NULL;
e_found: e_found:
// -g -G -t -r deleted, unimplemented -a deleted too // -g -G -t -r deleted, unimplemented -a deleted too
opt_complementary = "?2:vv:w+"; /* max 2 params; -v is a counter; -w N */ opt_complementary = "?2:vv:w+"; /* max 2 params; -v is a counter; -w N */
getopt32(argv, "hnp:s:uvw:" IF_NC_SERVER("l") getopt32(argv, "np:s:uvw:" IF_NC_SERVER("l")
IF_NC_EXTRA("i:o:z"), IF_NC_EXTRA("i:o:z"),
&str_p, &str_s, &o_wait &str_p, &str_s, &o_wait
IF_NC_EXTRA(, &str_i, &str_o), &o_verbose); IF_NC_EXTRA(, &str_i, &str_o), &o_verbose);