lib541.c 3.1 KB

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