tool_util.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "tool_setup.h"
  25. #if defined(HAVE_STRCASECMP) && defined(HAVE_STRINGS_H)
  26. #include <strings.h>
  27. #endif
  28. #include "tool_util.h"
  29. #include "memdebug.h" /* keep this as LAST include */
  30. #if defined(_WIN32)
  31. /* In case of bug fix this function has a counterpart in timeval.c */
  32. struct timeval tvnow(void)
  33. {
  34. struct timeval now;
  35. if(tool_isVistaOrGreater) { /* QPC timer might have issues pre-Vista */
  36. LARGE_INTEGER count;
  37. QueryPerformanceCounter(&count);
  38. now.tv_sec = (long)(count.QuadPart / tool_freq.QuadPart);
  39. now.tv_usec = (long)((count.QuadPart % tool_freq.QuadPart) * 1000000 /
  40. tool_freq.QuadPart);
  41. }
  42. else {
  43. /* Disable /analyze warning that GetTickCount64 is preferred */
  44. #if defined(_MSC_VER)
  45. #pragma warning(push)
  46. #pragma warning(disable:28159)
  47. #endif
  48. DWORD milliseconds = GetTickCount();
  49. #if defined(_MSC_VER)
  50. #pragma warning(pop)
  51. #endif
  52. now.tv_sec = (long)(milliseconds / 1000);
  53. now.tv_usec = (long)((milliseconds % 1000) * 1000);
  54. }
  55. return now;
  56. }
  57. #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
  58. struct timeval tvnow(void)
  59. {
  60. /*
  61. ** clock_gettime() is granted to be increased monotonically when the
  62. ** monotonic clock is queried. Time starting point is unspecified, it
  63. ** could be the system start-up time, the Epoch, or something else,
  64. ** in any case the time starting point does not change once that the
  65. ** system has started up.
  66. */
  67. struct timeval now;
  68. struct timespec tsnow;
  69. if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
  70. now.tv_sec = tsnow.tv_sec;
  71. now.tv_usec = (int)(tsnow.tv_nsec / 1000);
  72. }
  73. /*
  74. ** Even when the configure process has truly detected monotonic clock
  75. ** availability, it might happen that it is not actually available at
  76. ** run-time. When this occurs simply fallback to other time source.
  77. */
  78. #ifdef HAVE_GETTIMEOFDAY
  79. else
  80. (void)gettimeofday(&now, NULL);
  81. #else
  82. else {
  83. now.tv_sec = time(NULL);
  84. now.tv_usec = 0;
  85. }
  86. #endif
  87. return now;
  88. }
  89. #elif defined(HAVE_GETTIMEOFDAY)
  90. struct timeval tvnow(void)
  91. {
  92. /*
  93. ** gettimeofday() is not granted to be increased monotonically, due to
  94. ** clock drifting and external source time synchronization it can jump
  95. ** forward or backward in time.
  96. */
  97. struct timeval now;
  98. (void)gettimeofday(&now, NULL);
  99. return now;
  100. }
  101. #else
  102. struct timeval tvnow(void)
  103. {
  104. /*
  105. ** time() returns the value of time in seconds since the Epoch.
  106. */
  107. struct timeval now;
  108. now.tv_sec = time(NULL);
  109. now.tv_usec = 0;
  110. return now;
  111. }
  112. #endif
  113. /*
  114. * Make sure that the first argument is the more recent time, as otherwise
  115. * we'll get a weird negative time-diff back...
  116. *
  117. * Returns: the time difference in number of milliseconds.
  118. */
  119. long tvdiff(struct timeval newer, struct timeval older)
  120. {
  121. return (long)(newer.tv_sec-older.tv_sec)*1000+
  122. (long)(newer.tv_usec-older.tv_usec)/1000;
  123. }
  124. /* Case insensitive compare. Accept NULL pointers. */
  125. int struplocompare(const char *p1, const char *p2)
  126. {
  127. if(!p1)
  128. return p2? -1: 0;
  129. if(!p2)
  130. return 1;
  131. #ifdef HAVE_STRCASECMP
  132. return strcasecmp(p1, p2);
  133. #elif defined(HAVE_STRCMPI)
  134. return strcmpi(p1, p2);
  135. #elif defined(HAVE_STRICMP)
  136. return stricmp(p1, p2);
  137. #else
  138. return strcmp(p1, p2);
  139. #endif
  140. }
  141. /* Indirect version to use as qsort callback. */
  142. int struplocompare4sort(const void *p1, const void *p2)
  143. {
  144. return struplocompare(* (char * const *) p1, * (char * const *) p2);
  145. }