Apply improved WinSock error handling to the few situations it still
[u/mdw/putty] / scp.c
diff --git a/scp.c b/scp.c
index b831fbb..77dd7da 100644 (file)
--- a/scp.c
+++ b/scp.c
@@ -53,13 +53,12 @@ static int statistics = 1;
 static int portnumber = 0;
 static char *password = NULL;
 static int errs = 0;
-static int connection_open = 0;
 /* GUI Adaptation - Sept 2000 */
 #define NAME_STR_MAX 2048
 static char statname[NAME_STR_MAX+1];
 static unsigned long statsize = 0;
 static int statperct = 0;
-static time_t statelapsed = 0;
+static unsigned long statelapsed = 0;
 static int gui_mode = 0;
 static char *gui_hwnd = NULL;
 
@@ -72,7 +71,8 @@ static void tell_str(FILE *stream, char *str);
 static void tell_user(FILE *stream, char *fmt, ...);
 static void send_char_msg(unsigned int msg_id, char c);
 static void send_str_msg(unsigned int msg_id, char *str);
-static void gui_update_stats(char *name, unsigned long size, int percentage, time_t elapsed);
+static void gui_update_stats(char *name, unsigned long size,
+                             int percentage, unsigned long elapsed);
 
 void begin_session(void) { }
 void logevent(char *string) { }
@@ -183,7 +183,7 @@ static void tell_user(FILE *stream, char *fmt, ...)
     tell_str(stream, str);
 }
 
-static void gui_update_stats(char *name, unsigned long size, int percentage, time_t elapsed)
+static void gui_update_stats(char *name, unsigned long size, int percentage, unsigned long elapsed)
 {
     unsigned int i;
 
@@ -299,8 +299,8 @@ void from_backend(int is_stderr, char *data, int datalen) {
     if (len > 0) {
         if (pendsize < pendlen + len) {
             pendsize = pendlen + len + 4096;
-            pending = (pending ? realloc(pending, pendsize) :
-                       malloc(pendsize));
+            pending = (pending ? srealloc(pending, pendsize) :
+                       smalloc(pendsize));
             if (!pending)
                 fatalbox("Out of memory");
         }
@@ -327,7 +327,7 @@ static int ssh_scp_recv(unsigned char *buf, int len) {
         pendlen -= pendused;
         if (pendlen == 0) {
             pendsize = 0;
-            free(pending);
+            sfree(pending);
             pending = NULL;
         }
         if (outlen == 0)
@@ -377,7 +377,7 @@ static void bump(char *fmt, ...)
     strcat(str, "\n");
     tell_str(stderr, str);
 
-    if (connection_open) {
+    if (back != NULL && back->socket() != NULL) {
        char ch;
        back->special(TS_EOF);
        ssh_scp_recv(&ch, 1);
@@ -476,8 +476,6 @@ static void do_cmd(char *host, char *user, char *cmd)
     ssh_scp_init();
     if (verbose && realhost != NULL)
        tell_user(stderr, "Connected to %s\n", realhost);
-
-    connection_open = 1;
 }
 
 /*
@@ -493,7 +491,8 @@ static void print_stats(char *name, unsigned long size, unsigned long done,
 
     /* GUI Adaptation - Sept 2000 */
     if (gui_mode)
-       gui_update_stats(name, size, ((done *100) / size), now-start);
+       gui_update_stats(name, size, (int)(100 * (done*1.0/size)),
+                         (unsigned long)difftime(now, start));
     else {
        if (now > start)
            ratebs = (float) done / (now - start);
@@ -829,12 +828,6 @@ static void sink(char *targ, char *src)
        if (sscanf(buf+1, "%u %lu %[^\n]", &mode, &size, namebuf) != 3)
            bump("Protocol error: Illegal file descriptor format");
        /* Security fix: ensure the file ends up where we asked for it. */
-       if (src) {
-           char *p = src + strlen(src);
-           while (p > src && p[-1] != '/' && p[-1] != '\\')
-               p--;
-           strcpy(namebuf, p);
-       }
        if (targisdir) {
            char t[2048];
            char *p;
@@ -1010,6 +1003,27 @@ static void toremote(int argc, char *argv[])
        do {
            char *last;
            char namebuf[2048];
+           /*
+            * Ensure that . and .. are never matched by wildcards,
+            * but only by deliberate action.
+            */
+           if (!strcmp(fdat.cFileName, ".") ||
+               !strcmp(fdat.cFileName, "..")) {
+               /*
+                * Find*File has returned a special dir. We require
+                * that _either_ `src' ends in a backslash followed
+                * by that string, _or_ `src' is precisely that
+                * string.
+                */
+               int len = strlen(src), dlen = strlen(fdat.cFileName);
+               if (len == dlen && !strcmp(src, fdat.cFileName)) {
+                   /* ok */;
+               } else if (len > dlen+1 && src[len-dlen-1] == '\\' &&
+                          !strcmp(src+len-dlen, fdat.cFileName)) {
+                   /* ok */;
+               } else
+                   continue;          /* ignore this one */
+           }
            if (strlen(src) + strlen(fdat.cFileName) >=
                sizeof(namebuf)) {
                tell_user(stderr, "%s: Name too long", src);
@@ -1216,6 +1230,7 @@ int main(int argc, char *argv[])
     }
     argc -= i;
     argv += i;
+    back = NULL;
 
     if (list) {
        if (argc != 1)
@@ -1235,7 +1250,7 @@ int main(int argc, char *argv[])
            tolocal(argc, argv);
     }
 
-    if (connection_open) {
+    if (back != NULL && back->socket() != NULL) {
        char ch;
        back->special(TS_EOF);
        ssh_scp_recv(&ch, 1);