first.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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_LOCALE_H
  24. # include <locale.h> /* for setlocale() */
  25. #endif
  26. #ifdef HAVE_IO_H
  27. # include <io.h> /* for setmode() */
  28. #endif
  29. #ifdef HAVE_FCNTL_H
  30. # include <fcntl.h> /* for setmode() */
  31. #endif
  32. #ifdef CURLDEBUG
  33. # define MEMDEBUG_NODEFINES
  34. # include "memdebug.h"
  35. #endif
  36. int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc,
  37. struct timeval *tv)
  38. {
  39. if(nfds < 0) {
  40. SET_SOCKERRNO(EINVAL);
  41. return -1;
  42. }
  43. #ifdef USE_WINSOCK
  44. /*
  45. * Winsock select() requires that at least one of the three fd_set
  46. * pointers is not NULL and points to a non-empty fdset. IOW Winsock
  47. * select() can not be used to sleep without a single fd_set.
  48. */
  49. if(!nfds) {
  50. Sleep(1000*tv->tv_sec + tv->tv_usec/1000);
  51. return 0;
  52. }
  53. #endif
  54. return select(nfds, rd, wr, exc, tv);
  55. }
  56. void wait_ms(int ms)
  57. {
  58. struct timeval t;
  59. t.tv_sec = ms/1000;
  60. ms -= (int)t.tv_sec * 1000;
  61. t.tv_usec = ms * 1000;
  62. select_wrapper(0, NULL, NULL , NULL, &t);
  63. }
  64. char *libtest_arg2=NULL;
  65. char *libtest_arg3=NULL;
  66. int test_argc;
  67. char **test_argv;
  68. struct timeval tv_test_start; /* for test timing */
  69. #ifdef UNITTESTS
  70. int unitfail; /* for unittests */
  71. #endif
  72. #ifdef CURLDEBUG
  73. static void memory_tracking_init(void)
  74. {
  75. char *env;
  76. /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
  77. env = curl_getenv("CURL_MEMDEBUG");
  78. if(env) {
  79. /* use the value as file name */
  80. char fname[CURL_MT_LOGFNAME_BUFSIZE];
  81. if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE)
  82. env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0';
  83. strcpy(fname, env);
  84. curl_free(env);
  85. curl_memdebug(fname);
  86. /* this weird stuff here is to make curl_free() get called
  87. before curl_memdebug() as otherwise memory tracking will
  88. log a free() without an alloc! */
  89. }
  90. /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
  91. env = curl_getenv("CURL_MEMLIMIT");
  92. if(env) {
  93. char *endptr;
  94. long num = strtol(env, &endptr, 10);
  95. if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
  96. curl_memlimit(num);
  97. curl_free(env);
  98. }
  99. }
  100. #else
  101. # define memory_tracking_init() Curl_nop_stmt
  102. #endif
  103. int main(int argc, char **argv)
  104. {
  105. char *URL;
  106. #ifdef O_BINARY
  107. # ifdef __HIGHC__
  108. _setmode(stdout, O_BINARY);
  109. # else
  110. setmode(fileno(stdout), O_BINARY);
  111. # endif
  112. #endif
  113. memory_tracking_init();
  114. /*
  115. * Setup proper locale from environment. This is needed to enable locale-
  116. * specific behaviour by the C library in order to test for undesired side
  117. * effects that could cause in libcurl.
  118. */
  119. #ifdef HAVE_SETLOCALE
  120. setlocale(LC_ALL, "");
  121. #endif
  122. if(argc< 2 ) {
  123. fprintf(stderr, "Pass URL as argument please\n");
  124. return 1;
  125. }
  126. test_argc = argc;
  127. test_argv = argv;
  128. if(argc>2)
  129. libtest_arg2=argv[2];
  130. if(argc>3)
  131. libtest_arg3=argv[3];
  132. URL = argv[1]; /* provide this to the rest */
  133. fprintf(stderr, "URL: %s\n", URL);
  134. return test(URL);
  135. }