nc: minor code shrink

function                                             old     new   delta
dolisten                                             731     737      +6
tmtravel                                              32      21     -11
catch                                                 75      64     -11
oprint                                               272     229     -43
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/3 up/down: 6/-65)             Total: -59 bytes
   text    data     bss     dec     hex filename
 676698    3340   13936  693974   a96d6 busybox_old
 676650    3340   13936  693926   a96a6 busybox_unstripped
This commit is contained in:
Denis Vlasenko 2007-07-13 12:37:31 +00:00
parent 8cc9cb8bc5
commit 828e2a95da

View File

@ -160,18 +160,23 @@ enum {
/* catch: no-brainer interrupt handler */ /* catch: no-brainer interrupt handler */
static void catch(int sig) static void catch(int sig)
{ {
errno = 0;
if (o_verbose > 1) /* normally we don't care */ if (o_verbose > 1) /* normally we don't care */
fprintf(stderr, SENT_N_RECV_M, wrote_net, wrote_out); fprintf(stderr, SENT_N_RECV_M, wrote_net, wrote_out);
fprintf(stderr, "punt!\n"); fprintf(stderr, "punt!\n");
exit(1); exit(1);
} }
/* timeout and other signal handling cruft */ /* unarm */
static void tmtravel(int sig) static void unarm(void)
{ {
signal(SIGALRM, SIG_IGN); signal(SIGALRM, SIG_IGN);
alarm(0); alarm(0);
}
/* timeout and other signal handling cruft */
static void tmtravel(int sig)
{
unarm();
longjmp(jbuf, 1); longjmp(jbuf, 1);
} }
@ -182,13 +187,6 @@ static void arm(unsigned secs)
alarm(secs); alarm(secs);
} }
/* unarm */
static void unarm(void)
{
signal(SIGALRM, SIG_IGN);
alarm(0);
}
/* findline: /* findline:
find the next newline in a buffer; return inclusive size of that "line", find the next newline in a buffer; return inclusive size of that "line",
or the entire buffer size, so the caller knows how much to then write(). or the entire buffer size, so the caller knows how much to then write().
@ -246,11 +244,11 @@ static int connect_w_timeout(int fd)
arm(o_wait); arm(o_wait);
if (setjmp(jbuf) == 0) { if (setjmp(jbuf) == 0) {
rr = connect(fd, &themaddr->sa, themaddr->len); rr = connect(fd, &themaddr->sa, themaddr->len);
unarm();
} else { /* setjmp: connect failed... */ } else { /* setjmp: connect failed... */
rr = -1; rr = -1;
errno = ETIMEDOUT; /* fake it */ errno = ETIMEDOUT; /* fake it */
} }
unarm();
return rr; return rr;
} }
@ -319,9 +317,9 @@ static void dolisten(void)
&remend.sa, &ouraddr->sa, ouraddr->len); &remend.sa, &ouraddr->sa, ouraddr->len);
if (rr < 0) if (rr < 0)
bb_perror_msg_and_die("recvfrom"); bb_perror_msg_and_die("recvfrom");
unarm();
} else } else
bb_error_msg_and_die("timeout"); bb_error_msg_and_die("timeout");
unarm();
/* Now we learned *to which IP* peer has connected, and we want to anchor /* Now we learned *to which IP* peer has connected, and we want to anchor
our socket on it, so that our outbound packets will have correct local IP. our socket on it, so that our outbound packets will have correct local IP.
Unfortunately, bind() on already bound socket will fail now (EINVAL): Unfortunately, bind() on already bound socket will fail now (EINVAL):
@ -350,10 +348,9 @@ create new one, and bind() it. TODO */
close(rr); close(rr);
goto again; goto again;
} }
unarm();
} else } else
bb_error_msg_and_die("timeout"); bb_error_msg_and_die("timeout");
unarm();
xmove_fd(rr, netfd); /* dump the old socket, here's our new one */ xmove_fd(rr, netfd); /* dump the old socket, here's our new one */
/* find out what address the connection was *to* on our end, in case we're /* find out what address the connection was *to* on our end, in case we're
doing a listen-on-any on a multihomed machine. This allows one to doing a listen-on-any on a multihomed machine. This allows one to
@ -462,68 +459,54 @@ int udptest(void);
what when. Adapted from dgaudet's original example -- but must be ripping what when. Adapted from dgaudet's original example -- but must be ripping
*fast*, since we don't want to be too disk-bound... */ *fast*, since we don't want to be too disk-bound... */
#if ENABLE_NC_EXTRA #if ENABLE_NC_EXTRA
static void oprint(int direction, unsigned char *p, int bc) static void oprint(int direction, unsigned char *p, unsigned bc)
{ {
int obc; /* current "global" offset */ unsigned obc; /* current "global" offset */
int soc; /* stage write count */ unsigned x;
unsigned char *op; /* out hexdump ptr */ unsigned char *op; /* out hexdump ptr */
unsigned char *a; /* out asc-dump ptr */ unsigned char *ap; /* out asc-dump ptr */
int x;
unsigned char stage[100]; unsigned char stage[100];
if (bc == 0) if (bc == 0)
return; return;
op = stage;
obc = wrote_net; /* use the globals! */ obc = wrote_net; /* use the globals! */
if (direction == '<') if (direction == '<')
obc = wrote_out; obc = wrote_out;
*op++ = direction; stage[0] = direction;
*op = ' '; stage[59] = '#'; /* preload separator */
stage[59] = '#'; /* preload separator */
stage[60] = ' '; stage[60] = ' ';
while (bc) { /* for chunk-o-data ... */ do { /* for chunk-o-data ... */
x = 16; x = 16;
soc = 78; /* len of whole formatted line */ if (bc < 16) {
if (bc < x) { /* memset(&stage[bc*3 + 11], ' ', 16*3 - bc*3); */
soc = soc - 16 + bc; /* fiddle for however much is left */ memset(&stage[11], ' ', 16*3);
x = (bc * 3) + 11; /* 2 digits + space per, after D & offset */ x = bc;
op = &stage[x]; }
x = 16 - bc; sprintf(&stage[1], " %8.8x ", obc); /* xxx: still slow? */
while (x) { bc -= x; /* fix current count */
*op++ = ' '; /* preload filler spaces */ obc += x; /* fix current offset */
*op++ = ' '; op = &stage[11]; /* where hex starts */
*op++ = ' '; ap = &stage[61]; /* where ascii starts */
x--;
}
x = bc; /* re-fix current linecount */
} /* if bc < x */
bc -= x; /* fix wrt current line size */ do { /* for line of dump, however long ... */
sprintf(&stage[2], "%8.8x ", obc); /* xxx: still slow? */
obc += x; /* fix current offset */
op = &stage[11]; /* where hex starts */
a = &stage[61]; /* where ascii starts */
while (x) { /* for line of dump, however long ... */
*op++ = 0x20 | bb_hexdigits_upcase[*p >> 4]; *op++ = 0x20 | bb_hexdigits_upcase[*p >> 4];
*op++ = 0x20 | bb_hexdigits_upcase[*p & 0x0f]; *op++ = 0x20 | bb_hexdigits_upcase[*p & 0x0f];
*op++ = ' '; *op++ = ' ';
if ((*p > 31) && (*p < 127)) if ((*p > 31) && (*p < 127))
*a = *p; /* printing */ *ap = *p; /* printing */
else else
*a = '.'; /* nonprinting, loose def */ *ap = '.'; /* nonprinting, loose def */
a++; ap++;
p++; p++;
x--; } while (--x);
} /* while x */ *ap++ = '\n'; /* finish the line */
*a = '\n'; /* finish the line */ xwrite(ofd, stage, ap - stage);
xwrite(ofd, stage, soc); } while (bc);
} /* while bc */
} }
#else #else
void oprint(int direction, unsigned char *p, int bc); void oprint(int direction, unsigned char *p, unsigned bc);
#endif #endif
/* readwrite: /* readwrite: