telnet: shrink

telnetd: shrink, and fix issue file printing
test: better and shorter usage text

function                                             old     new   delta
putiac2                                               51      50      -1
putiac                                                24      20      -4
handlenetoutput                                       95      91      -4
telnet_main                                         1480    1475      -5
iacflush                                              37      32      -5
make_new_session                                     436     421     -15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/6 up/down: 0/-34)             Total: -34 bytes
This commit is contained in:
Denis Vlasenko 2008-07-21 09:22:28 +00:00
parent 84c8daa11f
commit 1101d1c980
3 changed files with 24 additions and 27 deletions

View File

@ -4015,8 +4015,8 @@
#define telnet_full_usage "\n\n" \ #define telnet_full_usage "\n\n" \
"Connect to telnet server\n" \ "Connect to telnet server\n" \
"\nOptions:" \ "\nOptions:" \
"\n -a Attempt an automatic login with USER variable" \ "\n -a Automatic login with $USER variable" \
"\n -l USER Attempt an automatic login with USER argument" \ "\n -l USER Automatic login as USER" \
#else #else
#define telnet_trivial_usage \ #define telnet_trivial_usage \
@ -4047,8 +4047,8 @@
#define test_trivial_usage \ #define test_trivial_usage \
"EXPRESSION ]" "EXPRESSION ]"
#define test_full_usage "\n\n" \ #define test_full_usage "\n\n" \
"Check file types and compares values returning an exit code\n" \ "Check file types, compare values etc. Return a 0/1 exit code\n" \
"determined by the value of EXPRESSION" "depending on logical value of EXPRESSION"
#define test_example_usage \ #define test_example_usage \
"$ test 1 -eq 2\n" \ "$ test 1 -eq 2\n" \
"$ echo $?\n" \ "$ echo $?\n" \

View File

@ -52,9 +52,10 @@ enum {
typedef unsigned char byte; typedef unsigned char byte;
enum { netfd = 3 };
struct globals { struct globals {
int netfd; /* console fd:s are 0 and 1 (and 2) */ int iaclen; /* could even use byte, but it's a loss on x86 */
short iaclen; /* could even use byte */
byte telstate; /* telnet negotiation state from network input */ byte telstate; /* telnet negotiation state from network input */
byte telwish; /* DO, DONT, WILL, WONT */ byte telwish; /* DO, DONT, WILL, WONT */
byte charmode; byte charmode;
@ -95,7 +96,7 @@ static int subneg(byte c);
static void iacflush(void) static void iacflush(void)
{ {
write(G.netfd, G.iacbuf, G.iaclen); write(netfd, G.iacbuf, G.iaclen);
G.iaclen = 0; G.iaclen = 0;
} }
@ -191,7 +192,7 @@ static void handlenetoutput(int len)
outbuf[j++] = 0x00; outbuf[j++] = 0x00;
} }
if (j > 0) if (j > 0)
write(G.netfd, outbuf, j); write(netfd, outbuf, j);
} }
static void handlenetinput(int len) static void handlenetinput(int len)
@ -545,7 +546,7 @@ static void cookmode(void)
#define USE_POLL 1 #define USE_POLL 1
int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int telnet_main(int argc, char **argv) int telnet_main(int argc UNUSED_PARAM, char **argv)
{ {
char *host; char *host;
int port; int port;
@ -573,9 +574,6 @@ int telnet_main(int argc, char **argv)
cfmakeraw(&G.termios_raw); cfmakeraw(&G.termios_raw);
} }
if (argc < 2)
bb_show_usage();
#if ENABLE_FEATURE_TELNET_AUTOLOGIN #if ENABLE_FEATURE_TELNET_AUTOLOGIN
if (1 & getopt32(argv, "al:", &G.autologin)) if (1 & getopt32(argv, "al:", &G.autologin))
G.autologin = getenv("USER"); G.autologin = getenv("USER");
@ -590,20 +588,20 @@ int telnet_main(int argc, char **argv)
if (*argv) /* extra params?? */ if (*argv) /* extra params?? */
bb_show_usage(); bb_show_usage();
G.netfd = create_and_connect_stream_or_die(host, port); xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
signal(SIGINT, fgotsig); signal(SIGINT, fgotsig);
#ifdef USE_POLL #ifdef USE_POLL
ufds[0].fd = 0; ufds[1].fd = G.netfd; ufds[0].fd = 0; ufds[1].fd = netfd;
ufds[0].events = ufds[1].events = POLLIN; ufds[0].events = ufds[1].events = POLLIN;
#else #else
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds); FD_SET(STDIN_FILENO, &readfds);
FD_SET(G.netfd, &readfds); FD_SET(netfd, &readfds);
maxfd = G.netfd + 1; maxfd = netfd + 1;
#endif #endif
while (1) { while (1) {
@ -642,17 +640,17 @@ int telnet_main(int argc, char **argv)
#ifdef USE_POLL #ifdef USE_POLL
if (ufds[1].revents) /* well, should check POLLIN, but ... */ if (ufds[1].revents) /* well, should check POLLIN, but ... */
#else #else
if (FD_ISSET(G.netfd, &rfds)) if (FD_ISSET(netfd, &rfds))
#endif #endif
{ {
len = read(G.netfd, G.buf, DATABUFSIZE); len = read(netfd, G.buf, DATABUFSIZE);
if (len <= 0) { if (len <= 0) {
write_str(1, "Connection closed by foreign host\r\n"); write_str(1, "Connection closed by foreign host\r\n");
doexit(EXIT_FAILURE); doexit(EXIT_FAILURE);
} }
TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len)); TRACE(0, ("Read netfd (%d): %d\n", netfd, len));
handlenetinput(len); handlenetinput(len);
} }
} }
} } /* while (1) */
} }

View File

@ -229,11 +229,10 @@ make_new_session(
/* open the child's side of the tty. */ /* open the child's side of the tty. */
/* NB: setsid() disconnects from any previous ctty's. Therefore /* NB: setsid() disconnects from any previous ctty's. Therefore
* we must open child's side of the tty AFTER setsid! */ * we must open child's side of the tty AFTER setsid! */
fd = xopen(tty_name, O_RDWR); /* becomes our ctty */ close(0);
dup2(fd, 0); xopen(tty_name, O_RDWR); /* becomes our ctty */
dup2(fd, 1); xdup2(0, 1);
dup2(fd, 2); xdup2(0, 2);
while (fd > 2) close(fd--);
tcsetpgrp(0, getpid()); /* switch this tty's process group to us */ tcsetpgrp(0, getpid()); /* switch this tty's process group to us */
/* The pseudo-terminal allocated to the client is configured to operate in /* The pseudo-terminal allocated to the client is configured to operate in
@ -252,7 +251,7 @@ make_new_session(
* issue files, and they may block writing to fd 1, * issue files, and they may block writing to fd 1,
* (parent is supposed to read it, but parent waits * (parent is supposed to read it, but parent waits
* for vforked child to exec!) */ * for vforked child to exec!) */
print_login_issue(issuefile, NULL); print_login_issue(issuefile, tty_name);
/* Exec shell / login / whatever */ /* Exec shell / login / whatever */
login_argv[0] = loginpath; login_argv[0] = loginpath;