lib541.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 http://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. * 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. int error;
  38. if (!libtest_arg2) {
  39. fprintf(stderr, "Usage: <url> <file-to-upload>\n");
  40. return -1;
  41. }
  42. hd_src = fopen(libtest_arg2, "rb");
  43. if(NULL == hd_src) {
  44. error = ERRNO;
  45. fprintf(stderr, "fopen() failed with error: %d %s\n",
  46. error, strerror(error));
  47. fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
  48. return -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. error = ERRNO;
  55. fprintf(stderr, "fstat() failed with error: %d %s\n",
  56. error, strerror(error));
  57. fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
  58. fclose(hd_src);
  59. return -1;
  60. }
  61. if(! file_info.st_size) {
  62. fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
  63. fclose(hd_src);
  64. return -4;
  65. }
  66. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  67. fprintf(stderr, "curl_global_init() failed\n");
  68. fclose(hd_src);
  69. return TEST_ERR_MAJOR_BAD;
  70. }
  71. /* get a curl handle */
  72. if ((curl = curl_easy_init()) == NULL) {
  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_INFILE, hd_src);
  86. /* Now run off and do what you've been told! */
  87. res = curl_easy_perform(curl);
  88. /* and now upload the exact same again, but without rewinding so it already
  89. is at end of file */
  90. res = curl_easy_perform(curl);
  91. test_cleanup:
  92. /* close the local file */
  93. fclose(hd_src);
  94. curl_easy_cleanup(curl);
  95. curl_global_cleanup();
  96. return res;
  97. }