2
0

lib505.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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 "test.h"
  25. #ifdef HAVE_FCNTL_H
  26. #include <fcntl.h>
  27. #endif
  28. #include "memdebug.h"
  29. /*
  30. * This example shows an FTP upload, with a rename of the file just after
  31. * a successful upload.
  32. *
  33. * Example based on source code provided by Erick Nuwendam. Thanks!
  34. */
  35. int test(char *URL)
  36. {
  37. CURL *curl;
  38. CURLcode res = CURLE_OK;
  39. FILE *hd_src;
  40. int hd;
  41. struct_stat file_info;
  42. struct curl_slist *hl;
  43. struct curl_slist *headerlist = NULL;
  44. const char *buf_1 = "RNFR 505";
  45. const char *buf_2 = "RNTO 505-forreal";
  46. if(!libtest_arg2) {
  47. fprintf(stderr, "Usage: <url> <file-to-upload>\n");
  48. return TEST_ERR_USAGE;
  49. }
  50. hd_src = fopen(libtest_arg2, "rb");
  51. if(!hd_src) {
  52. fprintf(stderr, "fopen failed with error: %d %s\n",
  53. errno, strerror(errno));
  54. fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
  55. return TEST_ERR_MAJOR_BAD; /* if this happens things are major weird */
  56. }
  57. /* get the file size of the local file */
  58. hd = fstat(fileno(hd_src), &file_info);
  59. if(hd == -1) {
  60. /* can't open file, bail out */
  61. fprintf(stderr, "fstat() failed with error: %d %s\n",
  62. errno, strerror(errno));
  63. fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
  64. fclose(hd_src);
  65. return TEST_ERR_MAJOR_BAD;
  66. }
  67. if(!file_info.st_size) {
  68. fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
  69. fclose(hd_src);
  70. return TEST_ERR_MAJOR_BAD;
  71. }
  72. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  73. fprintf(stderr, "curl_global_init() failed\n");
  74. fclose(hd_src);
  75. return TEST_ERR_MAJOR_BAD;
  76. }
  77. /* get a curl handle */
  78. curl = curl_easy_init();
  79. if(!curl) {
  80. fprintf(stderr, "curl_easy_init() failed\n");
  81. curl_global_cleanup();
  82. fclose(hd_src);
  83. return TEST_ERR_MAJOR_BAD;
  84. }
  85. /* build a list of commands to pass to libcurl */
  86. hl = curl_slist_append(headerlist, buf_1);
  87. if(!hl) {
  88. fprintf(stderr, "curl_slist_append() failed\n");
  89. curl_easy_cleanup(curl);
  90. curl_global_cleanup();
  91. fclose(hd_src);
  92. return TEST_ERR_MAJOR_BAD;
  93. }
  94. headerlist = curl_slist_append(hl, buf_2);
  95. if(!headerlist) {
  96. fprintf(stderr, "curl_slist_append() failed\n");
  97. curl_slist_free_all(hl);
  98. curl_easy_cleanup(curl);
  99. curl_global_cleanup();
  100. fclose(hd_src);
  101. return TEST_ERR_MAJOR_BAD;
  102. }
  103. headerlist = hl;
  104. /* enable uploading */
  105. test_setopt(curl, CURLOPT_UPLOAD, 1L);
  106. /* enable verbose */
  107. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  108. /* specify target */
  109. test_setopt(curl, CURLOPT_URL, URL);
  110. /* pass in that last of FTP commands to run after the transfer */
  111. test_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
  112. /* now specify which file to upload */
  113. test_setopt(curl, CURLOPT_READDATA, hd_src);
  114. /* and give the size of the upload (optional) */
  115. test_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  116. (curl_off_t)file_info.st_size);
  117. /* Now run off and do what you've been told! */
  118. res = curl_easy_perform(curl);
  119. test_cleanup:
  120. /* clean up the FTP commands list */
  121. curl_slist_free_all(headerlist);
  122. /* close the local file */
  123. fclose(hd_src);
  124. curl_easy_cleanup(curl);
  125. curl_global_cleanup();
  126. return res;
  127. }