Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file compat.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief compatibility functions |
| 5 | * |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 6 | * Copyright (c) 2021 CESNET, z.s.p.o. |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 14 | #define _POSIX_C_SOURCE 200809L /* fdopen, _POSIX_PATH_MAX, strdup */ |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 15 | #define _ISOC99_SOURCE /* vsnprintf */ |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 16 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 17 | #include "compat.h" |
| 18 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 19 | #include <errno.h> |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 20 | #include <inttypes.h> |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 21 | #include <limits.h> |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 22 | #include <pthread.h> |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 23 | #include <stdarg.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 26 | #include <string.h> |
Jan Kundrát | 8ae30ea | 2021-12-09 23:37:02 +0100 | [diff] [blame] | 27 | #ifndef _MSC_VER |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 28 | #include <sys/time.h> |
Jan Kundrát | 8ae30ea | 2021-12-09 23:37:02 +0100 | [diff] [blame] | 29 | #endif |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 30 | #include <time.h> |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | |
| 33 | #ifndef HAVE_VDPRINTF |
| 34 | int |
| 35 | vdprintf(int fd, const char *format, va_list ap) |
| 36 | { |
| 37 | FILE *stream; |
Radek Krejci | afb2883 | 2020-12-01 12:16:28 +0100 | [diff] [blame] | 38 | int count = 0; |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 39 | |
| 40 | stream = fdopen(dup(fd), "a+"); |
| 41 | if (stream) { |
| 42 | count = vfprintf(stream, format, ap); |
| 43 | fclose(stream); |
| 44 | } |
| 45 | return count; |
| 46 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 47 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 48 | #endif |
| 49 | |
| 50 | #ifndef HAVE_ASPRINTF |
| 51 | int |
| 52 | asprintf(char **strp, const char *fmt, ...) |
| 53 | { |
| 54 | int ret; |
| 55 | va_list ap; |
| 56 | |
| 57 | va_start(ap, fmt); |
| 58 | ret = vasprintf(strp, fmt, ap); |
| 59 | va_end(ap); |
| 60 | return ret; |
| 61 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 62 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 63 | #endif |
| 64 | |
| 65 | #ifndef HAVE_VASPRINTF |
| 66 | int |
| 67 | vasprintf(char **strp, const char *fmt, va_list ap) |
| 68 | { |
| 69 | va_list ap2; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 70 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 71 | va_copy(ap2, ap); |
| 72 | int l = vsnprintf(0, 0, fmt, ap2); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 73 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 74 | va_end(ap2); |
| 75 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 76 | if ((l < 0) || !(*strp = malloc(l + 1U))) { |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | return vsnprintf(*strp, l + 1U, fmt, ap); |
| 81 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 82 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 83 | #endif |
| 84 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 85 | #ifndef HAVE_GETLINE |
| 86 | ssize_t |
| 87 | getline(char **lineptr, size_t *n, FILE *stream) |
| 88 | { |
| 89 | static char line[256]; |
| 90 | char *ptr; |
| 91 | ssize_t len; |
| 92 | |
| 93 | if (!lineptr || !n) { |
| 94 | errno = EINVAL; |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | if (ferror(stream) || feof(stream)) { |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | if (!fgets(line, 256, stream)) { |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | ptr = strchr(line, '\n'); |
| 107 | if (ptr) { |
| 108 | *ptr = '\0'; |
| 109 | } |
| 110 | |
| 111 | len = strlen(line); |
| 112 | |
| 113 | if (len + 1 < 256) { |
| 114 | ptr = realloc(*lineptr, 256); |
| 115 | if (!ptr) { |
| 116 | return -1; |
| 117 | } |
| 118 | *lineptr = ptr; |
| 119 | *n = 256; |
| 120 | } |
| 121 | |
| 122 | strcpy(*lineptr, line); |
| 123 | return len; |
| 124 | } |
| 125 | |
| 126 | #endif |
| 127 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 128 | #ifndef HAVE_STRNDUP |
| 129 | char * |
| 130 | strndup(const char *s, size_t n) |
| 131 | { |
| 132 | char *buf; |
| 133 | size_t len = 0; |
| 134 | |
| 135 | /* strnlen */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 136 | for ( ; (len < n) && (s[len] != '\0'); ++len) {} |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 137 | |
| 138 | if (!(buf = malloc(len + 1U))) { |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | memcpy(buf, s, len); |
| 143 | buf[len] = '\0'; |
| 144 | return buf; |
| 145 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 146 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 147 | #endif |
| 148 | |
| 149 | #ifndef HAVE_STRNSTR |
| 150 | char * |
| 151 | strnstr(const char *s, const char *find, size_t slen) |
| 152 | { |
| 153 | char c, sc; |
| 154 | size_t len; |
| 155 | |
| 156 | if ((c = *find++) != '\0') { |
| 157 | len = strlen(find); |
| 158 | do { |
| 159 | do { |
| 160 | if ((slen-- < 1) || ((sc = *s++) == '\0')) { |
| 161 | return NULL; |
| 162 | } |
| 163 | } while (sc != c); |
| 164 | if (len > slen) { |
| 165 | return NULL; |
| 166 | } |
| 167 | } while (strncmp(s, find, len)); |
| 168 | s--; |
| 169 | } |
| 170 | return (char *)s; |
| 171 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 172 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 173 | #endif |
| 174 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 175 | #ifndef HAVE_STRCHRNUL |
| 176 | char * |
| 177 | strchrnul(const char *s, int c) |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 178 | { |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 179 | char *p = strchr(s, c); |
Michal Vasko | 45c4b1a | 2021-05-19 16:48:12 +0200 | [diff] [blame] | 180 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 181 | return p ? p : (char *)s + strlen(s); |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 182 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 183 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 184 | #endif |
| 185 | |
| 186 | #ifndef HAVE_GET_CURRENT_DIR_NAME |
| 187 | char * |
| 188 | get_current_dir_name(void) |
| 189 | { |
| 190 | char tmp[_POSIX_PATH_MAX]; |
| 191 | char *retval = NULL; |
| 192 | |
| 193 | if (getcwd(tmp, sizeof(tmp))) { |
| 194 | retval = strdup(tmp); |
| 195 | if (!retval) { |
| 196 | errno = ENOMEM; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return retval; |
| 201 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 202 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 203 | #endif |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 204 | |
Jan Kundrát | e235414 | 2021-12-10 16:29:21 +0100 | [diff] [blame] | 205 | #ifndef _MSC_VER |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 206 | #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK |
| 207 | int |
| 208 | pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime) |
| 209 | { |
| 210 | int64_t nsec_diff; |
| 211 | int32_t diff; |
| 212 | struct timespec cur, dur; |
| 213 | int rc; |
| 214 | |
| 215 | /* try to acquire the lock and, if we fail, sleep for 5ms. */ |
| 216 | while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) { |
| 217 | /* get real time */ |
| 218 | #ifdef CLOCK_REALTIME |
| 219 | clock_gettime(CLOCK_REALTIME, &cur); |
| 220 | #else |
| 221 | struct timeval tv; |
| 222 | |
| 223 | gettimeofday(&tv, NULL); |
| 224 | cur.tv_sec = (time_t)tv.tv_sec; |
| 225 | cur.tv_nsec = 1000L * (long)tv.tv_usec; |
| 226 | #endif |
| 227 | |
| 228 | /* get time diff */ |
| 229 | nsec_diff = 0; |
| 230 | nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L; |
| 231 | nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec); |
| 232 | diff = (nsec_diff ? nsec_diff / 1000000L : 0); |
| 233 | |
| 234 | if (diff < 1) { |
| 235 | /* timeout */ |
| 236 | break; |
| 237 | } else if (diff < 5) { |
| 238 | /* sleep until timeout */ |
| 239 | dur.tv_sec = 0; |
| 240 | dur.tv_nsec = (long)diff * 1000000; |
| 241 | } else { |
| 242 | /* sleep 5 ms */ |
| 243 | dur.tv_sec = 0; |
| 244 | dur.tv_nsec = 5000000; |
| 245 | } |
| 246 | |
| 247 | nanosleep(&dur, NULL); |
| 248 | } |
| 249 | |
| 250 | return rc; |
| 251 | } |
| 252 | |
| 253 | #endif |
Jan Kundrát | e235414 | 2021-12-10 16:29:21 +0100 | [diff] [blame] | 254 | #endif |
Jan Kundrát | 147a72c | 2021-12-10 16:13:57 +0100 | [diff] [blame] | 255 | |
| 256 | #ifndef HAVE_REALPATH |
| 257 | #ifdef _WIN32 |
| 258 | char * |
| 259 | realpath(const char *path, char *resolved_path) |
| 260 | { |
| 261 | char *resolved = _fullpath(resolved_path, path, PATH_MAX); |
| 262 | |
| 263 | if ((_access(resolved, 0) == -1) && (errno == ENOENT)) { |
| 264 | return NULL; |
| 265 | } |
| 266 | return resolved; |
| 267 | } |
| 268 | |
Donatas Abraitis | d5d533c | 2022-06-10 08:22:25 +0300 | [diff] [blame^] | 269 | #elif defined (__NetBSD__) |
| 270 | char * |
| 271 | realpath(const char *path, char *resolved_path) |
| 272 | { |
| 273 | ssize_t nbytes; |
| 274 | |
| 275 | nbytes = readlink(path, resolved_path, PATH_MAX); |
| 276 | if (nbytes == -1) { |
| 277 | return NULL; |
| 278 | } |
| 279 | return resolved_path; |
| 280 | } |
| 281 | |
Jan Kundrát | 147a72c | 2021-12-10 16:13:57 +0100 | [diff] [blame] | 282 | #else |
| 283 | #error No realpath() implementation for this platform is available. |
| 284 | #endif |
| 285 | #endif |
Jan Kundrát | 9190763 | 2021-12-14 16:18:16 +0100 | [diff] [blame] | 286 | |
| 287 | #ifndef HAVE_LOCALTIME_R |
| 288 | #ifdef _WIN32 |
| 289 | struct tm * |
| 290 | localtime_r(const time_t *timep, struct tm *result) |
| 291 | { |
| 292 | errno_t res = localtime_s(result, timep); |
| 293 | |
| 294 | if (res) { |
| 295 | return NULL; |
| 296 | } else { |
| 297 | return result; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | #else |
| 302 | #error No localtime_r() implementation for this platform is available. |
| 303 | #endif |
| 304 | #endif |
Jan Kundrát | 406e96e | 2021-12-11 19:37:33 +0100 | [diff] [blame] | 305 | |
Jan Kundrát | a9caaf7 | 2022-02-14 18:29:43 +0100 | [diff] [blame] | 306 | #ifndef HAVE_GMTIME_R |
| 307 | #ifdef _WIN32 |
| 308 | struct tm * |
| 309 | gmtime_r(const time_t *timep, struct tm *result) |
| 310 | { |
| 311 | errno_t res = gmtime_s(result, timep); |
| 312 | |
| 313 | if (res) { |
| 314 | return NULL; |
| 315 | } else { |
| 316 | return result; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | #else |
| 321 | #error No gmtime_r() implementation for this platform is available. |
| 322 | #endif |
| 323 | #endif |
| 324 | |
Jan Kundrát | 406e96e | 2021-12-11 19:37:33 +0100 | [diff] [blame] | 325 | #ifndef HAVE_DIRNAME |
| 326 | #ifdef _WIN32 |
| 327 | #include <shlwapi.h> |
| 328 | char * |
| 329 | dirname(char *path) |
| 330 | { |
| 331 | PathRemoveFileSpecA(path); |
| 332 | return path; |
| 333 | } |
| 334 | |
| 335 | #else |
| 336 | #error No dirname() implementation for this platform is available. |
| 337 | #endif |
| 338 | #endif |
Jan Kundrát | c880530 | 2021-12-11 23:40:26 +0100 | [diff] [blame] | 339 | |
| 340 | #ifndef HAVE_SETENV |
| 341 | #ifdef _WIN32 |
| 342 | int |
| 343 | setenv(const char *name, const char *value, int overwrite) |
| 344 | { |
| 345 | int errcode = 0; |
| 346 | |
| 347 | if (!overwrite) { |
| 348 | size_t envsize = 0; |
| 349 | errcode = getenv_s(&envsize, NULL, 0, name); |
| 350 | if (errcode || envsize) { |
| 351 | return errcode; |
| 352 | } |
| 353 | } |
| 354 | return _putenv_s(name, value); |
| 355 | } |
| 356 | |
| 357 | #else |
| 358 | #error No setenv() implementation for this platform is available. |
| 359 | #endif |
| 360 | #endif |