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 | { |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame^] | 89 | static char chunk[256]; |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 90 | char *ptr; |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame^] | 91 | ssize_t len, written; |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 92 | |
| 93 | if (!lineptr || !n) { |
| 94 | errno = EINVAL; |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | if (ferror(stream) || feof(stream)) { |
| 99 | return -1; |
| 100 | } |
| 101 | |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame^] | 102 | *n = *lineptr ? *n : 0; |
| 103 | written = 0; |
| 104 | while (fgets(chunk, sizeof(chunk), stream)) { |
| 105 | len = strlen(chunk); |
| 106 | if (written + len > *n) { |
| 107 | ptr = realloc(*lineptr, *n + sizeof(chunk)); |
| 108 | if (!ptr) { |
| 109 | return -1; |
| 110 | } |
| 111 | *lineptr = ptr; |
| 112 | *n = *n + sizeof(chunk); |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 113 | } |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame^] | 114 | memcpy(*lineptr + written, &chunk, len); |
| 115 | written += len; |
| 116 | if ((*lineptr)[written - 1] == '\n') { |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | if (written) { |
| 121 | (*lineptr)[written] = '\0'; |
| 122 | } else { |
| 123 | written = -1; |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 124 | } |
| 125 | |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame^] | 126 | return written; |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | #endif |
| 130 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 131 | #ifndef HAVE_STRNDUP |
| 132 | char * |
| 133 | strndup(const char *s, size_t n) |
| 134 | { |
| 135 | char *buf; |
| 136 | size_t len = 0; |
| 137 | |
| 138 | /* strnlen */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 139 | for ( ; (len < n) && (s[len] != '\0'); ++len) {} |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 140 | |
| 141 | if (!(buf = malloc(len + 1U))) { |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | memcpy(buf, s, len); |
| 146 | buf[len] = '\0'; |
| 147 | return buf; |
| 148 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 149 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 150 | #endif |
| 151 | |
| 152 | #ifndef HAVE_STRNSTR |
| 153 | char * |
| 154 | strnstr(const char *s, const char *find, size_t slen) |
| 155 | { |
| 156 | char c, sc; |
| 157 | size_t len; |
| 158 | |
| 159 | if ((c = *find++) != '\0') { |
| 160 | len = strlen(find); |
| 161 | do { |
| 162 | do { |
| 163 | if ((slen-- < 1) || ((sc = *s++) == '\0')) { |
| 164 | return NULL; |
| 165 | } |
| 166 | } while (sc != c); |
| 167 | if (len > slen) { |
| 168 | return NULL; |
| 169 | } |
| 170 | } while (strncmp(s, find, len)); |
| 171 | s--; |
| 172 | } |
| 173 | return (char *)s; |
| 174 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 175 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 176 | #endif |
| 177 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 178 | #ifndef HAVE_STRCHRNUL |
| 179 | char * |
| 180 | strchrnul(const char *s, int c) |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 181 | { |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 182 | char *p = strchr(s, c); |
Michal Vasko | 45c4b1a | 2021-05-19 16:48:12 +0200 | [diff] [blame] | 183 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 184 | return p ? p : (char *)s + strlen(s); |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 185 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 186 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 187 | #endif |
| 188 | |
| 189 | #ifndef HAVE_GET_CURRENT_DIR_NAME |
| 190 | char * |
| 191 | get_current_dir_name(void) |
| 192 | { |
| 193 | char tmp[_POSIX_PATH_MAX]; |
| 194 | char *retval = NULL; |
| 195 | |
| 196 | if (getcwd(tmp, sizeof(tmp))) { |
| 197 | retval = strdup(tmp); |
| 198 | if (!retval) { |
| 199 | errno = ENOMEM; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return retval; |
| 204 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 205 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 206 | #endif |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 207 | |
Jan Kundrát | e235414 | 2021-12-10 16:29:21 +0100 | [diff] [blame] | 208 | #ifndef _MSC_VER |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 209 | #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK |
| 210 | int |
| 211 | pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime) |
| 212 | { |
| 213 | int64_t nsec_diff; |
| 214 | int32_t diff; |
| 215 | struct timespec cur, dur; |
| 216 | int rc; |
| 217 | |
| 218 | /* try to acquire the lock and, if we fail, sleep for 5ms. */ |
| 219 | while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) { |
| 220 | /* get real time */ |
| 221 | #ifdef CLOCK_REALTIME |
| 222 | clock_gettime(CLOCK_REALTIME, &cur); |
| 223 | #else |
| 224 | struct timeval tv; |
| 225 | |
| 226 | gettimeofday(&tv, NULL); |
| 227 | cur.tv_sec = (time_t)tv.tv_sec; |
| 228 | cur.tv_nsec = 1000L * (long)tv.tv_usec; |
| 229 | #endif |
| 230 | |
| 231 | /* get time diff */ |
| 232 | nsec_diff = 0; |
| 233 | nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L; |
| 234 | nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec); |
| 235 | diff = (nsec_diff ? nsec_diff / 1000000L : 0); |
| 236 | |
| 237 | if (diff < 1) { |
| 238 | /* timeout */ |
| 239 | break; |
| 240 | } else if (diff < 5) { |
| 241 | /* sleep until timeout */ |
| 242 | dur.tv_sec = 0; |
| 243 | dur.tv_nsec = (long)diff * 1000000; |
| 244 | } else { |
| 245 | /* sleep 5 ms */ |
| 246 | dur.tv_sec = 0; |
| 247 | dur.tv_nsec = 5000000; |
| 248 | } |
| 249 | |
| 250 | nanosleep(&dur, NULL); |
| 251 | } |
| 252 | |
| 253 | return rc; |
| 254 | } |
| 255 | |
| 256 | #endif |
Jan Kundrát | e235414 | 2021-12-10 16:29:21 +0100 | [diff] [blame] | 257 | #endif |
Jan Kundrát | 147a72c | 2021-12-10 16:13:57 +0100 | [diff] [blame] | 258 | |
| 259 | #ifndef HAVE_REALPATH |
| 260 | #ifdef _WIN32 |
| 261 | char * |
| 262 | realpath(const char *path, char *resolved_path) |
| 263 | { |
| 264 | char *resolved = _fullpath(resolved_path, path, PATH_MAX); |
| 265 | |
| 266 | if ((_access(resolved, 0) == -1) && (errno == ENOENT)) { |
| 267 | return NULL; |
| 268 | } |
| 269 | return resolved; |
| 270 | } |
| 271 | |
Donatas Abraitis | d5d533c | 2022-06-10 08:22:25 +0300 | [diff] [blame] | 272 | #elif defined (__NetBSD__) |
| 273 | char * |
| 274 | realpath(const char *path, char *resolved_path) |
| 275 | { |
| 276 | ssize_t nbytes; |
| 277 | |
| 278 | nbytes = readlink(path, resolved_path, PATH_MAX); |
| 279 | if (nbytes == -1) { |
| 280 | return NULL; |
| 281 | } |
| 282 | return resolved_path; |
| 283 | } |
| 284 | |
Jan Kundrát | 147a72c | 2021-12-10 16:13:57 +0100 | [diff] [blame] | 285 | #else |
| 286 | #error No realpath() implementation for this platform is available. |
| 287 | #endif |
| 288 | #endif |
Jan Kundrát | 9190763 | 2021-12-14 16:18:16 +0100 | [diff] [blame] | 289 | |
| 290 | #ifndef HAVE_LOCALTIME_R |
| 291 | #ifdef _WIN32 |
| 292 | struct tm * |
| 293 | localtime_r(const time_t *timep, struct tm *result) |
| 294 | { |
| 295 | errno_t res = localtime_s(result, timep); |
| 296 | |
| 297 | if (res) { |
| 298 | return NULL; |
| 299 | } else { |
| 300 | return result; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | #else |
| 305 | #error No localtime_r() implementation for this platform is available. |
| 306 | #endif |
| 307 | #endif |
Jan Kundrát | 406e96e | 2021-12-11 19:37:33 +0100 | [diff] [blame] | 308 | |
Jan Kundrát | a9caaf7 | 2022-02-14 18:29:43 +0100 | [diff] [blame] | 309 | #ifndef HAVE_GMTIME_R |
| 310 | #ifdef _WIN32 |
| 311 | struct tm * |
| 312 | gmtime_r(const time_t *timep, struct tm *result) |
| 313 | { |
| 314 | errno_t res = gmtime_s(result, timep); |
| 315 | |
| 316 | if (res) { |
| 317 | return NULL; |
| 318 | } else { |
| 319 | return result; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | #else |
| 324 | #error No gmtime_r() implementation for this platform is available. |
| 325 | #endif |
| 326 | #endif |
| 327 | |
Jan Kundrát | c880530 | 2021-12-11 23:40:26 +0100 | [diff] [blame] | 328 | #ifndef HAVE_SETENV |
| 329 | #ifdef _WIN32 |
| 330 | int |
| 331 | setenv(const char *name, const char *value, int overwrite) |
| 332 | { |
| 333 | int errcode = 0; |
| 334 | |
| 335 | if (!overwrite) { |
| 336 | size_t envsize = 0; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 337 | |
Jan Kundrát | c880530 | 2021-12-11 23:40:26 +0100 | [diff] [blame] | 338 | errcode = getenv_s(&envsize, NULL, 0, name); |
| 339 | if (errcode || envsize) { |
| 340 | return errcode; |
| 341 | } |
| 342 | } |
| 343 | return _putenv_s(name, value); |
| 344 | } |
| 345 | |
| 346 | #else |
| 347 | #error No setenv() implementation for this platform is available. |
| 348 | #endif |
| 349 | #endif |