2
0

lib505.c 4.1 KB

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