lib525.c (4249B)
1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 9 * 10 * This software is licensed as described in the file COPYING, which 11 * you should have received as part of this distribution. The terms 12 * are also available at https://curl.se/docs/copyright.html. 13 * 14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 15 * copies of the Software, and permit persons to whom the Software is 16 * furnished to do so, under the terms of the COPYING file. 17 * 18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 * KIND, either express or implied. 20 * 21 * SPDX-License-Identifier: curl 22 * 23 ***************************************************************************/ 24 #include "first.h" 25 26 #include "memdebug.h" 27 28 static CURLcode test_lib525(char *URL) 29 { 30 CURLcode res = CURLE_OK; 31 CURL *curl = NULL; 32 FILE *hd_src = NULL; 33 int hd; 34 struct_stat file_info; 35 CURLM *m = NULL; 36 int running; 37 38 start_test_timing(); 39 40 if(!libtest_arg2) { 41 curl_mfprintf(stderr, "Usage: test [url] [uploadfile]\n"); 42 return TEST_ERR_USAGE; 43 } 44 45 hd_src = fopen(libtest_arg2, "rb"); 46 if(!hd_src) { 47 curl_mfprintf(stderr, "fopen failed with error (%d) %s\n", 48 errno, strerror(errno)); 49 curl_mfprintf(stderr, "Error opening file '%s'\n", libtest_arg2); 50 return TEST_ERR_FOPEN; 51 } 52 53 /* get the file size of the local file */ 54 #ifdef UNDER_CE 55 hd = stat(libtest_arg2, &file_info); 56 #else 57 hd = fstat(fileno(hd_src), &file_info); 58 #endif 59 if(hd == -1) { 60 /* can't open file, bail out */ 61 curl_mfprintf(stderr, "fstat() failed with error (%d) %s\n", 62 errno, strerror(errno)); 63 curl_mfprintf(stderr, "Error opening file '%s'\n", libtest_arg2); 64 fclose(hd_src); 65 return TEST_ERR_FSTAT; 66 } 67 68 res_global_init(CURL_GLOBAL_ALL); 69 if(res) { 70 fclose(hd_src); 71 return res; 72 } 73 74 easy_init(curl); 75 76 /* enable uploading */ 77 easy_setopt(curl, CURLOPT_UPLOAD, 1L); 78 79 /* specify target */ 80 easy_setopt(curl, CURLOPT_URL, URL); 81 82 /* go verbose */ 83 easy_setopt(curl, CURLOPT_VERBOSE, 1L); 84 85 /* use active FTP */ 86 easy_setopt(curl, CURLOPT_FTPPORT, "-"); 87 88 /* now specify which file to upload */ 89 easy_setopt(curl, CURLOPT_READDATA, hd_src); 90 91 /* NOTE: if you want this code to work on Windows with libcurl as a DLL, you 92 MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to 93 do so will give you a crash since a DLL may not use the variable's memory 94 when passed in to it from an app like this. */ 95 96 /* Set the size of the file to upload (optional). If you give a *_LARGE 97 option you MUST make sure that the type of the passed-in argument is a 98 curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must 99 make sure that to pass in a type 'long' argument. */ 100 easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size); 101 102 multi_init(m); 103 104 multi_add_handle(m, curl); 105 106 for(;;) { 107 struct timeval interval; 108 fd_set rd, wr, exc; 109 int maxfd = -99; 110 111 interval.tv_sec = 1; 112 interval.tv_usec = 0; 113 114 multi_perform(m, &running); 115 116 abort_on_test_timeout(); 117 118 if(!running) 119 break; /* done */ 120 121 FD_ZERO(&rd); 122 FD_ZERO(&wr); 123 FD_ZERO(&exc); 124 125 multi_fdset(m, &rd, &wr, &exc, &maxfd); 126 127 /* At this point, maxfd is guaranteed to be greater or equal than -1. */ 128 129 select_test(maxfd + 1, &rd, &wr, &exc, &interval); 130 131 abort_on_test_timeout(); 132 } 133 134 test_cleanup: 135 136 if(testnum == 529) { 137 /* proper cleanup sequence - type PA */ 138 curl_multi_remove_handle(m, curl); 139 curl_multi_cleanup(m); 140 curl_easy_cleanup(curl); 141 curl_global_cleanup(); 142 } 143 else { /* testnum == 525 */ 144 /* proper cleanup sequence - type PB */ 145 curl_multi_remove_handle(m, curl); 146 curl_easy_cleanup(curl); 147 curl_multi_cleanup(m); 148 curl_global_cleanup(); 149 } 150 151 /* close the local file */ 152 fclose(hd_src); 153 154 return res; 155 }