Merged heads
authorlbatlle@npdl268.bpo.hp.com
Thu, 09 Aug 2007 16:31:54 +0200
changeset 11 53b2466f1c2f
parent 10 c3156fd18d14 (diff)
parent 8 e5fc2f4d0c5c (current diff)
child 12 dc26a5985758
Merged heads
tcpscript.c
--- a/tcpscript.c	Wed Jul 04 21:16:57 2007 +0200
+++ b/tcpscript.c	Thu Aug 09 16:31:54 2007 +0200
@@ -73,6 +73,7 @@
 void doinput(void);
 void dooutput(void);
 void doshell(void);
+void resize_clients(void);
 
 char	*shell;
 int	master;
@@ -99,6 +100,9 @@
 static int accept_socket;
 const char version[] = "0.1";
 
+int conns[MAXCONNS];
+int nconns = 0;
+
 static char *progname;
 
 static void
@@ -162,7 +166,7 @@
 		case '?':
 		default:
 			fprintf(stderr,
-				"usage: script [-l maxconns] [-p port] [-q] [-t] \n");
+				"usage: tcpscript [-l nconns] [-p port] [-q] [-t] \n");
 			exit(1);
 		}
 	argc -= optind;
@@ -257,6 +261,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;
@@ -273,15 +309,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;
 }
 
@@ -296,11 +332,10 @@
 dooutput() {
 	int cc;
 	char obuf[BUFSIZ];
-    int conns[MAXCONNS];
-    int nconns;
+    int accept_socket;
     int i;
 
-	(void) close(0);
+/*	(void) close(0); lets use it in resize*/
 #ifdef HAVE_openpty
 	(void) close(slave);
 #endif
@@ -311,8 +346,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;
@@ -325,7 +360,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))
         {
@@ -343,7 +380,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]);
@@ -367,6 +404,8 @@
                 FD_SET(s, &should_read_set);
                 conns[nconns] = s;
                 nconns++;
+                send_noecho(s);
+                resize_clients();
             } else
                 close(s);
         }