blob: 4491680dfec1ae2c73dccbffaa75b0c866e549b4 [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;
36 int32_t diff;
37 struct timespec cur, dur;
38 int rc;
39
40 /* try to acquire the lock and, if we fail, sleep for 5ms. */
41 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
42 /* get time */
43 clock_gettime(COMPAT_CLOCK_ID, &cur);
44
45 /* get time diff */
46 nsec_diff = 0;
47 nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
48 nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);
49 diff = (nsec_diff ? nsec_diff / 1000000L : 0);
50
51 if (diff < 1) {
52 /* timeout */
53 break;
54 } else if (diff < 5) {
55 /* sleep until timeout */
56 dur.tv_sec = 0;
57 dur.tv_nsec = (long)diff * 1000000;
58 } else {
59 /* sleep 5 ms */
60 dur.tv_sec = 0;
61 dur.tv_nsec = 5000000;
62 }
63
64 nanosleep(&dur, NULL);
65 }
66
67 return rc;
68}
69
70#endif
71
72#ifndef HAVE_PTHREAD_MUTEX_CLOCKLOCK
73int
74pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime)
75{
76 /* only real time supported without this function */
77 if (clockid != CLOCK_REALTIME) {
78 return EINVAL;
79 }
80
81 return pthread_mutex_timedlock(mutex, abstime);
82}
83
84#endif
85
86#ifndef HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
87int
88pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime)
89{
90 /* only real time supported without this function */
91 if (clockid != CLOCK_REALTIME) {
92 return EINVAL;
93 }
94
95 return pthread_rwlock_timedrdlock(rwlock, abstime);
96}
97
98#endif
99
100#ifndef HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
101int
102pthread_rwlock_clockwrlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime)
103{
104 /* only real time supported without this function */
105 if (clockid != CLOCK_REALTIME) {
106 return EINVAL;
107 }
108
109 return pthread_rwlock_timedwrlock(rwlock, abstime);
110}
111
112#endif
113
114#ifndef HAVE_PTHREAD_COND_CLOCKWAIT
115int
116pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t UNUSED(clockid),
117 const struct timespec *abstime)
118{
119 /* assume the correct clock is set during cond init */
120 return pthread_cond_timedwait(cond, mutex, abstime);
121}
122
123#endif
124
Michal Vasko9e8ac262020-04-07 13:06:45 +0200125#ifndef HAVE_VDPRINTF
126int
127vdprintf(int fd, const char *format, va_list ap)
128{
129 FILE *stream;
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200130 int count = 0;
Michal Vasko9e8ac262020-04-07 13:06:45 +0200131
132 stream = fdopen(dup(fd), "a+");
133 if (stream) {
134 count = vfprintf(stream, format, ap);
135 fclose(stream);
136 }
137 return count;
138}
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200139
Michal Vasko9e8ac262020-04-07 13:06:45 +0200140#endif
141
142#ifndef HAVE_ASPRINTF
143int
144asprintf(char **strp, const char *fmt, ...)
145{
146 int ret;
147 va_list ap;
148
149 va_start(ap, fmt);
150 ret = vasprintf(strp, fmt, ap);
151 va_end(ap);
152 return ret;
153}
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200154
Michal Vasko9e8ac262020-04-07 13:06:45 +0200155#endif
156
157#ifndef HAVE_VASPRINTF
158int
159vasprintf(char **strp, const char *fmt, va_list ap)
160{
161 va_list ap2;
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200162
Michal Vasko9e8ac262020-04-07 13:06:45 +0200163 va_copy(ap2, ap);
164 int l = vsnprintf(0, 0, fmt, ap2);
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200165
Michal Vasko9e8ac262020-04-07 13:06:45 +0200166 va_end(ap2);
167
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200168 if ((l < 0) || !(*strp = malloc(l + 1U))) {
Michal Vasko9e8ac262020-04-07 13:06:45 +0200169 return -1;
170 }
171
172 return vsnprintf(*strp, l + 1U, fmt, ap);
173}
Michal Vasko9e8ac262020-04-07 13:06:45 +0200174
Michal Vasko9e8ac262020-04-07 13:06:45 +0200175#endif
176
177#ifndef HAVE_GETLINE
178ssize_t
179getline(char **lineptr, size_t *n, FILE *stream)
180{
181 static char line[256];
182 char *ptr;
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200183 ssize_t len;
Michal Vasko9e8ac262020-04-07 13:06:45 +0200184
185 if (!lineptr || !n) {
186 errno = EINVAL;
187 return -1;
188 }
189
190 if (ferror(stream) || feof(stream)) {
191 return -1;
192 }
193
194 if (!fgets(line, 256, stream)) {
195 return -1;
196 }
197
198 ptr = strchr(line, '\n');
199 if (ptr) {
200 *ptr = '\0';
201 }
202
203 len = strlen(line);
204
205 if (len + 1 < 256) {
206 ptr = realloc(*lineptr, 256);
207 if (!ptr) {
208 return -1;
209 }
210 *lineptr = ptr;
211 *n = 256;
212 }
213
214 strcpy(*lineptr, line);
215 return len;
216}
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200217
218#endif
219
220#ifndef HAVE_STRNDUP
221char *
222strndup(const char *s, size_t n)
223{
224 char *buf;
225 size_t len = 0;
226
227 /* strnlen */
228 for ( ; (len < n) && (s[len] != '\0'); ++len) {}
229
230 if (!(buf = malloc(len + 1U))) {
231 return NULL;
232 }
233
234 memcpy(buf, s, len);
235 buf[len] = '\0';
236 return buf;
237}
238
239#endif
240
241#ifndef HAVE_STRNSTR
242char *
243strnstr(const char *s, const char *find, size_t slen)
244{
245 char c, sc;
246 size_t len;
247
248 if ((c = *find++) != '\0') {
249 len = strlen(find);
250 do {
251 do {
252 if ((slen-- < 1) || ((sc = *s++) == '\0')) {
253 return NULL;
254 }
255 } while (sc != c);
256 if (len > slen) {
257 return NULL;
258 }
259 } while (strncmp(s, find, len));
260 s--;
261 }
262 return (char *)s;
263}
264
265#endif
266
267#ifndef HAVE_STRCHRNUL
268char *
269strchrnul(const char *s, int c)
270{
271 char *p = strchr(s, c);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200272
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200273 return p ? p : (char *)s + strlen(s);
274}
275
Michal Vasko9e8ac262020-04-07 13:06:45 +0200276#endif
277
278#ifndef HAVE_GET_CURRENT_DIR_NAME
279char *
280get_current_dir_name(void)
281{
282 char tmp[_POSIX_PATH_MAX];
283 char *retval = NULL;
284
285 if (getcwd(tmp, sizeof(tmp))) {
286 retval = strdup(tmp);
287 if (!retval) {
288 errno = ENOMEM;
289 }
290 }
291
292 return retval;
293}
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200294
295#endif