Render timing.c robust in the face of strangeness. The strangenesses
[u/mdw/putty] / unix / uxplink.c
index 6f17b5a..c026157 100644 (file)
@@ -13,6 +13,7 @@
 #include <termios.h>
 #include <pwd.h>
 #include <sys/ioctl.h>
+#include <sys/select.h>
 
 #define PUTTY_DO_GLOBALS              /* actually _define_ globals */
 #include "putty.h"
@@ -128,10 +129,15 @@ void ldisc_update(void *frontend, int echo, int edit)
     else
        mode.c_lflag &= ~ECHO;
 
-    if (edit)
+    if (edit) {
+       mode.c_iflag |= ICRNL;
        mode.c_lflag |= ISIG | ICANON;
-    else
+    } else {
+       mode.c_iflag &= ~ICRNL;
        mode.c_lflag &= ~(ISIG | ICANON);
+       mode.c_cc[VMIN] = 1;
+       mode.c_cc[VTIME] = 0;
+    }
 
     tcsetattr(0, TCSANOW, &mode);
 }
@@ -206,14 +212,14 @@ static void usage(void)
     printf("Usage: plink [options] [user@]host [command]\n");
     printf("       (\"host\" can also be a PuTTY saved session name)\n");
     printf("Options:\n");
-    printf("  -V        print version information\n");
+    printf("  -V        print version information and exit\n");
+    printf("  -pgpfp    print PGP key fingerprints and exit\n");
     printf("  -v        show verbose messages\n");
     printf("  -load sessname  Load settings from saved session\n");
     printf("  -ssh -telnet -rlogin -raw\n");
     printf("            force use of a particular protocol\n");
     printf("  -P port   connect to specified port\n");
     printf("  -l user   connect with specified username\n");
-    printf("  -m file   read remote command(s) from file\n");
     printf("  -batch    disable all interactive prompts\n");
     printf("The following options only apply to SSH connections:\n");
     printf("  -pw passw login with specified password\n");
@@ -227,8 +233,10 @@ static void usage(void)
     printf("  -A -a     enable / disable agent forwarding\n");
     printf("  -t -T     enable / disable pty allocation\n");
     printf("  -1 -2     force use of particular protocol version\n");
+    printf("  -4 -6     force use of IPv4 or IPv6\n");
     printf("  -C        enable compression\n");
     printf("  -i key    private key file for authentication\n");
+    printf("  -m file   read remote command(s) from file\n");
     printf("  -s        remote command is an SSH subsystem (SSH-2 only)\n");
     printf("  -N        don't start a shell/command (SSH-2 only)\n");
     exit(1);
@@ -311,6 +319,9 @@ int main(int argc, char **argv)
                use_subsystem = 1;
            } else if (!strcmp(p, "-V")) {
                 version();
+            } else if (!strcmp(p, "-pgpfp")) {
+                pgp_fingerprints();
+                exit(1);
            } else if (!strcmp(p, "-o")) {
                 if (argc <= 1) {
                     fprintf(stderr,
@@ -409,8 +420,6 @@ int main(int argc, char **argv)
                            cfg.port = default_port;
                        } else {
                            cfg = cfg2;
-                           /* Ick: patch up internal pointer after copy */
-                           cfg.remote_cmd_ptr = cfg.remote_cmd;
                        }
                    }
 
@@ -513,7 +522,7 @@ int main(int argc, char **argv)
        cfg.host[p1] = '\0';
     }
 
-    if (!*cfg.remote_cmd_ptr)
+    if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd)
        flags |= FLAG_INTERACTIVE;
 
     /*
@@ -661,8 +670,27 @@ int main(int argc, char **argv)
            ret = select(maxfd, &rset, &wset, &xset, ptv);
            if (ret == 0)
                now = next;
-           else
-               now = GETTICKCOUNT();
+           else {
+               long newnow = GETTICKCOUNT();
+               /*
+                * Check to see whether the system clock has
+                * changed massively during the select.
+                */
+               if (newnow - now < 0 || newnow - now > next - now) {
+                   /*
+                    * If so, look at the elapsed time in the
+                    * select and use it to compute a new
+                    * tickcount_offset.
+                    */
+                   long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
+                   /* So we'd like GETTICKCOUNT to have returned othernow,
+                    * but instead it return newnow. Hence ... */
+                   tickcount_offset += othernow - newnow;
+                   now = othernow;
+               } else {
+                   now = newnow;
+               }
+           }
        } while (ret < 0 && errno == EINTR);
 
        if (ret < 0) {