tool_util.c (3907B)
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 #include "tool_util.h" 27 #include "memdebug.h" /* keep this as LAST include */ 28 29 #ifdef _WIN32 30 31 struct timeval tvrealnow(void) 32 { 33 /* UNIX EPOCH (1970-01-01) in FILETIME (1601-01-01) as 64-bit value */ 34 static const curl_uint64_t EPOCH = (curl_uint64_t)116444736000000000ULL; 35 SYSTEMTIME systime; 36 FILETIME ftime; /* 100ns since 1601-01-01, as double 32-bit value */ 37 curl_uint64_t time; /* 100ns since 1601-01-01, as 64-bit value */ 38 struct timeval now; 39 40 GetSystemTime(&systime); 41 SystemTimeToFileTime(&systime, &ftime); 42 time = ((curl_uint64_t)ftime.dwLowDateTime); 43 time += ((curl_uint64_t)ftime.dwHighDateTime) << 32; 44 45 now.tv_sec = (long)((time - EPOCH) / 10000000L); /* unit is 100ns */ 46 now.tv_usec = (long)(systime.wMilliseconds * 1000); 47 return now; 48 } 49 50 #else 51 52 struct timeval tvrealnow(void) 53 { 54 struct timeval now; 55 #ifdef HAVE_GETTIMEOFDAY 56 (void)gettimeofday(&now, NULL); 57 #else 58 now.tv_sec = time(NULL); 59 now.tv_usec = 0; 60 #endif 61 return now; 62 } 63 64 #endif 65 66 /* Case insensitive compare. Accept NULL pointers. */ 67 int struplocompare(const char *p1, const char *p2) 68 { 69 if(!p1) 70 return p2 ? -1 : 0; 71 if(!p2) 72 return 1; 73 return CURL_STRICMP(p1, p2); 74 } 75 76 /* Indirect version to use as qsort callback. */ 77 int struplocompare4sort(const void *p1, const void *p2) 78 { 79 return struplocompare(* (char * const *) p1, * (char * const *) p2); 80 } 81 82 #ifdef USE_TOOL_FTRUNCATE 83 84 #ifdef UNDER_CE 85 /* 64-bit lseek-like function unavailable */ 86 # undef _lseeki64 87 # define _lseeki64(hnd,ofs,whence) lseek(hnd,ofs,whence) 88 #endif 89 90 /* 91 * Truncate a file handle at a 64-bit position 'where'. 92 */ 93 94 int tool_ftruncate64(int fd, curl_off_t where) 95 { 96 intptr_t handle = _get_osfhandle(fd); 97 98 if(_lseeki64(fd, where, SEEK_SET) < 0) 99 return -1; 100 101 if(!SetEndOfFile((HANDLE)handle)) 102 return -1; 103 104 return 0; 105 } 106 107 #endif /* USE_TOOL_FTRUNCATE */ 108 109 #if defined(_WIN32) && !defined(UNDER_CE) 110 FILE *tool_execpath(const char *filename, char **pathp) 111 { 112 static char filebuffer[512]; 113 unsigned long len; 114 /* Get the filename of our executable. GetModuleFileName is already declared 115 * via inclusions done in setup header file. We assume that we are using 116 * the ASCII version here. 117 */ 118 len = GetModuleFileNameA(0, filebuffer, sizeof(filebuffer)); 119 if(len > 0 && len < sizeof(filebuffer)) { 120 /* We got a valid filename - get the directory part */ 121 char *lastdirchar = strrchr(filebuffer, DIR_CHAR[0]); 122 if(lastdirchar) { 123 size_t remaining; 124 *lastdirchar = 0; 125 /* If we have enough space, build the RC filename */ 126 remaining = sizeof(filebuffer) - strlen(filebuffer); 127 if(strlen(filename) < remaining - 1) { 128 curl_msnprintf(lastdirchar, remaining, "%s%s", DIR_CHAR, filename); 129 *pathp = filebuffer; 130 return fopen(filebuffer, FOPEN_READTEXT); 131 } 132 } 133 } 134 135 return NULL; 136 } 137 #endif