blob: fe50092d02d8cd428097aa840595d631dc71f9ff [file] [log] [blame]
Michal Vasko5aa44c02020-06-29 11:47:02 +02001/**
2 * @file compat.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief compatibility functions
5 *
Michal Vaskod304a082021-05-19 16:36:10 +02006 * Copyright (c) 2021 CESNET, z.s.p.o.
Michal Vasko5aa44c02020-06-29 11:47:02 +02007 *
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 Krejcif8dc59a2020-11-25 13:47:44 +010014#define _POSIX_C_SOURCE 200809L /* fdopen, _POSIX_PATH_MAX, strdup */
Michal Vasko5aa44c02020-06-29 11:47:02 +020015#define _ISOC99_SOURCE /* vsnprintf */
Michal Vasko5aa44c02020-06-29 11:47:02 +020016
Michal Vaskod304a082021-05-19 16:36:10 +020017#include "compat.h"
18
Michal Vasko5aa44c02020-06-29 11:47:02 +020019#include <errno.h>
Michal Vaskod304a082021-05-19 16:36:10 +020020#include <inttypes.h>
Michal Vasko5aa44c02020-06-29 11:47:02 +020021#include <limits.h>
Michal Vaskod304a082021-05-19 16:36:10 +020022#include <pthread.h>
Michal Vasko5aa44c02020-06-29 11:47:02 +020023#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
Michal Vasko69730152020-10-09 16:30:07 +020026#include <string.h>
Jan Kundrát3ce107d2021-12-09 23:37:02 +010027#ifndef _MSC_VER
Michal Vaskod304a082021-05-19 16:36:10 +020028#include <sys/time.h>
Jan Kundrát3ce107d2021-12-09 23:37:02 +010029#endif
Michal Vaskod304a082021-05-19 16:36:10 +020030#include <time.h>
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include <unistd.h>
32
33#ifndef HAVE_VDPRINTF
34int
35vdprintf(int fd, const char *format, va_list ap)
36{
37 FILE *stream;
Radek Krejciafb28832020-12-01 12:16:28 +010038 int count = 0;
Michal Vasko5aa44c02020-06-29 11:47:02 +020039
40 stream = fdopen(dup(fd), "a+");
41 if (stream) {
42 count = vfprintf(stream, format, ap);
43 fclose(stream);
44 }
45 return count;
46}
Radek Krejci0f969882020-08-21 16:56:47 +020047
Michal Vasko5aa44c02020-06-29 11:47:02 +020048#endif
49
50#ifndef HAVE_ASPRINTF
51int
52asprintf(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 Krejci0f969882020-08-21 16:56:47 +020062
Michal Vasko5aa44c02020-06-29 11:47:02 +020063#endif
64
65#ifndef HAVE_VASPRINTF
66int
67vasprintf(char **strp, const char *fmt, va_list ap)
68{
69 va_list ap2;
Michal Vasko69730152020-10-09 16:30:07 +020070
Michal Vasko5aa44c02020-06-29 11:47:02 +020071 va_copy(ap2, ap);
72 int l = vsnprintf(0, 0, fmt, ap2);
Michal Vasko69730152020-10-09 16:30:07 +020073
Michal Vasko5aa44c02020-06-29 11:47:02 +020074 va_end(ap2);
75
Michal Vasko69730152020-10-09 16:30:07 +020076 if ((l < 0) || !(*strp = malloc(l + 1U))) {
Michal Vasko5aa44c02020-06-29 11:47:02 +020077 return -1;
78 }
79
80 return vsnprintf(*strp, l + 1U, fmt, ap);
81}
Radek Krejci0f969882020-08-21 16:56:47 +020082
Michal Vasko5aa44c02020-06-29 11:47:02 +020083#endif
84
Michal Vaskod304a082021-05-19 16:36:10 +020085#ifndef HAVE_GETLINE
86ssize_t
87getline(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 Vasko5aa44c02020-06-29 11:47:02 +0200128#ifndef HAVE_STRNDUP
129char *
130strndup(const char *s, size_t n)
131{
132 char *buf;
133 size_t len = 0;
134
135 /* strnlen */
Michal Vaskod989ba02020-08-24 10:59:24 +0200136 for ( ; (len < n) && (s[len] != '\0'); ++len) {}
Michal Vasko5aa44c02020-06-29 11:47:02 +0200137
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 Krejci0f969882020-08-21 16:56:47 +0200146
Michal Vasko5aa44c02020-06-29 11:47:02 +0200147#endif
148
149#ifndef HAVE_STRNSTR
150char *
151strnstr(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 Krejci0f969882020-08-21 16:56:47 +0200172
Michal Vasko5aa44c02020-06-29 11:47:02 +0200173#endif
174
Michal Vaskod304a082021-05-19 16:36:10 +0200175#ifndef HAVE_STRCHRNUL
176char *
177strchrnul(const char *s, int c)
Michal Vasko5aa44c02020-06-29 11:47:02 +0200178{
Michal Vaskod304a082021-05-19 16:36:10 +0200179 char *p = strchr(s, c);
Michal Vasko45c4b1a2021-05-19 16:48:12 +0200180
Michal Vaskod304a082021-05-19 16:36:10 +0200181 return p ? p : (char *)s + strlen(s);
Michal Vasko5aa44c02020-06-29 11:47:02 +0200182}
Radek Krejci0f969882020-08-21 16:56:47 +0200183
Michal Vasko5aa44c02020-06-29 11:47:02 +0200184#endif
185
186#ifndef HAVE_GET_CURRENT_DIR_NAME
187char *
188get_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 Krejci0f969882020-08-21 16:56:47 +0200202
Michal Vasko5aa44c02020-06-29 11:47:02 +0200203#endif
Michal Vaskod304a082021-05-19 16:36:10 +0200204
205#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
206int
207pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
208{
209 int64_t nsec_diff;
210 int32_t diff;
211 struct timespec cur, dur;
212 int rc;
213
214 /* try to acquire the lock and, if we fail, sleep for 5ms. */
215 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
216 /* get real time */
217#ifdef CLOCK_REALTIME
218 clock_gettime(CLOCK_REALTIME, &cur);
219#else
220 struct timeval tv;
221
222 gettimeofday(&tv, NULL);
223 cur.tv_sec = (time_t)tv.tv_sec;
224 cur.tv_nsec = 1000L * (long)tv.tv_usec;
225#endif
226
227 /* get time diff */
228 nsec_diff = 0;
229 nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
230 nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);
231 diff = (nsec_diff ? nsec_diff / 1000000L : 0);
232
233 if (diff < 1) {
234 /* timeout */
235 break;
236 } else if (diff < 5) {
237 /* sleep until timeout */
238 dur.tv_sec = 0;
239 dur.tv_nsec = (long)diff * 1000000;
240 } else {
241 /* sleep 5 ms */
242 dur.tv_sec = 0;
243 dur.tv_nsec = 5000000;
244 }
245
246 nanosleep(&dur, NULL);
247 }
248
249 return rc;
250}
251
252#endif
Jan Kundráta5b3b992021-12-10 16:13:57 +0100253
254#ifndef HAVE_REALPATH
255#ifdef _WIN32
256char *
257realpath(const char *path, char *resolved_path)
258{
259 char *resolved = _fullpath(resolved_path, path, PATH_MAX);
260
261 if ((_access(resolved, 0) == -1) && (errno == ENOENT)) {
262 return NULL;
263 }
264 return resolved;
265}
266
267#else
268#error No realpath() implementation for this platform is available.
269#endif
270#endif
Jan Kundrát447970d2021-12-14 16:18:16 +0100271
272#ifndef HAVE_LOCALTIME_R
273#ifdef _WIN32
274struct tm *
275localtime_r(const time_t *timep, struct tm *result)
276{
277 errno_t res = localtime_s(result, timep);
278
279 if (res) {
280 return NULL;
281 } else {
282 return result;
283 }
284}
285
286#else
287#error No localtime_r() implementation for this platform is available.
288#endif
289#endif