lib541.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "test.h"
  25. #ifdef HAVE_FCNTL_H
  26. #include <fcntl.h>
  27. #endif
  28. #include "memdebug.h"
  29. /*
  30. * Two FTP uploads, the second with no content sent.
  31. */
  32. CURLcode test(char *URL)
  33. {
  34. CURL *curl;
  35. CURLcode res = CURLE_OK;
  36. FILE *hd_src;
  37. int hd;
  38. struct_stat file_info;
  39. if(!libtest_arg2) {
  40. fprintf(stderr, "Usage: <url> <file-to-upload>\n");
  41. return TEST_ERR_USAGE;
  42. }
  43. hd_src = fopen(libtest_arg2, "rb");
  44. if(!hd_src) {
  45. fprintf(stderr, "fopen failed with error: %d %s\n",
  46. errno, strerror(errno));
  47. fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
  48. return (CURLcode)-2; /* if this happens things are major weird */
  49. }
  50. /* get the file size of the local file */
  51. hd = fstat(fileno(hd_src), &file_info);
  52. if(hd == -1) {
  53. /* can't open file, bail out */
  54. fprintf(stderr, "fstat() failed with error: %d %s\n",
  55. errno, strerror(errno));
  56. fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
  57. fclose(hd_src);
  58. return TEST_ERR_MAJOR_BAD;
  59. }
  60. if(!file_info.st_size) {
  61. fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
  62. fclose(hd_src);
  63. return TEST_ERR_MAJOR_BAD;
  64. }
  65. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  66. fprintf(stderr, "curl_global_init() failed\n");
  67. fclose(hd_src);
  68. return TEST_ERR_MAJOR_BAD;
  69. }
  70. /* get a curl handle */
  71. curl = curl_easy_init();
  72. if(!curl) {
  73. fprintf(stderr, "curl_easy_init() failed\n");
  74. curl_global_cleanup();
  75. fclose(hd_src);
  76. return TEST_ERR_MAJOR_BAD;
  77. }
  78. /* enable uploading */
  79. test_setopt(curl, CURLOPT_UPLOAD, 1L);
  80. /* enable verbose */
  81. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  82. /* specify target */
  83. test_setopt(curl, CURLOPT_URL, URL);
  84. /* now specify which file to upload */
  85. test_setopt(curl, CURLOPT_READDATA, hd_src);
  86. /* Now run off and do what you've been told! */
  87. res = curl_easy_perform(curl);
  88. if(res)
  89. goto test_cleanup;
  90. /* and now upload the exact same again, but without rewinding so it already
  91. is at end of file */
  92. res = curl_easy_perform(curl);
  93. test_cleanup:
  94. /* close the local file */
  95. fclose(hd_src);
  96. curl_easy_cleanup(curl);
  97. curl_global_cleanup();
  98. return res;
  99. }