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
26 #if defined(HAVE_STRCASECMP) && defined(HAVE_STRINGS_H)
27 #include <strings.h>
28 #endif
29
30 #include "tool_util.h"
31
32 #include "curlx.h"
33 #include "memdebug.h" /* keep this as LAST include */
34
35 #if defined(_WIN32)
36
37 /* In case of bug fix this function has a counterpart in timeval.c */
tvnow(void)38 struct timeval tvnow(void)
39 {
40 struct timeval now;
41 if(tool_isVistaOrGreater) { /* QPC timer might have issues pre-Vista */
42 LARGE_INTEGER count;
43 QueryPerformanceCounter(&count);
44 now.tv_sec = (long)(count.QuadPart / tool_freq.QuadPart);
45 now.tv_usec = (long)((count.QuadPart % tool_freq.QuadPart) * 1000000 /
46 tool_freq.QuadPart);
47 }
48 else {
49 /* Disable /analyze warning that GetTickCount64 is preferred */
50 #if defined(_MSC_VER)
51 #pragma warning(push)
52 #pragma warning(disable:28159)
53 #endif
54 DWORD milliseconds = GetTickCount();
55 #if defined(_MSC_VER)
56 #pragma warning(pop)
57 #endif
58
59 now.tv_sec = (long)(milliseconds / 1000);
60 now.tv_usec = (long)((milliseconds % 1000) * 1000);
61 }
62 return now;
63 }
64
65 #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
66
tvnow(void)67 struct timeval tvnow(void)
68 {
69 /*
70 ** clock_gettime() is granted to be increased monotonically when the
71 ** monotonic clock is queried. Time starting point is unspecified, it
72 ** could be the system start-up time, the Epoch, or something else,
73 ** in any case the time starting point does not change once that the
74 ** system has started up.
75 */
76 struct timeval now;
77 struct timespec tsnow;
78 if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
79 now.tv_sec = tsnow.tv_sec;
80 now.tv_usec = (int)(tsnow.tv_nsec / 1000);
81 }
82 /*
83 ** Even when the configure process has truly detected monotonic clock
84 ** availability, it might happen that it is not actually available at
85 ** runtime. When this occurs simply fallback to other time source.
86 */
87 #ifdef HAVE_GETTIMEOFDAY
88 else
89 (void)gettimeofday(&now, NULL);
90 #else
91 else {
92 now.tv_sec = time(NULL);
93 now.tv_usec = 0;
94 }
95 #endif
96 return now;
97 }
98
99 #elif defined(HAVE_GETTIMEOFDAY)
100
tvnow(void)101 struct timeval tvnow(void)
102 {
103 /*
104 ** gettimeofday() is not granted to be increased monotonically, due to
105 ** clock drifting and external source time synchronization it can jump
106 ** forward or backward in time.
107 */
108 struct timeval now;
109 (void)gettimeofday(&now, NULL);
110 return now;
111 }
112
113 #else
114
tvnow(void)115 struct timeval tvnow(void)
116 {
117 /*
118 ** time() returns the value of time in seconds since the Epoch.
119 */
120 struct timeval now;
121 now.tv_sec = time(NULL);
122 now.tv_usec = 0;
123 return now;
124 }
125
126 #endif
127
128 /*
129 * Make sure that the first argument is the more recent time, as otherwise
130 * we will get a weird negative time-diff back...
131 *
132 * Returns: the time difference in number of milliseconds.
133 */
tvdiff(struct timeval newer,struct timeval older)134 long tvdiff(struct timeval newer, struct timeval older)
135 {
136 return (long)(newer.tv_sec-older.tv_sec)*1000+
137 (long)(newer.tv_usec-older.tv_usec)/1000;
138 }
139
140 /* Case insensitive compare. Accept NULL pointers. */
struplocompare(const char * p1,const char * p2)141 int struplocompare(const char *p1, const char *p2)
142 {
143 if(!p1)
144 return p2 ? -1 : 0;
145 if(!p2)
146 return 1;
147 #ifdef HAVE_STRCASECMP
148 return strcasecmp(p1, p2);
149 #elif defined(HAVE_STRCMPI)
150 return strcmpi(p1, p2);
151 #elif defined(HAVE_STRICMP)
152 return stricmp(p1, p2);
153 #else
154 return strcmp(p1, p2);
155 #endif
156 }
157
158 /* Indirect version to use as qsort callback. */
struplocompare4sort(const void * p1,const void * p2)159 int struplocompare4sort(const void *p1, const void *p2)
160 {
161 return struplocompare(* (char * const *) p1, * (char * const *) p2);
162 }
163
164 #ifdef USE_TOOL_FTRUNCATE
165
166 #ifdef _WIN32_WCE
167 /* 64-bit lseek-like function unavailable */
168 # undef _lseeki64
169 # define _lseeki64(hnd,ofs,whence) lseek(hnd,ofs,whence)
170 # undef _get_osfhandle
171 # define _get_osfhandle(fd) (fd)
172 #endif
173
174 /*
175 * Truncate a file handle at a 64-bit position 'where'.
176 */
177
tool_ftruncate64(int fd,curl_off_t where)178 int tool_ftruncate64(int fd, curl_off_t where)
179 {
180 intptr_t handle = _get_osfhandle(fd);
181
182 if(_lseeki64(fd, where, SEEK_SET) < 0)
183 return -1;
184
185 if(!SetEndOfFile((HANDLE)handle))
186 return -1;
187
188 return 0;
189 }
190
191 #endif /* USE_TOOL_FTRUNCATE */
192
193 #ifdef _WIN32
Curl_execpath(const char * filename,char ** pathp)194 FILE *Curl_execpath(const char *filename, char **pathp)
195 {
196 static char filebuffer[512];
197 unsigned long len;
198 /* Get the filename of our executable. GetModuleFileName is already declared
199 * via inclusions done in setup header file. We assume that we are using
200 * the ASCII version here.
201 */
202 len = GetModuleFileNameA(0, filebuffer, sizeof(filebuffer));
203 if(len > 0 && len < sizeof(filebuffer)) {
204 /* We got a valid filename - get the directory part */
205 char *lastdirchar = strrchr(filebuffer, DIR_CHAR[0]);
206 if(lastdirchar) {
207 size_t remaining;
208 *lastdirchar = 0;
209 /* If we have enough space, build the RC filename */
210 remaining = sizeof(filebuffer) - strlen(filebuffer);
211 if(strlen(filename) < remaining - 1) {
212 msnprintf(lastdirchar, remaining, "%s%s", DIR_CHAR, filename);
213 *pathp = filebuffer;
214 return fopen(filebuffer, FOPEN_READTEXT);
215 }
216 }
217 }
218
219 return NULL;
220 }
221 #endif
222