blob: 9a7456c997fcc84789aa30484f37332526ca131d [file] [log] [blame]
Michal Vasko9e8ac262020-04-07 13:06:45 +02001/**
2 * @file compat.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief compatibility functions
5 *
Michal Vaskod8a74192023-02-06 15:51:50 +01006 * Copyright (c) 2021 - 2023 CESNET, z.s.p.o.
Michal Vasko9e8ac262020-04-07 13:06:45 +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 */
Michal Vasko7a20d2e2021-05-19 16:40:23 +020014#define _POSIX_C_SOURCE 200809L /* fdopen, _POSIX_PATH_MAX, strdup */
Michal Vasko9e8ac262020-04-07 13:06:45 +020015#define _ISOC99_SOURCE /* vsnprintf */
Michal Vasko7a20d2e2021-05-19 16:40:23 +020016
17#include "compat.h"
Michal Vasko9e8ac262020-04-07 13:06:45 +020018
19#include <errno.h>
Michal Vasko7a20d2e2021-05-19 16:40:23 +020020#include <inttypes.h>
Michal Vasko9e8ac262020-04-07 13:06:45 +020021#include <limits.h>
Michal Vasko7a20d2e2021-05-19 16:40:23 +020022#include <pthread.h>
Michal Vasko9e8ac262020-04-07 13:06:45 +020023#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
Michal Vasko7a20d2e2021-05-19 16:40:23 +020026#include <string.h>
27#include <sys/time.h>
28#include <time.h>
Michal Vasko9e8ac262020-04-07 13:06:45 +020029#include <unistd.h>
30
Michal Vaskod8a74192023-02-06 15:51:50 +010031#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
32int
33pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
34{
35 int64_t nsec_diff;
Michal Vaskod8a74192023-02-06 15:51:50 +010036 struct timespec cur, dur;
37 int rc;
38
39 /* try to acquire the lock and, if we fail, sleep for 5ms. */
40 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
41 /* get time */
42 clock_gettime(COMPAT_CLOCK_ID, &cur);
43
44 /* get time diff */
45 nsec_diff = 0;
46 nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
47 nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);
Michal Vaskod8a74192023-02-06 15:51:50 +010048
Jan Kundrát922885a2023-06-05 18:12:39 +020049 if (nsec_diff <= 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +010050 /* timeout */
Jan Kundrát2a2b6de2023-06-05 18:19:49 +020051 rc = ETIMEDOUT;
Michal Vaskod8a74192023-02-06 15:51:50 +010052 break;
Jan Kundrát922885a2023-06-05 18:12:39 +020053 } else if (nsec_diff < 5000000) {
Michal Vaskod8a74192023-02-06 15:51:50 +010054 /* sleep until timeout */
55 dur.tv_sec = 0;
Jan Kundrát922885a2023-06-05 18:12:39 +020056 dur.tv_nsec = nsec_diff;
Michal Vaskod8a74192023-02-06 15:51:50 +010057 } else {
58 /* sleep 5 ms */
59 dur.tv_sec = 0;
60 dur.tv_nsec = 5000000;
61 }
62
63 nanosleep(&dur, NULL);
64 }
65
66 return rc;
67}
68
69#endif
70
71#ifndef HAVE_PTHREAD_MUTEX_CLOCKLOCK
72int
73pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime)
74{
75 /* only real time supported without this function */
76 if (clockid != CLOCK_REALTIME) {
77 return EINVAL;
78 }
79
80 return pthread_mutex_timedlock(mutex, abstime);
81}
82
83#endif
84
85#ifndef HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
86int
87pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime)
88{
89 /* only real time supported without this function */
90 if (clockid != CLOCK_REALTIME) {
91 return EINVAL;
92 }
93
94 return pthread_rwlock_timedrdlock(rwlock, abstime);
95}
96
97#endif
98
99#ifndef HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
100int
101pthread_rwlock_clockwrlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime)
102{
103 /* only real time supported without this function */
104 if (clockid != CLOCK_REALTIME) {
105 return EINVAL;
106 }
107
108 return pthread_rwlock_timedwrlock(rwlock, abstime);
109}
110
111#endif
112
113#ifndef HAVE_PTHREAD_COND_CLOCKWAIT
114int
115pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t UNUSED(clockid),
116 const struct timespec *abstime)
117{
118 /* assume the correct clock is set during cond init */
119 return pthread_cond_timedwait(cond, mutex, abstime);
120}
121
122#endif
123
Michal Vasko9e8ac262020-04-07 13:06:45 +0200124#ifndef HAVE_VDPRINTF
125int
126vdprintf(int fd, const char *format, va_list ap)
127{
128 FILE *stream;
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200129 int count = 0;
Michal Vasko9e8ac262020-04-07 13:06:45 +0200130
131 stream = fdopen(dup(fd), "a+");
132 if (stream) {
133 count = vfprintf(stream, format, ap);
134 fclose(stream);
135 }
136 return count;
137}
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200138
Michal Vasko9e8ac262020-04-07 13:06:45 +0200139#endif
140
141#ifndef HAVE_ASPRINTF
142int
143asprintf(char **strp, const char *fmt, ...)
144{
145 int ret;
146 va_list ap;
147
148 va_start(ap, fmt);
149 ret = vasprintf(strp, fmt, ap);
150 va_end(ap);
151 return ret;
152}
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200153
Michal Vasko9e8ac262020-04-07 13:06:45 +0200154#endif
155
156#ifndef HAVE_VASPRINTF
157int
158vasprintf(char **strp, const char *fmt, va_list ap)
159{
160 va_list ap2;
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200161
Michal Vasko9e8ac262020-04-07 13:06:45 +0200162 va_copy(ap2, ap);
163 int l = vsnprintf(0, 0, fmt, ap2);
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200164
Michal Vasko9e8ac262020-04-07 13:06:45 +0200165 va_end(ap2);
166
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200167 if ((l < 0) || !(*strp = malloc(l + 1U))) {
Michal Vasko9e8ac262020-04-07 13:06:45 +0200168 return -1;
169 }
170
171 return vsnprintf(*strp, l + 1U, fmt, ap);
172}
Michal Vasko9e8ac262020-04-07 13:06:45 +0200173
Michal Vasko9e8ac262020-04-07 13:06:45 +0200174#endif
175
176#ifndef HAVE_GETLINE
177ssize_t
178getline(char **lineptr, size_t *n, FILE *stream)
179{
180 static char line[256];
181 char *ptr;
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200182 ssize_t len;
Michal Vasko9e8ac262020-04-07 13:06:45 +0200183
184 if (!lineptr || !n) {
185 errno = EINVAL;
186 return -1;
187 }
188
189 if (ferror(stream) || feof(stream)) {
190 return -1;
191 }
192
193 if (!fgets(line, 256, stream)) {
194 return -1;
195 }
196
197 ptr = strchr(line, '\n');
198 if (ptr) {
199 *ptr = '\0';
200 }
201
202 len = strlen(line);
203
204 if (len + 1 < 256) {
205 ptr = realloc(*lineptr, 256);
206 if (!ptr) {
207 return -1;
208 }
209 *lineptr = ptr;
210 *n = 256;
211 }
212
213 strcpy(*lineptr, line);
214 return len;
215}
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200216
217#endif
218
219#ifndef HAVE_STRNDUP
220char *
221strndup(const char *s, size_t n)
222{
223 char *buf;
224 size_t len = 0;
225
226 /* strnlen */
227 for ( ; (len < n) && (s[len] != '\0'); ++len) {}
228
229 if (!(buf = malloc(len + 1U))) {
230 return NULL;
231 }
232
233 memcpy(buf, s, len);
234 buf[len] = '\0';
235 return buf;
236}
237
238#endif
239
240#ifndef HAVE_STRNSTR
241char *
242strnstr(const char *s, const char *find, size_t slen)
243{
244 char c, sc;
245 size_t len;
246
247 if ((c = *find++) != '\0') {
248 len = strlen(find);
249 do {
250 do {
251 if ((slen-- < 1) || ((sc = *s++) == '\0')) {
252 return NULL;
253 }
254 } while (sc != c);
255 if (len > slen) {
256 return NULL;
257 }
258 } while (strncmp(s, find, len));
259 s--;
260 }
261 return (char *)s;
262}
263
264#endif
265
266#ifndef HAVE_STRCHRNUL
267char *
268strchrnul(const char *s, int c)
269{
270 char *p = strchr(s, c);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200271
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200272 return p ? p : (char *)s + strlen(s);
273}
274
Michal Vasko9e8ac262020-04-07 13:06:45 +0200275#endif
276
277#ifndef HAVE_GET_CURRENT_DIR_NAME
278char *
279get_current_dir_name(void)
280{
281 char tmp[_POSIX_PATH_MAX];
282 char *retval = NULL;
283
284 if (getcwd(tmp, sizeof(tmp))) {
285 retval = strdup(tmp);
286 if (!retval) {
287 errno = ENOMEM;
288 }
289 }
290
291 return retval;
292}
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200293
294#endif