d6cc41e6 |
1 | /* |
2 | * winsftp.c: the Windows-specific parts of PSFTP. |
3 | */ |
4 | |
5 | #include <windows.h> |
6 | |
7 | #include "putty.h" |
8 | #include "psftp.h" |
9 | |
10 | /* |
11 | * Be told what socket we're supposed to be using. |
12 | */ |
13 | static SOCKET sftp_ssh_socket; |
14 | char *do_select(SOCKET skt, int startup) |
15 | { |
16 | if (startup) |
17 | sftp_ssh_socket = skt; |
18 | else |
19 | sftp_ssh_socket = INVALID_SOCKET; |
20 | return NULL; |
21 | } |
22 | extern int select_result(WPARAM, LPARAM); |
23 | |
24 | /* |
25 | * Initialize the WinSock driver. |
26 | */ |
27 | static void init_winsock(void) |
28 | { |
29 | WORD winsock_ver; |
30 | WSADATA wsadata; |
31 | |
32 | winsock_ver = MAKEWORD(1, 1); |
33 | if (WSAStartup(winsock_ver, &wsadata)) { |
34 | fprintf(stderr, "Unable to initialise WinSock"); |
35 | cleanup_exit(1); |
36 | } |
37 | if (LOBYTE(wsadata.wVersion) != 1 || HIBYTE(wsadata.wVersion) != 1) { |
38 | fprintf(stderr, "WinSock version is incompatible with 1.1"); |
39 | cleanup_exit(1); |
40 | } |
41 | } |
42 | |
43 | /* |
44 | * Set local current directory. Returns NULL on success, or else an |
45 | * error message which must be freed after printing. |
46 | */ |
47 | char *psftp_lcd(char *dir) |
48 | { |
49 | char *ret = NULL; |
50 | |
51 | if (!SetCurrentDirectory(dir)) { |
52 | LPVOID message; |
53 | int i; |
54 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
55 | FORMAT_MESSAGE_FROM_SYSTEM | |
56 | FORMAT_MESSAGE_IGNORE_INSERTS, |
57 | NULL, GetLastError(), |
58 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
59 | (LPTSTR)&message, 0, NULL); |
60 | i = strcspn((char *)message, "\n"); |
61 | ret = dupprintf("%.*s", i, (LPCTSTR)message); |
62 | LocalFree(message); |
63 | } |
64 | |
65 | return ret; |
66 | } |
67 | |
68 | /* |
69 | * Get local current directory. Returns a string which must be |
70 | * freed. |
71 | */ |
72 | char *psftp_getcwd(void) |
73 | { |
74 | char *ret = snewn(256, char); |
75 | int len = GetCurrentDirectory(256, ret); |
76 | if (len > 256) |
77 | ret = sresize(ret, len, char); |
78 | GetCurrentDirectory(len, ret); |
79 | return ret; |
80 | } |
81 | |
82 | /* |
83 | * Wait for some network data and process it. |
84 | */ |
85 | int ssh_sftp_loop_iteration(void) |
86 | { |
87 | fd_set readfds; |
88 | |
89 | if (sftp_ssh_socket == INVALID_SOCKET) |
90 | return -1; /* doom */ |
91 | |
92 | FD_ZERO(&readfds); |
93 | FD_SET(sftp_ssh_socket, &readfds); |
94 | if (select(1, &readfds, NULL, NULL, NULL) < 0) |
95 | return -1; /* doom */ |
96 | |
97 | select_result((WPARAM) sftp_ssh_socket, (LPARAM) FD_READ); |
98 | return 0; |
99 | } |
100 | |
101 | /* |
102 | * Main program. Parse arguments etc. |
103 | */ |
104 | int main(int argc, char *argv[]) |
105 | { |
106 | int ret; |
107 | |
108 | init_winsock(); |
109 | ret = psftp_main(argc, argv); |
110 | WSACleanup(); |
111 | |
112 | return ret; |
113 | } |