2 * scp.c - Scp (Secure Copy) client for PuTTY.
3 * Joris van Rantwijk, Simon Tatham
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.
8 * Adaptations to enable connecting a GUI by L. Gunnarsson - Sept 2000
23 /* GUI Adaptation - Sept 2000 */
27 #define PUTTY_DO_GLOBALS
32 #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \
33 ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000)
34 #define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \
35 ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600))
37 /* GUI Adaptation - Sept 2000 */
38 #define WM_APP_BASE 0x8000
39 #define WM_STD_OUT_CHAR ( WM_APP_BASE+400 )
40 #define WM_STD_ERR_CHAR ( WM_APP_BASE+401 )
41 #define WM_STATS_CHAR ( WM_APP_BASE+402 )
42 #define WM_STATS_SIZE ( WM_APP_BASE+403 )
43 #define WM_STATS_PERCENT ( WM_APP_BASE+404 )
44 #define WM_STATS_ELAPSED ( WM_APP_BASE+405 )
45 #define WM_RET_ERR_CNT ( WM_APP_BASE+406 )
46 #define WM_LS_RET_ERR_CNT ( WM_APP_BASE+407 )
48 static int verbose = 0;
49 static int recursive = 0;
50 static int preserve = 0;
51 static int targetshouldbedirectory = 0;
52 static int statistics = 1;
53 static int portnumber = 0;
54 static char *password = NULL;
56 /* GUI Adaptation - Sept 2000 */
57 #define NAME_STR_MAX 2048
58 static char statname[NAME_STR_MAX+1];
59 static unsigned long statsize = 0;
60 static int statperct = 0;
61 static time_t statelapsed = 0;
62 static int gui_mode = 0;
63 static char *gui_hwnd = NULL;
65 static void source(char *src);
66 static void rsource(char *src);
67 static void sink(char *targ, char *src);
68 /* GUI Adaptation - Sept 2000 */
69 static void tell_char(FILE *stream, char c);
70 static void tell_str(FILE *stream, char *str);
71 static void tell_user(FILE *stream, char *fmt, ...);
72 static void send_char_msg(unsigned int msg_id, char c);
73 static void send_str_msg(unsigned int msg_id, char *str);
74 static void gui_update_stats(char *name, unsigned long size, int percentage, time_t elapsed);
76 void begin_session(void) { }
77 void logevent(char *string) { }
79 void verify_ssh_host_key(char *host, int port, char *keytype,
80 char *keystr, char *fingerprint) {
83 static const char absentmsg[] =
84 "The server's host key is not cached in the registry. You\n"
85 "have no guarantee that the server is the computer you\n"
87 "The server's key fingerprint is:\n"
89 "If you trust this host, enter \"y\" to add the key to\n"
90 "PuTTY's cache and carry on connecting.\n"
91 "If you do not trust this host, enter \"n\" to abandon the\n"
93 "Continue connecting? (y/n) ";
95 static const char wrongmsg[] =
96 "WARNING - POTENTIAL SECURITY BREACH!\n"
97 "The server's host key does not match the one PuTTY has\n"
98 "cached in the registry. This means that either the\n"
99 "server administrator has changed the host key, or you\n"
100 "have actually connected to another computer pretending\n"
101 "to be the server.\n"
102 "The new key fingerprint is:\n"
104 "If you were expecting this change and trust the new key,\n"
105 "enter Yes to update PuTTY's cache and continue connecting.\n"
106 "If you want to carry on connecting but without updating\n"
107 "the cache, enter No.\n"
108 "If you want to abandon the connection completely, press\n"
109 "Return to cancel. Pressing Return is the ONLY guaranteed\n"
111 "Update cached key? (y/n, Return cancels connection) ";
113 static const char abandoned[] = "Connection abandoned.\n";
118 * Verify the key against the registry.
120 ret = verify_host_key(host, port, keytype, keystr);
122 if (ret == 0) /* success - key matched OK */
124 if (ret == 2) { /* key was different */
125 fprintf(stderr, wrongmsg, fingerprint);
126 if (fgets(line, sizeof(line), stdin) &&
127 line[0] != '\0' && line[0] != '\n') {
128 if (line[0] == 'y' || line[0] == 'Y')
129 store_host_key(host, port, keytype, keystr);
131 fprintf(stderr, abandoned);
135 if (ret == 1) { /* key was absent */
136 fprintf(stderr, absentmsg, fingerprint);
137 if (fgets(line, sizeof(line), stdin) &&
138 (line[0] == 'y' || line[0] == 'Y'))
139 store_host_key(host, port, keytype, keystr);
141 fprintf(stderr, abandoned);
147 /* GUI Adaptation - Sept 2000 */
148 static void send_msg(HWND h, UINT message, WPARAM wParam)
150 while (!PostMessage( h, message, wParam, 0))
154 static void tell_char(FILE *stream, char c)
160 unsigned int msg_id = WM_STD_OUT_CHAR;
161 if (stream == stderr) msg_id = WM_STD_ERR_CHAR;
162 send_msg( (HWND)atoi(gui_hwnd), msg_id, (WPARAM)c );
166 static void tell_str(FILE *stream, char *str)
170 for( i = 0; i < strlen(str); ++i )
171 tell_char(stream, str[i]);
174 static void tell_user(FILE *stream, char *fmt, ...)
176 char str[0x100]; /* Make the size big enough */
179 vsprintf(str, fmt, ap);
182 tell_str(stream, str);
185 static void gui_update_stats(char *name, unsigned long size, int percentage, time_t elapsed)
189 if (strcmp(name,statname) != 0)
191 for( i = 0; i < strlen(name); ++i )
192 send_msg( (HWND)atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM)name[i]);
193 send_msg( (HWND)atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM)'\n' );
194 strcpy(statname,name);
196 if (statsize != size)
198 send_msg( (HWND)atoi(gui_hwnd), WM_STATS_SIZE, (WPARAM)size );
201 if (statelapsed != elapsed)
203 send_msg( (HWND)atoi(gui_hwnd), WM_STATS_ELAPSED, (WPARAM)elapsed );
204 statelapsed = elapsed;
206 if (statperct != percentage)
208 send_msg( (HWND)atoi(gui_hwnd), WM_STATS_PERCENT, (WPARAM)percentage );
209 statperct = percentage;
214 * Print an error message and perform a fatal exit.
216 void fatalbox(char *fmt, ...)
218 char str[0x100]; /* Make the size big enough */
221 strcpy(str, "Fatal:");
222 vsprintf(str+strlen(str), fmt, ap);
225 tell_str(stderr, str);
229 void connection_fatal(char *fmt, ...)
231 char str[0x100]; /* Make the size big enough */
234 strcpy(str, "Fatal:");
235 vsprintf(str+strlen(str), fmt, ap);
238 tell_str(stderr, str);
244 * Be told what socket we're supposed to be using.
246 static SOCKET scp_ssh_socket;
247 char *do_select(SOCKET skt, int startup) {
249 scp_ssh_socket = skt;
251 scp_ssh_socket = INVALID_SOCKET;
254 extern int select_result(WPARAM, LPARAM);
257 * Receive a block of data from the SSH link. Block until all data
260 * To do this, we repeatedly call the SSH protocol module, with our
261 * own trap in from_backend() to catch the data that comes back. We
262 * do this until we have enough data.
265 static unsigned char *outptr; /* where to put the data */
266 static unsigned outlen; /* how much data required */
267 static unsigned char *pending = NULL; /* any spare data */
268 static unsigned pendlen=0, pendsize=0; /* length and phys. size of buffer */
269 void from_backend(int is_stderr, char *data, int datalen) {
270 unsigned char *p = (unsigned char *)data;
271 unsigned len = (unsigned)datalen;
274 * stderr data is just spouted to local stderr and otherwise
278 fwrite(data, 1, len, stderr);
285 * If this is before the real session begins, just return.
291 unsigned used = outlen;
292 if (used > len) used = len;
293 memcpy(outptr, p, used);
294 outptr += used; outlen -= used;
295 p += used; len -= used;
299 if (pendsize < pendlen + len) {
300 pendsize = pendlen + len + 4096;
301 pending = (pending ? realloc(pending, pendsize) :
304 fatalbox("Out of memory");
306 memcpy(pending+pendlen, p, len);
310 static int ssh_scp_recv(unsigned char *buf, int len) {
315 * See if the pending-input block contains some of what we
319 unsigned pendused = pendlen;
320 if (pendused > outlen)
322 memcpy(outptr, pending, pendused);
323 memmove(pending, pending+pendused, pendlen-pendused);
340 FD_SET(scp_ssh_socket, &readfds);
341 if (select(1, &readfds, NULL, NULL, NULL) < 0)
343 select_result((WPARAM)scp_ssh_socket, (LPARAM)FD_READ);
350 * Loop through the ssh connection and authentication process.
352 static void ssh_scp_init(void) {
353 if (scp_ssh_socket == INVALID_SOCKET)
355 while (!back->sendok()) {
358 FD_SET(scp_ssh_socket, &readfds);
359 if (select(1, &readfds, NULL, NULL, NULL) < 0)
361 select_result((WPARAM)scp_ssh_socket, (LPARAM)FD_READ);
366 * Print an error message and exit after closing the SSH link.
368 static void bump(char *fmt, ...)
370 char str[0x100]; /* Make the size big enough */
373 strcpy(str, "Fatal:");
374 vsprintf(str+strlen(str), fmt, ap);
377 tell_str(stderr, str);
379 if (back->socket() != NULL) {
381 back->special(TS_EOF);
382 ssh_scp_recv(&ch, 1);
387 static int get_password(const char *prompt, char *str, int maxlen)
393 static int tried_once = 0;
398 strncpy(str, password, maxlen);
399 str[maxlen-1] = '\0';
405 /* GUI Adaptation - Sept 2000 */
407 if (maxlen>0) str[0] = '\0';
409 hin = GetStdHandle(STD_INPUT_HANDLE);
410 hout = GetStdHandle(STD_OUTPUT_HANDLE);
411 if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE)
412 bump("Cannot get standard input/output handles");
414 GetConsoleMode(hin, &savemode);
415 SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) |
416 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT);
418 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
419 ReadFile(hin, str, maxlen-1, &i, NULL);
421 SetConsoleMode(hin, savemode);
423 if ((int)i > maxlen) i = maxlen-1; else i = i - 2;
426 WriteFile(hout, "\r\n", 2, &i, NULL);
433 * Open an SSH connection to user@host and execute cmd.
435 static void do_cmd(char *host, char *user, char *cmd)
437 char *err, *realhost;
439 if (host == NULL || host[0] == '\0')
440 bump("Empty host name");
442 /* Try to load settings for this host */
443 do_defaults(host, &cfg);
444 if (cfg.host[0] == '\0') {
445 /* No settings for this host; use defaults */
446 do_defaults(NULL, &cfg);
447 strncpy(cfg.host, host, sizeof(cfg.host)-1);
448 cfg.host[sizeof(cfg.host)-1] = '\0';
453 if (user != NULL && user[0] != '\0') {
454 strncpy(cfg.username, user, sizeof(cfg.username)-1);
455 cfg.username[sizeof(cfg.username)-1] = '\0';
456 } else if (cfg.username[0] == '\0') {
457 bump("Empty user name");
460 if (cfg.protocol != PROT_SSH)
464 cfg.port = portnumber;
466 strncpy(cfg.remote_cmd, cmd, sizeof(cfg.remote_cmd));
467 cfg.remote_cmd[sizeof(cfg.remote_cmd)-1] = '\0';
472 err = back->init(cfg.host, cfg.port, &realhost);
474 bump("ssh_init: %s", err);
476 if (verbose && realhost != NULL)
477 tell_user(stderr, "Connected to %s\n", realhost);
481 * Update statistic information about current file.
483 static void print_stats(char *name, unsigned long size, unsigned long done,
484 time_t start, time_t now)
491 /* GUI Adaptation - Sept 2000 */
493 gui_update_stats(name, size, ((done *100) / size), now-start);
496 ratebs = (float) done / (now - start);
498 ratebs = (float) done;
503 eta = (unsigned long) ((size - done) / ratebs);
504 sprintf(etastr, "%02ld:%02ld:%02ld",
505 eta / 3600, (eta % 3600) / 60, eta % 60);
507 pct = (int) (100.0 * (float) done / size);
509 printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%",
510 name, done / 1024, ratebs / 1024.0,
519 * Find a colon in str and return a pointer to the colon.
520 * This is used to separate hostname from filename.
522 static char * colon(char *str)
524 /* We ignore a leading colon, since the hostname cannot be
525 empty. We also ignore a colon as second character because
526 of filenames like f:myfile.txt. */
527 if (str[0] == '\0' ||
531 while (*str != '\0' &&
543 * Wait for a response from the other side.
544 * Return 0 if ok, -1 if error.
546 static int response(void)
548 char ch, resp, rbuf[2048];
551 if (ssh_scp_recv(&resp, 1) <= 0)
552 bump("Lost connection");
562 case 2: /* fatal error */
564 if (ssh_scp_recv(&ch, 1) <= 0)
565 bump("Protocol error: Lost connection");
567 } while (p < sizeof(rbuf) && ch != '\n');
570 tell_user(stderr, "%s\n", rbuf);
579 * Send an error message to the other side and to the screen.
580 * Increment error counter.
582 static void run_err(const char *fmt, ...)
588 strcpy(str, "scp: ");
589 vsprintf(str+strlen(str), fmt, ap);
591 back->send(str, strlen(str));
592 tell_user(stderr, "%s",str);
597 * Execute the source part of the SCP protocol.
599 static void source(char *src)
607 unsigned long stat_bytes;
608 time_t stat_starttime, stat_lasttime;
610 attr = GetFileAttributes(src);
611 if (attr == (DWORD)-1) {
612 run_err("%s: No such file or directory", src);
616 if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
619 * Avoid . and .. directories.
622 p = strrchr(src, '/');
624 p = strrchr(src, '\\');
629 if (!strcmp(p, ".") || !strcmp(p, ".."))
634 run_err("%s: not a regular file", src);
639 if ((last = strrchr(src, '/')) == NULL)
643 if (strrchr(last, '\\') != NULL)
644 last = strrchr(last, '\\') + 1;
645 if (last == src && strchr(src, ':') != NULL)
646 last = strchr(src, ':') + 1;
648 f = CreateFile(src, GENERIC_READ, FILE_SHARE_READ, NULL,
649 OPEN_EXISTING, 0, 0);
650 if (f == INVALID_HANDLE_VALUE) {
651 run_err("%s: Cannot open file", src);
656 FILETIME actime, wrtime;
657 unsigned long mtime, atime;
658 GetFileTime(f, NULL, &actime, &wrtime);
659 TIME_WIN_TO_POSIX(actime, atime);
660 TIME_WIN_TO_POSIX(wrtime, mtime);
661 sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime);
662 back->send(buf, strlen(buf));
667 size = GetFileSize(f, NULL);
668 sprintf(buf, "C0644 %lu %s\n", size, last);
670 tell_user(stderr, "Sending file modes: %s", buf);
671 back->send(buf, strlen(buf));
677 stat_starttime = time(NULL);
681 for (i = 0; i < size; i += 4096) {
684 if (i + k > size) k = size - i;
685 if (! ReadFile(f, transbuf, k, &j, NULL) || j != k) {
686 if (statistics) printf("\n");
687 bump("%s: Read error", src);
689 back->send(transbuf, k);
692 if (time(NULL) != stat_lasttime ||
694 stat_lasttime = time(NULL);
695 print_stats(last, size, stat_bytes,
696 stat_starttime, stat_lasttime);
707 * Recursively send the contents of a directory.
709 static void rsource(char *src)
714 WIN32_FIND_DATA fdat;
717 if ((last = strrchr(src, '/')) == NULL)
721 if (strrchr(last, '\\') != NULL)
722 last = strrchr(last, '\\') + 1;
723 if (last == src && strchr(src, ':') != NULL)
724 last = strchr(src, ':') + 1;
726 /* maybe send filetime */
728 sprintf(buf, "D0755 0 %s\n", last);
730 tell_user(stderr, "Entering directory: %s", buf);
731 back->send(buf, strlen(buf));
735 sprintf(buf, "%s/*", src);
736 dir = FindFirstFile(buf, &fdat);
737 ok = (dir != INVALID_HANDLE_VALUE);
739 if (strcmp(fdat.cFileName, ".") == 0 ||
740 strcmp(fdat.cFileName, "..") == 0) {
741 } else if (strlen(src) + 1 + strlen(fdat.cFileName) >=
743 run_err("%s/%s: Name too long", src, fdat.cFileName);
745 sprintf(buf, "%s/%s", src, fdat.cFileName);
748 ok = FindNextFile(dir, &fdat);
753 back->send(buf, strlen(buf));
758 * Execute the sink part of the SCP protocol.
760 static void sink(char *targ, char *src)
770 unsigned long mtime, atime;
772 unsigned long size, i;
774 unsigned long stat_bytes;
775 time_t stat_starttime, stat_lasttime;
778 attr = GetFileAttributes(targ);
779 if (attr != (DWORD)-1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
782 if (targetshouldbedirectory && !targisdir)
783 bump("%s: Not a directory", targ);
789 if (ssh_scp_recv(&ch, 1) <= 0)
792 bump("Protocol error: Unexpected newline");
796 if (ssh_scp_recv(&ch, 1) <= 0)
797 bump("Lost connection");
799 } while (i < sizeof(buf) && ch != '\n');
802 case '\01': /* error */
803 tell_user(stderr, "%s\n", buf+1);
806 case '\02': /* fatal error */
812 if (sscanf(buf, "T%ld %*d %ld %*d",
813 &mtime, &atime) == 2) {
818 bump("Protocol error: Illegal time format");
823 bump("Protocol error: Expected control record");
826 if (sscanf(buf+1, "%u %lu %[^\n]", &mode, &size, namebuf) != 3)
827 bump("Protocol error: Illegal file descriptor format");
828 /* Security fix: ensure the file ends up where we asked for it. */
830 char *p = src + strlen(src);
831 while (p > src && p[-1] != '/' && p[-1] != '\\')
841 p = namebuf + strlen(namebuf);
842 while (p > namebuf && p[-1] != '/' && p[-1] != '\\')
847 strcpy(namebuf, targ);
849 attr = GetFileAttributes(namebuf);
850 exists = (attr != (DWORD)-1);
853 if (exists && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
854 run_err("%s: Not a directory", namebuf);
858 if (! CreateDirectory(namebuf, NULL)) {
859 run_err("%s: Cannot create directory",
865 /* can we set the timestamp for directories ? */
869 f = CreateFile(namebuf, GENERIC_WRITE, 0, NULL,
870 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
871 if (f == INVALID_HANDLE_VALUE) {
872 run_err("%s: Cannot create file", namebuf);
880 stat_starttime = time(NULL);
882 if ((stat_name = strrchr(namebuf, '/')) == NULL)
886 if (strrchr(stat_name, '\\') != NULL)
887 stat_name = strrchr(stat_name, '\\') + 1;
890 for (i = 0; i < size; i += 4096) {
893 if (i + k > size) k = size - i;
894 if (ssh_scp_recv(transbuf, k) == 0)
895 bump("Lost connection");
896 if (wrerror) continue;
897 if (! WriteFile(f, transbuf, k, &j, NULL) || j != k) {
900 printf("\r%-25.25s | %50s\n",
902 "Write error.. waiting for end of file");
907 if (time(NULL) > stat_lasttime ||
909 stat_lasttime = time(NULL);
910 print_stats(stat_name, size, stat_bytes,
911 stat_starttime, stat_lasttime);
918 FILETIME actime, wrtime;
919 TIME_POSIX_TO_WIN(atime, actime);
920 TIME_POSIX_TO_WIN(mtime, wrtime);
921 SetFileTime(f, NULL, &actime, &wrtime);
926 run_err("%s: Write error", namebuf);
934 * We will copy local files to a remote server.
936 static void toremote(int argc, char *argv[])
938 char *src, *targ, *host, *user;
944 /* Separate host from filename */
948 bump("targ == NULL in toremote()");
952 /* Substitute "." for emtpy target */
954 /* Separate host and username */
956 host = strrchr(host, '@');
967 /* Find out if the source filespec covers multiple files
968 if so, we should set the targetshouldbedirectory flag */
970 WIN32_FIND_DATA fdat;
971 if (colon(argv[0]) != NULL)
972 bump("%s: Remote to remote not supported", argv[0]);
973 fh = FindFirstFile(argv[0], &fdat);
974 if (fh == INVALID_HANDLE_VALUE)
975 bump("%s: No such file or directory\n", argv[0]);
976 if (FindNextFile(fh, &fdat))
977 targetshouldbedirectory = 1;
981 cmd = smalloc(strlen(targ) + 100);
982 sprintf(cmd, "scp%s%s%s%s -t %s",
983 verbose ? " -v" : "",
984 recursive ? " -r" : "",
985 preserve ? " -p" : "",
986 targetshouldbedirectory ? " -d" : "",
988 do_cmd(host, user, cmd);
993 for (i = 0; i < argc - 1; i++) {
995 WIN32_FIND_DATA fdat;
997 if (colon(src) != NULL) {
998 tell_user(stderr, "%s: Remote to remote not supported\n", src);
1002 dir = FindFirstFile(src, &fdat);
1003 if (dir == INVALID_HANDLE_VALUE) {
1004 run_err("%s: No such file or directory", src);
1010 if (strlen(src) + strlen(fdat.cFileName) >=
1012 tell_user(stderr, "%s: Name too long", src);
1015 strcpy(namebuf, src);
1016 if ((last = strrchr(namebuf, '/')) == NULL)
1020 if (strrchr(last, '\\') != NULL)
1021 last = strrchr(last, '\\') + 1;
1022 if (last == namebuf && strrchr(namebuf, ':') != NULL)
1023 last = strchr(namebuf, ':') + 1;
1024 strcpy(last, fdat.cFileName);
1026 } while (FindNextFile(dir, &fdat));
1032 * We will copy files from a remote server to the local machine.
1034 static void tolocal(int argc, char *argv[])
1036 char *src, *targ, *host, *user;
1040 bump("More than one remote source not supported");
1045 /* Separate host from filename */
1049 bump("Local to local copy not supported");
1053 /* Substitute "." for empty filename */
1055 /* Separate username and hostname */
1057 host = strrchr(host, '@');
1067 cmd = smalloc(strlen(src) + 100);
1068 sprintf(cmd, "scp%s%s%s%s -f %s",
1069 verbose ? " -v" : "",
1070 recursive ? " -r" : "",
1071 preserve ? " -p" : "",
1072 targetshouldbedirectory ? " -d" : "",
1074 do_cmd(host, user, cmd);
1081 * We will issue a list command to get a remote directory.
1083 static void get_dir_list(int argc, char *argv[])
1085 char *src, *host, *user;
1091 /* Separate host from filename */
1095 bump("Local to local copy not supported");
1099 /* Substitute "." for empty filename */
1101 /* Separate username and hostname */
1103 host = strrchr(host, '@');
1113 cmd = smalloc(4*strlen(src) + 100);
1114 strcpy(cmd, "ls -la '");
1115 p = cmd + strlen(cmd);
1116 for (q = src; *q; q++) {
1118 *p++ = '\''; *p++ = '\\'; *p++ = '\''; *p++ = '\'';
1126 do_cmd(host, user, cmd);
1129 while (ssh_scp_recv(&c, 1) > 0)
1130 tell_char(stdout, c);
1134 * Initialize the Win$ock driver.
1136 static void init_winsock(void)
1141 winsock_ver = MAKEWORD(1, 1);
1142 if (WSAStartup(winsock_ver, &wsadata))
1143 bump("Unable to initialise WinSock");
1144 if (LOBYTE(wsadata.wVersion) != 1 ||
1145 HIBYTE(wsadata.wVersion) != 1)
1146 bump("WinSock version is incompatible with 1.1");
1150 * Short description of parameters.
1152 static void usage(void)
1154 printf("PuTTY Secure Copy client\n");
1155 printf("%s\n", ver);
1156 printf("Usage: pscp [options] [user@]host:source target\n");
1157 printf(" pscp [options] source [source...] [user@]host:target\n");
1158 printf(" pscp [options] -ls user@host:filespec\n");
1159 printf("Options:\n");
1160 printf(" -p preserve file attributes\n");
1161 printf(" -q quiet, don't show statistics\n");
1162 printf(" -r copy directories recursively\n");
1163 printf(" -v show verbose messages\n");
1164 printf(" -P port connect to specified port\n");
1165 printf(" -pw passw login with specified password\n");
1166 /* GUI Adaptation - Sept 2000 */
1167 printf(" -gui hWnd GUI mode with the windows handle for receiving messages\n");
1172 * Main program (no, really?)
1174 int main(int argc, char *argv[])
1179 default_protocol = PROT_TELNET;
1181 flags = FLAG_STDERR;
1182 ssh_get_password = &get_password;
1186 for (i = 1; i < argc; i++) {
1187 if (argv[i][0] != '-')
1189 if (strcmp(argv[i], "-v") == 0)
1190 verbose = 1, flags |= FLAG_VERBOSE;
1191 else if (strcmp(argv[i], "-r") == 0)
1193 else if (strcmp(argv[i], "-p") == 0)
1195 else if (strcmp(argv[i], "-q") == 0)
1197 else if (strcmp(argv[i], "-h") == 0 ||
1198 strcmp(argv[i], "-?") == 0)
1200 else if (strcmp(argv[i], "-P") == 0 && i+1 < argc)
1201 portnumber = atoi(argv[++i]);
1202 else if (strcmp(argv[i], "-pw") == 0 && i+1 < argc)
1203 password = argv[++i];
1204 else if (strcmp(argv[i], "-gui") == 0 && i+1 < argc) {
1205 gui_hwnd = argv[++i];
1207 } else if (strcmp(argv[i], "-ls") == 0)
1209 else if (strcmp(argv[i], "--") == 0)
1220 get_dir_list(argc, argv);
1227 targetshouldbedirectory = 1;
1229 if (colon(argv[argc-1]) != NULL)
1230 toremote(argc, argv);
1232 tolocal(argc, argv);
1235 if (back->socket() != NULL) {
1237 back->special(TS_EOF);
1238 ssh_scp_recv(&ch, 1);
1243 /* GUI Adaptation - August 2000 */
1245 unsigned int msg_id = WM_RET_ERR_CNT;
1246 if (list) msg_id = WM_LS_RET_ERR_CNT;
1247 while (!PostMessage( (HWND)atoi(gui_hwnd), msg_id, (WPARAM)errs, 0/*lParam*/ ) )
1250 return (errs == 0 ? 0 : 1);