From e7c442681a17dc0084ea3b5c66aaf070639bc6f6 Mon Sep 17 00:00:00 2001 From: Sean Middleditch Date: Sun, 15 Mar 2009 20:59:48 -0400 Subject: [PATCH] fix line ending in telnet-client --- telnet-client.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/telnet-client.c b/telnet-client.c index ba81f00..099a846 100644 --- a/telnet-client.c +++ b/telnet-client.c @@ -29,12 +29,34 @@ #include "libtelnet.h" static struct termios orig_tios; +static struct libtelnet_t telnet; static int do_echo; static void _cleanup(void) { tcsetattr(STDOUT_FILENO, TCSADRAIN, &orig_tios); } +static void _input(unsigned char *buffer, int size) { + static unsigned char crlf[] = { '\r', '\n' }; + int i; + + for (i = 0; i != size; ++i) { + /* if we got a CR or LF, replace with CRLF + * NOTE that usually you'd get a CR in UNIX, but in raw + * mode we get LF instead (not sure why) + */ + if (buffer[i] == '\r' || buffer[i] == '\n') { + if (do_echo) + write(STDOUT_FILENO, crlf, 2); + libtelnet_send_data(&telnet, crlf, 2); + } else { + if (do_echo) + write(STDOUT_FILENO, buffer + i, 1); + libtelnet_send_data(&telnet, buffer + i, 1); + } + } +} + static void _send(int sock, unsigned char *buffer, unsigned int size) { int rs; @@ -147,7 +169,6 @@ int main(int argc, char **argv) { int sock; struct sockaddr_in addr; struct pollfd pfd[2]; - struct libtelnet_t telnet; struct addrinfo *ai; struct addrinfo hints; struct termios tios; @@ -218,12 +239,7 @@ int main(int argc, char **argv) { /* read from stdin */ if (pfd[0].revents & POLLIN) { if ((rs = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) { - /* local echo */ - if (do_echo) - write(STDOUT_FILENO, buffer, rs); - - /* send over the wire */ - libtelnet_send_data(&telnet, buffer, rs); + _input(buffer, rs); } else if (rs == 0) { break; } else {