Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
6.9 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. ftpclose.c
  5. Abstract:
  6. Tests FTP open/read/close. Main purpose is to make sure we do the right
  7. thing with ABOR
  8. Author:
  9. Richard L Firth (rfirth) 11-Oct-1995
  10. Revision History:
  11. 11-Oct-1995 rfirth
  12. Created
  13. --*/
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <windows.h>
  17. #include <wininet.h>
  18. #ifndef _CRTAPI1
  19. #define _CRTAPI1
  20. #endif
  21. #define IS_ARG(c) (((c) == '-') || ((c) == '/'))
  22. #define DEFAULT_BUFFER_SIZE 1024
  23. void _CRTAPI1 main(int, char**);
  24. void usage(void);
  25. BOOL Verbose = FALSE;
  26. void _CRTAPI1 main(int argc, char** argv) {
  27. HINTERNET hInternet;
  28. HINTERNET hFtpSession;
  29. HINTERNET hFile;
  30. LPSTR server = NULL;
  31. LPSTR username = NULL;
  32. LPSTR password = NULL;
  33. LPSTR filename = NULL;
  34. LPBYTE buffer;
  35. int buflen = DEFAULT_BUFFER_SIZE;
  36. DWORD bytesRead;
  37. DWORD totalBytes;
  38. DWORD closeAfter = 0xffffffff;
  39. DWORD accessMethod = INTERNET_OPEN_TYPE_PRECONFIG;
  40. BOOL expectingProxyServer = FALSE;
  41. LPSTR proxyServer = NULL;
  42. for (--argc, ++argv; argc; --argc, ++argv) {
  43. if (IS_ARG(**argv)) {
  44. switch (*++*argv) {
  45. case '?':
  46. usage();
  47. break;
  48. case 'a':
  49. ++*argv;
  50. if (**argv == 'p') {
  51. accessMethod = INTERNET_OPEN_TYPE_PROXY;
  52. if (*++*argv) {
  53. proxyServer = *argv;
  54. } else {
  55. expectingProxyServer = TRUE;
  56. }
  57. } else if (**argv == 'l') {
  58. accessMethod = INTERNET_OPEN_TYPE_DIRECT;
  59. } else {
  60. if (**argv) {
  61. printf("error: unrecognised access type: '%c'\n", **argv);
  62. } else {
  63. printf("error: missing access type\n");
  64. }
  65. usage();
  66. }
  67. break;
  68. case 'b':
  69. buflen = atoi(++*argv);
  70. break;
  71. case 'f':
  72. filename = ++*argv;
  73. break;
  74. case 'n':
  75. closeAfter = (DWORD)atoi(++*argv);
  76. break;
  77. case 'p':
  78. password = ++*argv;
  79. break;
  80. case 's':
  81. server = ++*argv;
  82. break;
  83. case 'u':
  84. username = ++*argv;
  85. break;
  86. case 'v':
  87. Verbose = TRUE;
  88. break;
  89. default:
  90. printf("error: unrecognized command line flag: '%c'\n", **argv);
  91. usage();
  92. }
  93. } else if (expectingProxyServer) {
  94. proxyServer = *argv;
  95. expectingProxyServer = FALSE;
  96. }
  97. }
  98. if (!server) {
  99. printf("error: must supply server name\n");
  100. usage();
  101. }
  102. if (!filename) {
  103. printf("error: must supply file name\n");
  104. usage();
  105. }
  106. hInternet = InternetOpen("ftpclose",
  107. accessMethod,
  108. proxyServer,
  109. NULL,
  110. 0);
  111. if (!hInternet) {
  112. printf("error: InternetOpen() returns %d\n", GetLastError());
  113. exit(1);
  114. } else if (Verbose) {
  115. printf("opened Internet handle %x\n", hInternet);
  116. }
  117. hFtpSession = InternetConnect(hInternet,
  118. server,
  119. INTERNET_INVALID_PORT_NUMBER,
  120. username,
  121. password,
  122. INTERNET_SERVICE_FTP,
  123. 0,
  124. 0
  125. );
  126. if (!hFtpSession) {
  127. printf("error: InternetConnect() returns %d\n", GetLastError());
  128. InternetCloseHandle(hInternet);
  129. exit(1);
  130. } else if (Verbose) {
  131. printf("opened FTP connect handle %x\n", hFtpSession);
  132. }
  133. hFile = FtpOpenFile(hFtpSession,
  134. filename,
  135. GENERIC_READ,
  136. FTP_TRANSFER_TYPE_BINARY,
  137. 0,
  138. 0
  139. );
  140. if (!hFile) {
  141. printf("error: FtpOpenFile(%s) returns %d\n", filename, GetLastError());
  142. InternetCloseHandle(hFtpSession);
  143. InternetCloseHandle(hInternet);
  144. exit(1);
  145. } else if (Verbose) {
  146. printf("opened FTP File handle %x\n", hFile);
  147. }
  148. buffer = (LPBYTE)malloc(buflen);
  149. if (!buffer) {
  150. printf("error: failed to allocate %u bytes\n", buflen);
  151. InternetCloseHandle(hFile);
  152. InternetCloseHandle(hFtpSession);
  153. InternetCloseHandle(hInternet);
  154. exit(1);
  155. }
  156. if (closeAfter == 0) {
  157. if (Verbose) {
  158. printf("not reading file (close after 0)\n");
  159. }
  160. } else {
  161. if (Verbose) {
  162. printf("reading file %s", filename);
  163. if (closeAfter != 0xffffffff) {
  164. printf(" closing after %d bytes", closeAfter);
  165. }
  166. putchar('\n');
  167. }
  168. totalBytes = 0;
  169. while (InternetReadFile(hFile, buffer, buflen, &bytesRead)) {
  170. if (Verbose) {
  171. printf("read %d bytes\n", bytesRead);
  172. }
  173. if (bytesRead == 0) {
  174. break;
  175. }
  176. totalBytes += bytesRead;
  177. if (totalBytes >= closeAfter) {
  178. break;
  179. }
  180. }
  181. if (GetLastError() != ERROR_SUCCESS) {
  182. printf("error: InternetReadFile() returns %d\n", GetLastError());
  183. InternetCloseHandle(hFile);
  184. InternetCloseHandle(hFtpSession);
  185. InternetCloseHandle(hInternet);
  186. exit(1);
  187. }
  188. if (Verbose) {
  189. printf("%d bytes read\n", totalBytes);
  190. }
  191. }
  192. InternetCloseHandle(hFile);
  193. InternetCloseHandle(hFtpSession);
  194. InternetCloseHandle(hInternet);
  195. exit(0);
  196. }
  197. void usage() {
  198. printf("\n"
  199. "usage: ftpclose [-a{l|p}] [-b#] [-v] [-n#] <-sserver> [-uuser] [-ppassword]\n"
  200. " <-ffile>\n"
  201. "\n"
  202. "where: -a = Access mode: l = local; p = proxy\n"
  203. " -b = Buffer size. Default is %d\n"
  204. " -n = Number of bytes to read before closing file. Default is all\n"
  205. " -p = Password (with -u)\n"
  206. " -s = FTP server\n"
  207. " -u = User name\n"
  208. " -v = Verbose mode\n",
  209. DEFAULT_BUFFER_SIZE
  210. );
  211. exit(1);
  212. }