Applied patch from Gennady Feldman to use single-thread instead of forking.
[oweals/busybox.git] / telnet.c
index f276035270ba18542daff007c60a2334048f914e..758cce8b163edc2410201127500523446ef6bb56 100644 (file)
--- a/telnet.c
+++ b/telnet.c
@@ -30,8 +30,6 @@
  *
  */
 
-
-#include "internal.h"
 #include <termios.h>
 #include <unistd.h>
 #include <errno.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netdb.h>
+#include "busybox.h"
 
 #if 0
-#define DOTRACE 1
+static const int DOTRACE = 1;
 #endif
 
-#if DOTRACE
+#ifdef DOTRACE
 #include <arpa/inet.h> /* for inet_ntoa()... */
 #define TRACE(x, y) do { if (x) printf y; } while (0)
 #else
 #include <sys/time.h>
 #endif
 
-#define DATABUFSIZE 128
-#define IACBUFSIZE 128
+static const int DATABUFSIZE = 128;
+static const int IACBUFSIZE = 128;
 
-#define CHM_TRY 0
-#define CHM_ON 1
-#define CHM_OFF        2
+static const int CHM_TRY = 0;
+static const int CHM_ON = 1;
+static const int CHM_OFF = 2;
 
-#define UF_ECHO        0x01
-#define UF_SGA 0x02
+static const int UF_ECHO = 0x01;
+static const int UF_SGA = 0x02;
 
-#define TS_0   1
-#define TS_IAC 2
-#define TS_OPT 3
-#define TS_SUB1 4
-#define TS_SUB2        5
+enum {
+       TS_0 = 1,
+       TS_IAC = 2,
+       TS_OPT = 3,
+       TS_SUB1 = 4,
+       TS_SUB2 = 5,
+};
 
 #define WriteCS(fd, str) write(fd, str, sizeof str -1)
 
@@ -134,14 +135,6 @@ static int local_bind(int port);
 
 /* Some globals */
 static int one = 1;
-static const char telnet_usage[] =
-       "telnet host [port]\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
-       "\nTelnet is used to establish interactive communication with another\n"
-       "computer over a network using the TELNET protocol.\n"
-#endif
-       ;
-
 
 static void doexit(int ev)
 {
@@ -332,7 +325,7 @@ static void putiac1(byte c)
 
 /* ******************************* */
 
-char const escapecharis[] = "\r\nEscape character is ";
+static char const escapecharis[] = "\r\nEscape character is ";
 
 static void setConMode()
 {
@@ -340,7 +333,7 @@ static void setConMode()
        {
                if (G.charmode == CHM_TRY) {
                        G.charmode = CHM_ON;
-                       fprintf(stdout, "\r\nEntering character mode%s'^]'.\r\n", escapecharis);
+                       printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
                        rawmode();
                }
        }
@@ -348,7 +341,7 @@ static void setConMode()
        {
                if (G.charmode != CHM_OFF) {
                        G.charmode = CHM_OFF;
-                       fprintf(stdout, "\r\nEntering line mode%s'^C'.\r\n", escapecharis);
+                       printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
                        cookmode();
                }
        }
@@ -505,7 +498,7 @@ extern int telnet_main(int argc, char** argv)
 
        cfmakeraw(&G.termios_raw);
        
-       if (argc < 2)   usage(telnet_usage);
+       if (argc < 2)   show_usage();
        port = (argc > 2)? getport(argv[2]): 23;
        
        G.buf = xmalloc(DATABUFSIZE);
@@ -591,7 +584,7 @@ static int getport(char * p)
 
        if ((unsigned)(port - 1 ) > 65534)
        {
-               fatalError("%s: bad port number\n", p);
+               error_msg_and_die("%s: bad port number", p);
        }
        return port;
 }
@@ -603,7 +596,7 @@ static struct in_addr getserver(char * host)
        struct hostent * he;
        if ((he = gethostbyname(host)) == NULL)
        {
-               fatalError("%s: Unkonwn host\n", host);
+               error_msg_and_die("%s: Unknown host", host);
        }
        memcpy(&addr, he->h_addr, sizeof addr);
 
@@ -658,7 +651,7 @@ static int remote_connect(struct in_addr addr, int port)
 
        if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
        {
-               fatalError("Unable to connect to remote host: %s\n", strerror(errno));
+               perror_msg_and_die("Unable to connect to remote host");
        }
        return s;
 }