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)
307 ret = snew(DirHandle);
309 ret->name = dupstr(fdat.cFileName);
313 char *read_filename(DirHandle *dir)
316 WIN32_FIND_DATA fdat;
317 int ok = FindNextFile(dir->h, &fdat);
320 dir->name = dupstr(fdat.cFileName);
324 char *ret = dir->name;
331 void close_directory(DirHandle *dir)
339 int test_wildcard(char *name, int cmdline)
342 WIN32_FIND_DATA fdat;
344 /* First see if the exact name exists. */
345 if (GetFileAttributes(name) != (DWORD)-1)
346 return WCTYPE_FILENAME;
348 /* Otherwise see if a wildcard match finds anything. */
349 fh = FindFirstFile(name, &fdat);
350 if (fh == INVALID_HANDLE_VALUE)
351 return WCTYPE_NONEXISTENT;
354 return WCTYPE_WILDCARD;
357 struct WildcardMatcher {
364 * Return a pointer to the portion of str that comes after the last
365 * slash (or backslash or colon, if `local' is TRUE).
367 static char *stripslashes(char *str, int local)
372 p = strchr(str, ':');
376 p = strrchr(str, '/');
380 p = strrchr(str, '\\');
387 WildcardMatcher *begin_wildcard_matching(char *name)
390 WIN32_FIND_DATA fdat;
391 WildcardMatcher *ret;
394 h = FindFirstFile(name, &fdat);
395 if (h == INVALID_HANDLE_VALUE)
398 ret = snew(WildcardMatcher);
400 ret->srcpath = dupstr(name);
401 last = stripslashes(ret->srcpath, 1);
403 if (fdat.cFileName[0] == '.' &&
404 (fdat.cFileName[1] == '\0' ||
405 (fdat.cFileName[1] == '.' && fdat.cFileName[2] == '\0')))
408 ret->name = dupcat(ret->srcpath, fdat.cFileName, NULL);
413 char *wildcard_get_filename(WildcardMatcher *dir)
416 WIN32_FIND_DATA fdat;
417 int ok = FindNextFile(dir->h, &fdat);
422 if (fdat.cFileName[0] == '.' &&
423 (fdat.cFileName[1] == '\0' ||
424 (fdat.cFileName[1] == '.' && fdat.cFileName[2] == '\0')))
427 dir->name = dupcat(dir->srcpath, fdat.cFileName, NULL);
431 char *ret = dir->name;
438 void finish_wildcard_matching(WildcardMatcher *dir)
447 int create_directory(char *name)
449 return CreateDirectory(name, NULL) != 0;
452 /* ----------------------------------------------------------------------
453 * Platform-specific network handling.
457 * Be told what socket we're supposed to be using.
459 static SOCKET sftp_ssh_socket;
460 char *do_select(SOCKET skt, int startup)
463 sftp_ssh_socket = skt;
465 sftp_ssh_socket = INVALID_SOCKET;
468 extern int select_result(WPARAM, LPARAM);
471 * Initialize the WinSock driver.
473 static void init_winsock(void)
478 winsock_ver = MAKEWORD(1, 1);
479 if (WSAStartup(winsock_ver, &wsadata)) {
480 fprintf(stderr, "Unable to initialise WinSock");
483 if (LOBYTE(wsadata.wVersion) != 1 || HIBYTE(wsadata.wVersion) != 1) {
484 fprintf(stderr, "WinSock version is incompatible with 1.1");
490 * Wait for some network data and process it.
492 int ssh_sftp_loop_iteration(void)
496 if (sftp_ssh_socket == INVALID_SOCKET)
497 return -1; /* doom */
500 FD_SET(sftp_ssh_socket, &readfds);
501 if (select(1, &readfds, NULL, NULL, NULL) < 0)
502 return -1; /* doom */
504 select_result((WPARAM) sftp_ssh_socket, (LPARAM) FD_READ);
508 /* ----------------------------------------------------------------------
509 * Main program. Parse arguments etc.
511 int main(int argc, char *argv[])
516 ret = psftp_main(argc, argv);