tool_util.c 4.5 KB

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