2 * winsftp.c: the Windows-specific parts of PSFTP and PSCP.
17 /* ----------------------------------------------------------------------
18 * Interface to GUI driver program.
21 /* This is just a base value from which the main message numbers are
23 #define WM_APP_BASE 0x8000
25 /* These two pass a single character value in wParam. They represent
26 * the visible output from PSCP. */
27 #define WM_STD_OUT_CHAR ( WM_APP_BASE+400 )
28 #define WM_STD_ERR_CHAR ( WM_APP_BASE+401 )
30 /* These pass a transfer status update. WM_STATS_CHAR passes a single
31 * character in wParam, and is called repeatedly to pass the name of
32 * the file, terminated with "\n". WM_STATS_SIZE passes the size of
33 * the file being transferred in wParam. WM_STATS_ELAPSED is called
34 * to pass the elapsed time (in seconds) in wParam, and
35 * WM_STATS_PERCENT passes the percentage of the transfer which is
36 * complete, also in wParam. */
37 #define WM_STATS_CHAR ( WM_APP_BASE+402 )
38 #define WM_STATS_SIZE ( WM_APP_BASE+403 )
39 #define WM_STATS_PERCENT ( WM_APP_BASE+404 )
40 #define WM_STATS_ELAPSED ( WM_APP_BASE+405 )
42 /* These are used at the end of a run to pass an error code in
43 * wParam: zero means success, nonzero means failure. WM_RET_ERR_CNT
44 * is used after a copy, and WM_LS_RET_ERR_CNT is used after a file
46 #define WM_RET_ERR_CNT ( WM_APP_BASE+406 )
47 #define WM_LS_RET_ERR_CNT ( WM_APP_BASE+407 )
49 /* More transfer status update messages. WM_STATS_DONE passes the
50 * number of bytes sent so far in wParam. WM_STATS_ETA passes the
51 * estimated time to completion (in seconds). WM_STATS_RATEBS passes
52 * the average transfer rate (in bytes per second). */
53 #define WM_STATS_DONE ( WM_APP_BASE+408 )
54 #define WM_STATS_ETA ( WM_APP_BASE+409 )
55 #define WM_STATS_RATEBS ( WM_APP_BASE+410 )
57 #define NAME_STR_MAX 2048
58 static char statname[NAME_STR_MAX + 1];
59 static unsigned long statsize = 0;
60 static unsigned long statdone = 0;
61 static unsigned long stateta = 0;
62 static unsigned long statratebs = 0;
63 static int statperct = 0;
64 static unsigned long statelapsed = 0;
66 static HWND gui_hwnd = NULL;
68 static void send_msg(HWND h, UINT message, WPARAM wParam)
70 while (!PostMessage(h, message, wParam, 0))
74 void gui_send_char(int is_stderr, int c)
76 unsigned int msg_id = WM_STD_OUT_CHAR;
78 msg_id = WM_STD_ERR_CHAR;
79 send_msg(gui_hwnd, msg_id, (WPARAM) c);
82 void gui_send_errcount(int list, int errs)
84 unsigned int msg_id = WM_RET_ERR_CNT;
86 msg_id = WM_LS_RET_ERR_CNT;
87 while (!PostMessage(gui_hwnd, msg_id, (WPARAM) errs, 0))
91 void gui_update_stats(char *name, unsigned long size,
92 int percentage, unsigned long elapsed,
93 unsigned long done, unsigned long eta,
98 if (strcmp(name, statname) != 0) {
99 for (i = 0; i < strlen(name); ++i)
100 send_msg(gui_hwnd, WM_STATS_CHAR, (WPARAM) name[i]);
101 send_msg(gui_hwnd, WM_STATS_CHAR, (WPARAM) '\n');
102 strcpy(statname, name);
104 if (statsize != size) {
105 send_msg(gui_hwnd, WM_STATS_SIZE, (WPARAM) size);
108 if (statdone != done) {
109 send_msg(gui_hwnd, WM_STATS_DONE, (WPARAM) done);
112 if (stateta != eta) {
113 send_msg(gui_hwnd, WM_STATS_ETA, (WPARAM) eta);
116 if (statratebs != ratebs) {
117 send_msg(gui_hwnd, WM_STATS_RATEBS, (WPARAM) ratebs);
120 if (statelapsed != elapsed) {
121 send_msg(gui_hwnd, WM_STATS_ELAPSED, (WPARAM) elapsed);
122 statelapsed = elapsed;
124 if (statperct != percentage) {
125 send_msg(gui_hwnd, WM_STATS_PERCENT, (WPARAM) percentage);
126 statperct = percentage;
130 void gui_enable(char *arg)
132 gui_hwnd = (HWND) atoi(arg);
135 /* ----------------------------------------------------------------------
136 * File access abstraction.
140 * Set local current directory. Returns NULL on success, or else an
141 * error message which must be freed after printing.
143 char *psftp_lcd(char *dir)
147 if (!SetCurrentDirectory(dir)) {
150 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
151 FORMAT_MESSAGE_FROM_SYSTEM |
152 FORMAT_MESSAGE_IGNORE_INSERTS,
153 NULL, GetLastError(),
154 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
155 (LPTSTR)&message, 0, NULL);
156 i = strcspn((char *)message, "\n");
157 ret = dupprintf("%.*s", i, (LPCTSTR)message);
165 * Get local current directory. Returns a string which must be
168 char *psftp_getcwd(void)
170 char *ret = snewn(256, char);
171 int len = GetCurrentDirectory(256, ret);
173 ret = sresize(ret, len, char);
174 GetCurrentDirectory(len, ret);
178 #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \
179 ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000)
180 #define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \
181 ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600))
187 RFile *open_existing_file(char *name, unsigned long *size,
188 unsigned long *mtime, unsigned long *atime)
193 h = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL,
194 OPEN_EXISTING, 0, 0);
195 if (h == INVALID_HANDLE_VALUE)
202 *size = GetFileSize(h, NULL);
204 if (mtime || atime) {
205 FILETIME actime, wrtime;
206 GetFileTime(h, NULL, &actime, &wrtime);
208 TIME_WIN_TO_POSIX(actime, *atime);
210 TIME_WIN_TO_POSIX(wrtime, *mtime);
216 int read_from_file(RFile *f, void *buffer, int length)
219 ret = ReadFile(f->h, buffer, length, &read, NULL);
221 return -1; /* error */
226 void close_rfile(RFile *f)
236 WFile *open_new_file(char *name)
241 h = CreateFile(name, GENERIC_WRITE, 0, NULL,
242 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
243 if (h == INVALID_HANDLE_VALUE)
252 int write_to_file(WFile *f, void *buffer, int length)
255 ret = WriteFile(f->h, buffer, length, &written, NULL);
257 return -1; /* error */
262 void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
264 FILETIME actime, wrtime;
265 TIME_POSIX_TO_WIN(atime, actime);
266 TIME_POSIX_TO_WIN(mtime, wrtime);
267 SetFileTime(f->h, NULL, &actime, &wrtime);
270 void close_wfile(WFile *f)
276 int file_type(char *name)
279 attr = GetFileAttributes(name);
280 /* We know of no `weird' files under Windows. */
281 if (attr == (DWORD)-1)
282 return FILE_TYPE_NONEXISTENT;
283 else if (attr & FILE_ATTRIBUTE_DIRECTORY)
284 return FILE_TYPE_DIRECTORY;
286 return FILE_TYPE_FILE;
294 DirHandle *open_directory(char *name)
297 WIN32_FIND_DATA fdat;
301 /* To enumerate files in dir `foo', we search for `foo/*'. */
302 findfile = dupcat(name, "/*", NULL);
303 h = FindFirstFile(findfile, &fdat);
304 if (h == INVALID_HANDLE_VALUE)
308 ret = snew(DirHandle);
310 ret->name = dupstr(fdat.cFileName);
314 char *read_filename(DirHandle *dir)
317 WIN32_FIND_DATA fdat;
318 int ok = FindNextFile(dir->h, &fdat);
323 if (fdat.cFileName[0] == '.' &&
324 (fdat.cFileName[1] == '\0' ||
325 (fdat.cFileName[1] == '.' && fdat.cFileName[2] == '\0')))
328 dir->name = dupstr(fdat.cFileName);
332 char *ret = dir->name;
339 void close_directory(DirHandle *dir)
347 int test_wildcard(char *name, int cmdline)
350 WIN32_FIND_DATA fdat;
352 /* First see if the exact name exists. */
353 if (GetFileAttributes(name) != (DWORD)-1)
354 return WCTYPE_FILENAME;
356 /* Otherwise see if a wildcard match finds anything. */
357 fh = FindFirstFile(name, &fdat);
358 if (fh == INVALID_HANDLE_VALUE)
359 return WCTYPE_NONEXISTENT;
362 return WCTYPE_WILDCARD;
365 struct WildcardMatcher {
372 * Return a pointer to the portion of str that comes after the last
373 * slash (or backslash or colon, if `local' is TRUE).
375 static char *stripslashes(char *str, int local)
380 p = strchr(str, ':');
384 p = strrchr(str, '/');
388 p = strrchr(str, '\\');
395 WildcardMatcher *begin_wildcard_matching(char *name)
398 WIN32_FIND_DATA fdat;
399 WildcardMatcher *ret;
402 h = FindFirstFile(name, &fdat);
403 if (h == INVALID_HANDLE_VALUE)
406 ret = snew(WildcardMatcher);
408 ret->srcpath = dupstr(name);
409 last = stripslashes(ret->srcpath, 1);
411 if (fdat.cFileName[0] == '.' &&
412 (fdat.cFileName[1] == '\0' ||
413 (fdat.cFileName[1] == '.' && fdat.cFileName[2] == '\0')))
416 ret->name = dupcat(ret->srcpath, fdat.cFileName, NULL);
421 char *wildcard_get_filename(WildcardMatcher *dir)
424 WIN32_FIND_DATA fdat;
425 int ok = FindNextFile(dir->h, &fdat);
430 if (fdat.cFileName[0] == '.' &&
431 (fdat.cFileName[1] == '\0' ||
432 (fdat.cFileName[1] == '.' && fdat.cFileName[2] == '\0')))
435 dir->name = dupcat(dir->srcpath, fdat.cFileName, NULL);
439 char *ret = dir->name;
446 void finish_wildcard_matching(WildcardMatcher *dir)
455 int create_directory(char *name)
457 return CreateDirectory(name, NULL) != 0;
460 char *dir_file_cat(char *dir, char *file)
462 return dupcat(dir, "\\", file, NULL);
465 /* ----------------------------------------------------------------------
466 * Platform-specific network handling.
470 * Be told what socket we're supposed to be using.
472 static SOCKET sftp_ssh_socket;
473 char *do_select(SOCKET skt, int startup)
476 sftp_ssh_socket = skt;
478 sftp_ssh_socket = INVALID_SOCKET;
481 extern int select_result(WPARAM, LPARAM);
484 * Initialize the WinSock driver.
486 static void init_winsock(void)
491 winsock_ver = MAKEWORD(1, 1);
492 if (WSAStartup(winsock_ver, &wsadata)) {
493 fprintf(stderr, "Unable to initialise WinSock");
496 if (LOBYTE(wsadata.wVersion) != 1 || HIBYTE(wsadata.wVersion) != 1) {
497 fprintf(stderr, "WinSock version is incompatible with 1.1");
503 * Wait for some network data and process it.
505 int ssh_sftp_loop_iteration(void)
509 if (sftp_ssh_socket == INVALID_SOCKET)
510 return -1; /* doom */
513 FD_SET(sftp_ssh_socket, &readfds);
514 if (select(1, &readfds, NULL, NULL, NULL) < 0)
515 return -1; /* doom */
517 select_result((WPARAM) sftp_ssh_socket, (LPARAM) FD_READ);
521 /* ----------------------------------------------------------------------
522 * Main program. Parse arguments etc.
524 int main(int argc, char *argv[])
529 ret = psftp_main(argc, argv);