2 * winsftp.c: the Windows-specific parts of PSFTP and PSCP.
8 /* ----------------------------------------------------------------------
9 * Interface to GUI driver program.
12 /* This is just a base value from which the main message numbers are
14 #define WM_APP_BASE 0x8000
16 /* These two pass a single character value in wParam. They represent
17 * the visible output from PSCP. */
18 #define WM_STD_OUT_CHAR ( WM_APP_BASE+400 )
19 #define WM_STD_ERR_CHAR ( WM_APP_BASE+401 )
21 /* These pass a transfer status update. WM_STATS_CHAR passes a single
22 * character in wParam, and is called repeatedly to pass the name of
23 * the file, terminated with "\n". WM_STATS_SIZE passes the size of
24 * the file being transferred in wParam. WM_STATS_ELAPSED is called
25 * to pass the elapsed time (in seconds) in wParam, and
26 * WM_STATS_PERCENT passes the percentage of the transfer which is
27 * complete, also in wParam. */
28 #define WM_STATS_CHAR ( WM_APP_BASE+402 )
29 #define WM_STATS_SIZE ( WM_APP_BASE+403 )
30 #define WM_STATS_PERCENT ( WM_APP_BASE+404 )
31 #define WM_STATS_ELAPSED ( WM_APP_BASE+405 )
33 /* These are used at the end of a run to pass an error code in
34 * wParam: zero means success, nonzero means failure. WM_RET_ERR_CNT
35 * is used after a copy, and WM_LS_RET_ERR_CNT is used after a file
37 #define WM_RET_ERR_CNT ( WM_APP_BASE+406 )
38 #define WM_LS_RET_ERR_CNT ( WM_APP_BASE+407 )
40 /* More transfer status update messages. WM_STATS_DONE passes the
41 * number of bytes sent so far in wParam. WM_STATS_ETA passes the
42 * estimated time to completion (in seconds). WM_STATS_RATEBS passes
43 * the average transfer rate (in bytes per second). */
44 #define WM_STATS_DONE ( WM_APP_BASE+408 )
45 #define WM_STATS_ETA ( WM_APP_BASE+409 )
46 #define WM_STATS_RATEBS ( WM_APP_BASE+410 )
48 #define NAME_STR_MAX 2048
49 static char statname
[NAME_STR_MAX
+ 1];
50 static unsigned long statsize
= 0;
51 static unsigned long statdone
= 0;
52 static unsigned long stateta
= 0;
53 static unsigned long statratebs
= 0;
54 static int statperct
= 0;
55 static unsigned long statelapsed
= 0;
57 static HWND gui_hwnd
= NULL
;
59 static void send_msg(HWND h
, UINT message
, WPARAM wParam
)
61 while (!PostMessage(h
, message
, wParam
, 0))
65 void gui_send_char(int is_stderr
, int c
)
67 unsigned int msg_id
= WM_STD_OUT_CHAR
;
69 msg_id
= WM_STD_ERR_CHAR
;
70 send_msg(gui_hwnd
, msg_id
, (WPARAM
) c
);
73 void gui_send_errcount(int list
, int errs
)
75 unsigned int msg_id
= WM_RET_ERR_CNT
;
77 msg_id
= WM_LS_RET_ERR_CNT
;
78 while (!PostMessage(gui_hwnd
, msg_id
, (WPARAM
) errs
, 0))
82 void gui_update_stats(char *name
, unsigned long size
,
83 int percentage
, unsigned long elapsed
,
84 unsigned long done
, unsigned long eta
,
89 if (strcmp(name
, statname
) != 0) {
90 for (i
= 0; i
< strlen(name
); ++i
)
91 send_msg(gui_hwnd
, WM_STATS_CHAR
, (WPARAM
) name
[i
]);
92 send_msg(gui_hwnd
, WM_STATS_CHAR
, (WPARAM
) '\n');
93 strcpy(statname
, name
);
95 if (statsize
!= size
) {
96 send_msg(gui_hwnd
, WM_STATS_SIZE
, (WPARAM
) size
);
99 if (statdone
!= done
) {
100 send_msg(gui_hwnd
, WM_STATS_DONE
, (WPARAM
) done
);
103 if (stateta
!= eta
) {
104 send_msg(gui_hwnd
, WM_STATS_ETA
, (WPARAM
) eta
);
107 if (statratebs
!= ratebs
) {
108 send_msg(gui_hwnd
, WM_STATS_RATEBS
, (WPARAM
) ratebs
);
111 if (statelapsed
!= elapsed
) {
112 send_msg(gui_hwnd
, WM_STATS_ELAPSED
, (WPARAM
) elapsed
);
113 statelapsed
= elapsed
;
115 if (statperct
!= percentage
) {
116 send_msg(gui_hwnd
, WM_STATS_PERCENT
, (WPARAM
) percentage
);
117 statperct
= percentage
;
121 void gui_enable(char *arg
)
123 gui_hwnd
= (HWND
) atoi(arg
);
126 /* ----------------------------------------------------------------------
127 * File access abstraction.
131 * Set local current directory. Returns NULL on success, or else an
132 * error message which must be freed after printing.
134 char *psftp_lcd(char *dir
)
138 if (!SetCurrentDirectory(dir
)) {
141 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
142 FORMAT_MESSAGE_FROM_SYSTEM
|
143 FORMAT_MESSAGE_IGNORE_INSERTS
,
144 NULL
, GetLastError(),
145 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
146 (LPTSTR
)&message
, 0, NULL
);
147 i
= strcspn((char *)message
, "\n");
148 ret
= dupprintf("%.*s", i
, (LPCTSTR
)message
);
156 * Get local current directory. Returns a string which must be
159 char *psftp_getcwd(void)
161 char *ret
= snewn(256, char);
162 int len
= GetCurrentDirectory(256, ret
);
164 ret
= sresize(ret
, len
, char);
165 GetCurrentDirectory(len
, ret
);
169 #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \
170 ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000)
171 #define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \
172 ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600))
178 RFile
*open_existing_file(char *name
, unsigned long *size
,
179 unsigned long *mtime
, unsigned long *atime
)
184 h
= CreateFile(name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
185 OPEN_EXISTING
, 0, 0);
186 if (h
== INVALID_HANDLE_VALUE
)
193 *size
= GetFileSize(h
, NULL
);
195 if (mtime
|| atime
) {
196 FILETIME actime
, wrtime
;
197 GetFileTime(h
, NULL
, &actime
, &wrtime
);
199 TIME_WIN_TO_POSIX(actime
, *atime
);
201 TIME_WIN_TO_POSIX(wrtime
, *mtime
);
207 int read_from_file(RFile
*f
, void *buffer
, int length
)
210 ret
= ReadFile(f
->h
, buffer
, length
, &read
, NULL
);
212 return -1; /* error */
217 void close_rfile(RFile
*f
)
227 WFile
*open_new_file(char *name
)
232 h
= CreateFile(name
, GENERIC_WRITE
, 0, NULL
,
233 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
234 if (h
== INVALID_HANDLE_VALUE
)
243 int write_to_file(WFile
*f
, void *buffer
, int length
)
246 ret
= WriteFile(f
->h
, buffer
, length
, &written
, NULL
);
248 return -1; /* error */
253 void set_file_times(WFile
*f
, unsigned long mtime
, unsigned long atime
)
255 FILETIME actime
, wrtime
;
256 TIME_POSIX_TO_WIN(atime
, actime
);
257 TIME_POSIX_TO_WIN(mtime
, wrtime
);
258 SetFileTime(f
->h
, NULL
, &actime
, &wrtime
);
261 void close_wfile(WFile
*f
)
267 int file_type(char *name
)
270 attr
= GetFileAttributes(name
);
271 /* We know of no `weird' files under Windows. */
272 if (attr
== (DWORD
)-1)
273 return FILE_TYPE_NONEXISTENT
;
274 else if (attr
& FILE_ATTRIBUTE_DIRECTORY
)
275 return FILE_TYPE_DIRECTORY
;
277 return FILE_TYPE_FILE
;
285 DirHandle
*open_directory(char *name
)
288 WIN32_FIND_DATA fdat
;
292 /* To enumerate files in dir `foo', we search for `foo/*'. */
293 findfile
= dupcat(name
, "/*", NULL
);
294 h
= FindFirstFile(findfile
, &fdat
);
295 if (h
== INVALID_HANDLE_VALUE
)
299 ret
= snew(DirHandle
);
301 ret
->name
= dupstr(fdat
.cFileName
);
305 char *read_filename(DirHandle
*dir
)
308 WIN32_FIND_DATA fdat
;
309 int ok
= FindNextFile(dir
->h
, &fdat
);
314 if (fdat
.cFileName
[0] == '.' &&
315 (fdat
.cFileName
[1] == '\0' ||
316 (fdat
.cFileName
[1] == '.' && fdat
.cFileName
[2] == '\0')))
319 dir
->name
= dupstr(fdat
.cFileName
);
323 char *ret
= dir
->name
;
330 void close_directory(DirHandle
*dir
)
338 int test_wildcard(char *name
, int cmdline
)
341 WIN32_FIND_DATA fdat
;
343 /* First see if the exact name exists. */
344 if (GetFileAttributes(name
) != (DWORD
)-1)
345 return WCTYPE_FILENAME
;
347 /* Otherwise see if a wildcard match finds anything. */
348 fh
= FindFirstFile(name
, &fdat
);
349 if (fh
== INVALID_HANDLE_VALUE
)
350 return WCTYPE_NONEXISTENT
;
353 return WCTYPE_WILDCARD
;
356 struct WildcardMatcher
{
363 * Return a pointer to the portion of str that comes after the last
364 * slash (or backslash or colon, if `local' is TRUE).
366 static char *stripslashes(char *str
, int local
)
371 p
= strchr(str
, ':');
375 p
= strrchr(str
, '/');
379 p
= strrchr(str
, '\\');
386 WildcardMatcher
*begin_wildcard_matching(char *name
)
389 WIN32_FIND_DATA fdat
;
390 WildcardMatcher
*ret
;
393 h
= FindFirstFile(name
, &fdat
);
394 if (h
== INVALID_HANDLE_VALUE
)
397 ret
= snew(WildcardMatcher
);
399 ret
->srcpath
= dupstr(name
);
400 last
= stripslashes(ret
->srcpath
, 1);
402 if (fdat
.cFileName
[0] == '.' &&
403 (fdat
.cFileName
[1] == '\0' ||
404 (fdat
.cFileName
[1] == '.' && fdat
.cFileName
[2] == '\0')))
407 ret
->name
= dupcat(ret
->srcpath
, fdat
.cFileName
, NULL
);
412 char *wildcard_get_filename(WildcardMatcher
*dir
)
415 WIN32_FIND_DATA fdat
;
416 int ok
= FindNextFile(dir
->h
, &fdat
);
421 if (fdat
.cFileName
[0] == '.' &&
422 (fdat
.cFileName
[1] == '\0' ||
423 (fdat
.cFileName
[1] == '.' && fdat
.cFileName
[2] == '\0')))
426 dir
->name
= dupcat(dir
->srcpath
, fdat
.cFileName
, NULL
);
430 char *ret
= dir
->name
;
437 void finish_wildcard_matching(WildcardMatcher
*dir
)
446 int create_directory(char *name
)
448 return CreateDirectory(name
, NULL
) != 0;
451 char *dir_file_cat(char *dir
, char *file
)
453 return dupcat(dir
, "\\", file
, NULL
);
456 /* ----------------------------------------------------------------------
457 * Platform-specific network handling.
461 * Be told what socket we're supposed to be using.
463 static SOCKET sftp_ssh_socket
;
464 char *do_select(SOCKET skt
, int startup
)
467 sftp_ssh_socket
= skt
;
469 sftp_ssh_socket
= INVALID_SOCKET
;
472 extern int select_result(WPARAM
, LPARAM
);
475 * Wait for some network data and process it.
477 int ssh_sftp_loop_iteration(void)
481 if (sftp_ssh_socket
== INVALID_SOCKET
)
482 return -1; /* doom */
485 FD_SET(sftp_ssh_socket
, &readfds
);
486 if (p_select(1, &readfds
, NULL
, NULL
, NULL
) < 0)
487 return -1; /* doom */
489 select_result((WPARAM
) sftp_ssh_socket
, (LPARAM
) FD_READ
);
493 /* ----------------------------------------------------------------------
494 * Main program. Parse arguments etc.
496 int main(int argc
, char *argv
[])
500 ret
= psftp_main(argc
, argv
);