]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - scp.c
Silly mistake - restore-cursor-pos doesn't make sure the cursor
[PuTTY.git] / scp.c
1 /*
2  *  scp.c  -  Scp (Secure Copy) client for PuTTY.
3  *  Joris van Rantwijk, Simon Tatham
4  *
5  *  This is mainly based on ssh-1.2.26/scp.c by Timo Rinne & Tatu Ylonen.
6  *  They, in turn, used stuff from BSD rcp.
7  */
8
9 #include <windows.h>
10 #include <winsock.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <time.h>
15
16 #define PUTTY_DO_GLOBALS
17 #include "putty.h"
18 #include "scp.h"
19
20 #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \
21         ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000)
22 #define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \
23         ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600))
24
25 static int verbose = 0;
26 static int recursive = 0;
27 static int preserve = 0;
28 static int targetshouldbedirectory = 0;
29 static int statistics = 1;
30 static int portnumber = 0;
31 static char *password = NULL;
32 static int errs = 0;
33 static int connection_open = 0;
34
35 static void source(char *src);
36 static void rsource(char *src);
37 static void sink(char *targ);
38
39 /*
40  *  This function is needed to link with ssh.c, but it never gets called.
41  */
42 void term_out(void)
43 {
44     abort();
45 }
46
47 /*
48  *  Print an error message and perform a fatal exit.
49  */
50 void fatalbox(char *fmt, ...)
51 {
52     va_list ap;
53     va_start(ap, fmt);
54     fprintf(stderr, "Fatal: ");
55     vfprintf(stderr, fmt, ap);
56     fprintf(stderr, "\n");
57     va_end(ap);
58     exit(1);
59 }
60
61 /*
62  *  Print an error message and exit after closing the SSH link.
63  */
64 static void bump(char *fmt, ...)
65 {
66     va_list ap;
67     va_start(ap, fmt);
68     fprintf(stderr, "Fatal: ");
69     vfprintf(stderr, fmt, ap);
70     fprintf(stderr, "\n");
71     va_end(ap);
72     if (connection_open) {
73         char ch;
74         ssh_scp_send_eof();
75         ssh_scp_recv(&ch, 1);
76     }
77     exit(1);
78 }
79
80 static void get_password(const char *prompt, char *str, int maxlen)
81 {
82     HANDLE hin, hout;
83     DWORD savemode, i;
84
85     if (password) {
86         strncpy(str, password, maxlen);
87         str[maxlen-1] = '\0';
88         password = NULL;
89         return;
90     }
91
92     hin = GetStdHandle(STD_INPUT_HANDLE);
93     hout = GetStdHandle(STD_OUTPUT_HANDLE);
94     if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE)
95         bump("Cannot get standard input/output handles");
96
97     GetConsoleMode(hin, &savemode);
98     SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) |
99                    ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT);
100
101     WriteFile(hout, prompt, strlen(prompt), &i, NULL);
102     ReadFile(hin, str, maxlen-1, &i, NULL);
103
104     SetConsoleMode(hin, savemode);
105
106     if ((int)i > maxlen) i = maxlen-1; else i = i - 2;
107     str[i] = '\0';
108
109     WriteFile(hout, "\r\n", 2, &i, NULL);
110 }
111
112 /*
113  *  Open an SSH connection to user@host and execute cmd.
114  */
115 static void do_cmd(char *host, char *user, char *cmd)
116 {
117     char *err, *realhost;
118
119     if (host == NULL || host[0] == '\0')
120         bump("Empty host name");
121
122     /* Try to load settings for this host */
123     do_defaults(host);
124     if (cfg.host[0] == '\0') {
125         /* No settings for this host; use defaults */
126         strncpy(cfg.host, host, sizeof(cfg.host)-1);
127         cfg.host[sizeof(cfg.host)-1] = '\0';
128         cfg.port = 22;
129     }
130
131     /* Set username */
132     if (user != NULL && user[0] != '\0') {
133         strncpy(cfg.username, user, sizeof(cfg.username)-1);
134         cfg.username[sizeof(cfg.username)-1] = '\0';
135     } else if (cfg.username[0] == '\0') {
136         bump("Empty user name");
137     }
138
139     if (cfg.protocol != PROT_SSH)
140         cfg.port = 22;
141
142     if (portnumber)
143         cfg.port = portnumber;
144
145     err = ssh_scp_init(cfg.host, cfg.port, cmd, &realhost);
146     if (err != NULL)
147         bump("ssh_init: %s", err);
148     if (verbose && realhost != NULL)
149         fprintf(stderr, "Connected to %s\n", realhost);
150
151     connection_open = 1;
152 }
153
154 /*
155  *  Update statistic information about current file.
156  */
157 static void print_stats(char *name, unsigned long size, unsigned long done,
158                         time_t start, time_t now)
159 {
160     float ratebs;
161     unsigned long eta;
162     char etastr[10];
163     int pct;
164
165     if (now > start)
166         ratebs = (float) done / (now - start);
167     else
168         ratebs = (float) done;
169
170     if (ratebs < 1.0)
171         eta = size - done;
172     else
173         eta = (unsigned long) ((size - done) / ratebs);
174     sprintf(etastr, "%02ld:%02ld:%02ld",
175             eta / 3600, (eta % 3600) / 60, eta % 60);
176
177     pct = (int) (100.0 * (float) done / size);
178
179     printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%",
180            name, done / 1024, ratebs / 1024.0,
181            etastr, pct);
182
183     if (done == size)
184         printf("\n");
185 }
186
187 /*
188  *  Find a colon in str and return a pointer to the colon.
189  *  This is used to separate hostname from filename.
190  */
191 static char * colon(char *str)
192 {
193     /* We ignore a leading colon, since the hostname cannot be
194      empty. We also ignore a colon as second character because
195      of filenames like f:myfile.txt. */
196     if (str[0] == '\0' ||
197         str[0] == ':' ||
198         str[1] == ':')
199         return (NULL);
200     while (*str != '\0' &&
201            *str != ':' &&
202            *str != '/' &&
203            *str != '\\')
204         str++;
205     if (*str == ':')
206         return (str);
207     else
208         return (NULL);
209 }
210
211 /*
212  *  Wait for a response from the other side.
213  *  Return 0 if ok, -1 if error.
214  */
215 static int response(void)
216 {
217     char ch, resp, rbuf[2048];
218     int p;
219
220     if (ssh_scp_recv(&resp, 1) <= 0)
221         bump("Lost connection");
222
223     p = 0;
224     switch (resp) {
225       case 0:           /* ok */
226         return (0);
227       default:
228         rbuf[p++] = resp;
229         /* fallthrough */
230       case 1:           /* error */
231       case 2:           /* fatal error */
232         do {
233             if (ssh_scp_recv(&ch, 1) <= 0)
234                 bump("Protocol error: Lost connection");
235             rbuf[p++] = ch;
236         } while (p < sizeof(rbuf) && ch != '\n');
237         rbuf[p-1] = '\0';
238         if (resp == 1)
239             fprintf(stderr, "%s\n", rbuf);
240         else
241             bump("%s", rbuf);
242         errs++;
243         return (-1);
244     }
245 }
246
247 /*
248  *  Send an error message to the other side and to the screen.
249  *  Increment error counter.
250  */
251 static void run_err(const char *fmt, ...)
252 {
253     char str[2048];
254     va_list ap;
255     va_start(ap, fmt);
256     errs++;
257     strcpy(str, "\01scp: ");
258     vsprintf(str+strlen(str), fmt, ap);
259     strcat(str, "\n");
260     ssh_scp_send(str, strlen(str));
261     vfprintf(stderr, fmt, ap);
262     fprintf(stderr, "\n");
263     va_end(ap);
264 }
265
266 /*
267  *  Execute the source part of the SCP protocol.
268  */
269 static void source(char *src)
270 {
271     char buf[2048];
272     unsigned long size;
273     char *last;
274     HANDLE f;
275     DWORD attr;
276     unsigned long i;
277     unsigned long stat_bytes;
278     time_t stat_starttime, stat_lasttime;
279
280     attr = GetFileAttributes(src);
281     if (attr == (DWORD)-1) {
282         run_err("%s: No such file or directory", src);
283         return;
284     }
285
286     if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
287         if (recursive) {
288             /*
289              * Avoid . and .. directories.
290              */
291             char *p;
292             p = strrchr(src, '/');
293             if (!p)
294                 p = strrchr(src, '\\');
295             if (!p)
296                 p = src;
297             else
298                 p++;
299             if (!strcmp(p, ".") || !strcmp(p, ".."))
300                 /* skip . and .. */;
301             else
302                 rsource(src);
303         } else {
304             run_err("%s: not a regular file", src);
305         }
306         return;
307     }
308
309     if ((last = strrchr(src, '/')) == NULL)
310         last = src;
311     else
312         last++;
313     if (strrchr(last, '\\') != NULL)
314         last = strrchr(last, '\\') + 1;
315     if (last == src && strchr(src, ':') != NULL)
316         last = strchr(src, ':') + 1;
317
318     f = CreateFile(src, GENERIC_READ, FILE_SHARE_READ, NULL,
319                    OPEN_EXISTING, 0, 0);
320     if (f == INVALID_HANDLE_VALUE) {
321         run_err("%s: Cannot open file", src);
322         return;
323     }
324
325     if (preserve) {
326         FILETIME actime, wrtime;
327         unsigned long mtime, atime;
328         GetFileTime(f, NULL, &actime, &wrtime);
329         TIME_WIN_TO_POSIX(actime, atime);
330         TIME_WIN_TO_POSIX(wrtime, mtime);
331         sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime);
332         ssh_scp_send(buf, strlen(buf));
333         if (response())
334             return;
335     }
336
337     size = GetFileSize(f, NULL);
338     sprintf(buf, "C0644 %lu %s\n", size, last);
339     if (verbose)
340         fprintf(stderr, "Sending file modes: %s", buf);
341     ssh_scp_send(buf, strlen(buf));
342     if (response())
343         return;
344
345     if (statistics) {
346         stat_bytes = 0;
347         stat_starttime = time(NULL);
348         stat_lasttime = 0;
349     }
350
351     for (i = 0; i < size; i += 4096) {
352         char transbuf[4096];
353         DWORD j, k = 4096;
354         if (i + k > size) k = size - i;
355         if (! ReadFile(f, transbuf, k, &j, NULL) || j != k) {
356             if (statistics) printf("\n");
357             bump("%s: Read error", src);
358         }
359         ssh_scp_send(transbuf, k);
360         if (statistics) {
361             stat_bytes += k;
362             if (time(NULL) != stat_lasttime ||
363                 i + k == size) {
364                 stat_lasttime = time(NULL);
365                 print_stats(last, size, stat_bytes,
366                             stat_starttime, stat_lasttime);
367             }
368         }
369     }
370     CloseHandle(f);
371
372     ssh_scp_send("", 1);
373     (void) response();
374 }
375
376 /*
377  *  Recursively send the contents of a directory.
378  */
379 static void rsource(char *src)
380 {
381     char buf[2048];
382     char *last;
383     HANDLE dir;
384     WIN32_FIND_DATA fdat;
385     int ok;
386
387     if ((last = strrchr(src, '/')) == NULL)
388         last = src;
389     else
390         last++;
391     if (strrchr(last, '\\') != NULL)
392         last = strrchr(last, '\\') + 1;
393     if (last == src && strchr(src, ':') != NULL)
394         last = strchr(src, ':') + 1;
395
396     /* maybe send filetime */
397
398     sprintf(buf, "D0755 0 %s\n", last);
399     if (verbose)
400         fprintf(stderr, "Entering directory: %s", buf);
401     ssh_scp_send(buf, strlen(buf));
402     if (response())
403         return;
404
405     sprintf(buf, "%s/*", src);
406     dir = FindFirstFile(buf, &fdat);
407     ok = (dir != INVALID_HANDLE_VALUE);
408     while (ok) {
409         if (strcmp(fdat.cFileName, ".") == 0 ||
410             strcmp(fdat.cFileName, "..") == 0) {
411         } else if (strlen(src) + 1 + strlen(fdat.cFileName) >=
412                    sizeof(buf)) {
413             run_err("%s/%s: Name too long", src, fdat.cFileName);
414         } else {
415             sprintf(buf, "%s/%s", src, fdat.cFileName);
416             source(buf);
417         }
418         ok = FindNextFile(dir, &fdat);
419     }
420     FindClose(dir);
421
422     sprintf(buf, "E\n");
423     ssh_scp_send(buf, strlen(buf));
424     (void) response();
425 }
426
427 /*
428  *  Execute the sink part of the SCP protocol.
429  */
430 static void sink(char *targ)
431 {
432     char buf[2048];
433     char namebuf[2048];
434     char ch;
435     int targisdir = 0;
436     int settime;
437     int exists;
438     DWORD attr;
439     HANDLE f;
440     unsigned long mtime, atime;
441     unsigned int mode;
442     unsigned long size, i;
443     int wrerror = 0;
444     unsigned long stat_bytes;
445     time_t stat_starttime, stat_lasttime;
446     char *stat_name;
447
448     attr = GetFileAttributes(targ);
449     if (attr != (DWORD)-1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
450         targisdir = 1;
451
452     if (targetshouldbedirectory && !targisdir)
453         bump("%s: Not a directory", targ);
454
455     ssh_scp_send("", 1);
456     while (1) {
457         settime = 0;
458         gottime:
459         if (ssh_scp_recv(&ch, 1) <= 0)
460             return;
461         if (ch == '\n')
462             bump("Protocol error: Unexpected newline");
463         i = 0;
464         buf[i++] = ch;
465         do {
466             if (ssh_scp_recv(&ch, 1) <= 0)
467                 bump("Lost connection");
468             buf[i++] = ch;
469         } while (i < sizeof(buf) && ch != '\n');
470         buf[i-1] = '\0';
471         switch (buf[0]) {
472           case '\01':   /* error */
473             fprintf(stderr, "%s\n", buf+1);
474             errs++;
475             continue;
476           case '\02':   /* fatal error */
477             bump("%s", buf+1);
478           case 'E':
479             ssh_scp_send("", 1);
480             return;
481           case 'T':
482             if (sscanf(buf, "T%ld %*d %ld %*d",
483                        &mtime, &atime) == 2) {
484                 settime = 1;
485                 ssh_scp_send("", 1);
486                 goto gottime;
487             }
488             bump("Protocol error: Illegal time format");
489           case 'C':
490           case 'D':
491             break;
492           default:
493             bump("Protocol error: Expected control record");
494         }
495
496         if (sscanf(buf+1, "%u %lu %[^\n]", &mode, &size, namebuf) != 3)
497             bump("Protocol error: Illegal file descriptor format");
498         if (targisdir) {
499             char t[2048];
500             strcpy(t, targ);
501             if (targ[0] != '\0')
502                 strcat(t, "/");
503             strcat(t, namebuf);
504             strcpy(namebuf, t);
505         } else {
506             strcpy(namebuf, targ);
507         }
508         attr = GetFileAttributes(namebuf);
509         exists = (attr != (DWORD)-1);
510
511         if (buf[0] == 'D') {
512             if (exists && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
513                 run_err("%s: Not a directory", namebuf);
514                 continue;
515             }
516             if (!exists) {
517                 if (! CreateDirectory(namebuf, NULL)) {
518                     run_err("%s: Cannot create directory",
519                             namebuf);
520                     continue;
521                 }
522             }
523             sink(namebuf);
524             /* can we set the timestamp for directories ? */
525             continue;
526         }
527
528         f = CreateFile(namebuf, GENERIC_WRITE, 0, NULL,
529                        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
530         if (f == INVALID_HANDLE_VALUE) {
531             run_err("%s: Cannot create file", namebuf);
532             continue;
533         }
534
535         ssh_scp_send("", 1);
536
537         if (statistics) {
538             stat_bytes = 0;
539             stat_starttime = time(NULL);
540             stat_lasttime = 0;
541             if ((stat_name = strrchr(namebuf, '/')) == NULL)
542                 stat_name = namebuf;
543             else
544                 stat_name++;
545             if (strrchr(stat_name, '\\') != NULL)
546                 stat_name = strrchr(stat_name, '\\') + 1;
547         }
548
549         for (i = 0; i < size; i += 4096) {
550             char transbuf[4096];
551             DWORD j, k = 4096;
552             if (i + k > size) k = size - i;
553             if (ssh_scp_recv(transbuf, k) == 0)
554                 bump("Lost connection");
555             if (wrerror) continue;
556             if (! WriteFile(f, transbuf, k, &j, NULL) || j != k) {
557                 wrerror = 1;
558                 if (statistics)
559                     printf("\r%-25.25s | %50s\n",
560                            stat_name,
561                            "Write error.. waiting for end of file");
562                 continue;
563             }
564             if (statistics) {
565                 stat_bytes += k;
566                 if (time(NULL) > stat_lasttime ||
567                     i + k == size) {
568                     stat_lasttime = time(NULL);
569                     print_stats(stat_name, size, stat_bytes,
570                                 stat_starttime, stat_lasttime);
571                 }
572             }
573         }
574         (void) response();
575
576         if (settime) {
577             FILETIME actime, wrtime;
578             TIME_POSIX_TO_WIN(atime, actime);
579             TIME_POSIX_TO_WIN(mtime, wrtime);
580             SetFileTime(f, NULL, &actime, &wrtime);
581         }
582
583         CloseHandle(f);
584         if (wrerror) {
585             run_err("%s: Write error", namebuf);
586             continue;
587         }
588         ssh_scp_send("", 1);
589     }
590 }
591
592 /*
593  *  We will copy local files to a remote server.
594  */
595 static void toremote(int argc, char *argv[])
596 {
597     char *src, *targ, *host, *user;
598     char *cmd;
599     int i;
600
601     targ = argv[argc-1];
602
603     /* Separate host from filename */
604     host = targ;
605     targ = colon(targ);
606     if (targ == NULL)
607         bump("targ == NULL in toremote()");
608     *targ++ = '\0';
609     if (*targ == '\0')
610         targ = ".";
611     /* Substitute "." for emtpy target */
612
613     /* Separate host and username */
614     user = host;
615     host = strrchr(host, '@');
616     if (host == NULL) {
617         host = user;
618         user = NULL;
619     } else {
620         *host++ = '\0';
621         if (*user == '\0')
622             user = NULL;
623     }
624
625     if (argc == 2) {
626         /* Find out if the source filespec covers multiple files
627          if so, we should set the targetshouldbedirectory flag */
628         HANDLE fh;
629         WIN32_FIND_DATA fdat;
630         if (colon(argv[0]) != NULL)
631             bump("%s: Remote to remote not supported", argv[0]);
632         fh = FindFirstFile(argv[0], &fdat);
633         if (fh == INVALID_HANDLE_VALUE)
634             bump("%s: No such file or directory\n", argv[0]);
635         if (FindNextFile(fh, &fdat))
636             targetshouldbedirectory = 1;
637         FindClose(fh);
638     }
639
640     cmd = smalloc(strlen(targ) + 100);
641     sprintf(cmd, "scp%s%s%s%s -t %s",
642             verbose ? " -v" : "",
643             recursive ? " -r" : "",
644             preserve ? " -p" : "",
645             targetshouldbedirectory ? " -d" : "",
646             targ);
647     do_cmd(host, user, cmd);
648     sfree(cmd);
649
650     (void) response();
651
652     for (i = 0; i < argc - 1; i++) {
653         HANDLE dir;
654         WIN32_FIND_DATA fdat;
655         src = argv[i];
656         if (colon(src) != NULL) {
657             fprintf(stderr,
658                     "%s: Remote to remote not supported\n", src);
659             errs++;
660             continue;
661         }
662         dir = FindFirstFile(src, &fdat);
663         if (dir == INVALID_HANDLE_VALUE) {
664             run_err("%s: No such file or directory", src);
665             continue;
666         }
667         do {
668             char *last;
669             char namebuf[2048];
670             if (strlen(src) + strlen(fdat.cFileName) >=
671                 sizeof(namebuf)) {
672                 fprintf(stderr, "%s: Name too long", src);
673                 continue;
674             }
675             strcpy(namebuf, src);
676             if ((last = strrchr(namebuf, '/')) == NULL)
677                 last = namebuf;
678             else
679                 last++;
680             if (strrchr(last, '\\') != NULL)
681                 last = strrchr(last, '\\') + 1;
682             if (last == namebuf && strrchr(namebuf, ':') != NULL)
683                 last = strchr(namebuf, ':') + 1;
684             strcpy(last, fdat.cFileName);
685             source(namebuf);
686         } while (FindNextFile(dir, &fdat));
687         FindClose(dir);
688     }
689 }
690
691 /*
692  *  We will copy files from a remote server to the local machine.
693  */
694 static void tolocal(int argc, char *argv[])
695 {
696     char *src, *targ, *host, *user;
697     char *cmd;
698
699     if (argc != 2)
700         bump("More than one remote source not supported");
701
702     src = argv[0];
703     targ = argv[1];
704
705     /* Separate host from filename */
706     host = src;
707     src = colon(src);
708     if (src == NULL)
709         bump("Local to local copy not supported");
710     *src++ = '\0';
711     if (*src == '\0')
712         src = ".";
713     /* Substitute "." for empty filename */
714
715     /* Separate username and hostname */
716     user = host;
717     host = strrchr(host, '@');
718     if (host == NULL) {
719         host = user;
720         user = NULL;
721     } else {
722         *host++ = '\0';
723         if (*user == '\0')
724             user = NULL;
725     }
726
727     cmd = smalloc(strlen(src) + 100);
728     sprintf(cmd, "scp%s%s%s%s -f %s",
729             verbose ? " -v" : "",
730             recursive ? " -r" : "",
731             preserve ? " -p" : "",
732             targetshouldbedirectory ? " -d" : "",
733             src);
734     do_cmd(host, user, cmd);
735     sfree(cmd);
736
737     sink(targ);
738 }
739
740 /*
741  *  We will issue a list command to get a remote directory.
742  */
743 static void get_dir_list(int argc, char *argv[])
744 {
745     char *src, *host, *user;
746     char *cmd, *p, *q;
747     char c;
748
749     src = argv[0];
750
751     /* Separate host from filename */
752     host = src;
753     src = colon(src);
754     if (src == NULL)
755         bump("Local to local copy not supported");
756     *src++ = '\0';
757     if (*src == '\0')
758         src = ".";
759     /* Substitute "." for empty filename */
760
761     /* Separate username and hostname */
762     user = host;
763     host = strrchr(host, '@');
764     if (host == NULL) {
765         host = user;
766         user = NULL;
767     } else {
768         *host++ = '\0';
769         if (*user == '\0')
770             user = NULL;
771     }
772
773     cmd = smalloc(4*strlen(src) + 100);
774     strcpy(cmd, "ls -la '");
775     p = cmd + strlen(cmd);
776     for (q = src; *q; q++) {
777         if (*q == '\'') {
778             *p++ = '\''; *p++ = '\\'; *p++ = '\''; *p++ = '\'';
779         } else {
780             *p++ = *q;
781         }
782     }
783     *p++ = '\'';
784     *p = '\0';
785     
786     do_cmd(host, user, cmd);
787     sfree(cmd);
788
789     while (ssh_scp_recv(&c, 1) > 0)
790         fputc(c, stdout);              /* thank heavens for buffered I/O */
791 }
792
793 /*
794  *  Initialize the Win$ock driver.
795  */
796 static void init_winsock(void)
797 {
798     WORD winsock_ver;
799     WSADATA wsadata;
800
801     winsock_ver = MAKEWORD(1, 1);
802     if (WSAStartup(winsock_ver, &wsadata))
803         bump("Unable to initialise WinSock");
804     if (LOBYTE(wsadata.wVersion) != 1 ||
805         HIBYTE(wsadata.wVersion) != 1)
806         bump("WinSock version is incompatible with 1.1");
807 }
808
809 /*
810  *  Short description of parameters.
811  */
812 static void usage(void)
813 {
814     printf("PuTTY Secure Copy client\n");
815     printf("%s\n", ver);
816     printf("Usage: pscp [options] [user@]host:source target\n");
817     printf("       pscp [options] source [source...] [user@]host:target\n");
818     printf("       pscp [options] -ls user@host:filespec\n");
819     printf("Options:\n");
820     printf("  -p        preserve file attributes\n");
821     printf("  -q        quiet, don't show statistics\n");
822     printf("  -r        copy directories recursively\n");
823     printf("  -v        show verbose messages\n");
824     printf("  -P port   connect to specified port\n");
825     printf("  -pw passw login with specified password\n");
826     exit(1);
827 }
828
829 /*
830  *  Main program (no, really?)
831  */
832 int main(int argc, char *argv[])
833 {
834     int i;
835     int list = 0;
836
837     default_protocol = PROT_TELNET;
838
839     scp_flags = SCP_FLAG;
840     ssh_get_password = &get_password;
841     init_winsock();
842
843     for (i = 1; i < argc; i++) {
844         if (argv[i][0] != '-')
845             break;
846         if (strcmp(argv[i], "-v") == 0)
847             verbose = 1, scp_flags |= SCP_VERBOSE;
848         else if (strcmp(argv[i], "-r") == 0)
849             recursive = 1;
850         else if (strcmp(argv[i], "-p") == 0)
851             preserve = 1;
852         else if (strcmp(argv[i], "-q") == 0)
853             statistics = 0;
854         else if (strcmp(argv[i], "-h") == 0 ||
855                  strcmp(argv[i], "-?") == 0)
856             usage();
857         else if (strcmp(argv[i], "-P") == 0 && i+1 < argc)
858             portnumber = atoi(argv[++i]);
859         else if (strcmp(argv[i], "-pw") == 0 && i+1 < argc)
860             password = argv[++i];
861         else if (strcmp(argv[i], "-ls") == 0)
862             list = 1;
863         else if (strcmp(argv[i], "--") == 0)
864         { i++; break; }
865         else
866             usage();
867     }
868     argc -= i;
869     argv += i;
870
871     if (list) {
872         if (argc != 1)
873             usage();
874         get_dir_list(argc, argv);
875
876     } else {
877
878         if (argc < 2)
879             usage();
880         if (argc > 2)
881             targetshouldbedirectory = 1;
882
883         if (colon(argv[argc-1]) != NULL)
884             toremote(argc, argv);
885         else
886             tolocal(argc, argv);
887     }
888
889     if (connection_open) {
890         char ch;
891         ssh_scp_send_eof();
892         ssh_scp_recv(&ch, 1);
893     }
894     WSACleanup();
895     random_save_seed();
896
897     return (errs == 0 ? 0 : 1);
898 }
899
900 /* end */
901