--- a/tcpscript.c Wed Jun 20 12:25:38 2007 +0200
+++ b/tcpscript.c Thu Aug 09 16:13:17 2007 +0200
@@ -73,6 +73,7 @@
void doinput(void);
void dooutput(void);
void doshell(void);
+void resize_clients(void);
char *shell;
int master;
@@ -97,6 +98,9 @@
MAXCONNS = 10
};
+int conns[MAXCONNS];
+int nconns = 0;
+
static char *progname;
static void
@@ -245,6 +249,38 @@
perror("Setsockopt REUSE failed"), fatal();
}
+void sig_resize_clients(int x)
+{
+ resize_clients();
+}
+
+void resize_clients()
+{
+ int rows, cols;
+ char tmp[100];
+ int i;
+ int len;
+
+ /* Get rows and cols from our connection to the terminal: fd 1 */
+ (void) ioctl(0, TIOCGWINSZ, (char *)&win);
+ rows = win.ws_row;
+ cols = win.ws_col;
+
+ /* Prepare the xterm resize string */
+ snprintf(tmp, 100, "\033[8;%i;%it\n", rows, cols, rows, cols);
+ len = strlen(tmp);
+ for (i = 0; i < nconns; ++i)
+ {
+ send(conns[i], tmp, len, 0);
+ }
+}
+
+void send_noecho(int fd)
+{
+ char seq[] = { 255 /*IAC*/, 251 /*WILL*/, 1 /*ECHO*/ };
+ send(fd, seq, sizeof seq, 0);
+}
+
int listen_tcp()
{
int s;
@@ -261,15 +297,15 @@
addr.sin_port = htons(tcp_port);
addr.sin_addr.s_addr = INADDR_ANY;
res = bind(s, (struct sockaddr *) &addr, sizeof(addr));
- if (s != 0)
+ if (res != 0)
perror("Failed bind()"), fatal();
res = listen(s, 1);
- if (s != 0)
+ if (res != 0)
perror("Failed listen()"), fatal();
if (!qflg)
- printf("Listening on port %i\n", tcp_port);
+ printf("Listening on port %i\r\n", tcp_port);
return s;
}
@@ -285,11 +321,9 @@
int cc;
char obuf[BUFSIZ];
int accept_socket;
- int conns[MAXCONNS];
- int nconns;
int i;
- (void) close(0);
+/* (void) close(0); lets use it in resize*/
#ifdef HAVE_openpty
(void) close(slave);
#endif
@@ -301,8 +335,8 @@
FD_ZERO(&should_read_set);
FD_SET(accept_socket, &should_read_set);
FD_SET(master, &should_read_set);
-
nconns = 0;
+ (void) signal(SIGWINCH, sig_resize_clients);
for (;;) {
int max;
read_set = should_read_set;
@@ -315,7 +349,9 @@
if (conns[i] > max)
max = conns[i];
- select(max + 1, &read_set, 0, 0, 0);
+ i = select(max + 1, &read_set, 0, 0, 0);
+ if (i == -1)
+ continue;
if (FD_ISSET(master, &read_set))
{
@@ -333,7 +369,7 @@
if (FD_ISSET(conns[i], &read_set))
{
int size;
- size = read(conns[i], obuf, sizeof(obuf));
+ size = recv(conns[i], obuf, sizeof(obuf), 0);
if (size == 0)
{
close(conns[i]);
@@ -357,6 +393,8 @@
FD_SET(s, &should_read_set);
conns[nconns] = s;
nconns++;
+ send_noecho(s);
+ resize_clients();
} else
close(s);
}