blob: 71e81766693b87eeed27f89416e6e7838286f0ef [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>
Michal Vaskod304a082021-05-19 16:36:10 +020027#include <sys/time.h>
28#include <time.h>
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include <unistd.h>
30
31#ifndef HAVE_VDPRINTF
32int
33vdprintf(int fd, const char *format, va_list ap)
34{
35 FILE *stream;
Radek Krejciafb28832020-12-01 12:16:28 +010036 int count = 0;
Michal Vasko5aa44c02020-06-29 11:47:02 +020037
38 stream = fdopen(dup(fd), "a+");
39 if (stream) {
40 count = vfprintf(stream, format, ap);
41 fclose(stream);
42 }
43 return count;
44}
Radek Krejci0f969882020-08-21 16:56:47 +020045
Michal Vasko5aa44c02020-06-29 11:47:02 +020046#endif
47
48#ifndef HAVE_ASPRINTF
49int
50asprintf(char **strp, const char *fmt, ...)
51{
52 int ret;
53 va_list ap;
54
55 va_start(ap, fmt);
56 ret = vasprintf(strp, fmt, ap);
57 va_end(ap);
58 return ret;
59}
Radek Krejci0f969882020-08-21 16:56:47 +020060
Michal Vasko5aa44c02020-06-29 11:47:02 +020061#endif
62
63#ifndef HAVE_VASPRINTF
64int
65vasprintf(char **strp, const char *fmt, va_list ap)
66{
67 va_list ap2;
Michal Vasko69730152020-10-09 16:30:07 +020068
Michal Vasko5aa44c02020-06-29 11:47:02 +020069 va_copy(ap2, ap);
70 int l = vsnprintf(0, 0, fmt, ap2);
Michal Vasko69730152020-10-09 16:30:07 +020071
Michal Vasko5aa44c02020-06-29 11:47:02 +020072 va_end(ap2);
73
Michal Vasko69730152020-10-09 16:30:07 +020074 if ((l < 0) || !(*strp = malloc(l + 1U))) {
Michal Vasko5aa44c02020-06-29 11:47:02 +020075 return -1;
76 }
77
78 return vsnprintf(*strp, l + 1U, fmt, ap);
79}
Radek Krejci0f969882020-08-21 16:56:47 +020080
Michal Vasko5aa44c02020-06-29 11:47:02 +020081#endif
82
Michal Vaskod304a082021-05-19 16:36:10 +020083#ifndef HAVE_GETLINE
84ssize_t
85getline(char **lineptr, size_t *n, FILE *stream)
86{
87 static char line[256];
88 char *ptr;
89 ssize_t len;
90
91 if (!lineptr || !n) {
92 errno = EINVAL;
93 return -1;
94 }
95
96 if (ferror(stream) || feof(stream)) {
97 return -1;
98 }
99
100 if (!fgets(line, 256, stream)) {
101 return -1;
102 }
103
104 ptr = strchr(line, '\n');
105 if (ptr) {
106 *ptr = '\0';
107 }
108
109 len = strlen(line);
110
111 if (len + 1 < 256) {
112 ptr = realloc(*lineptr, 256);
113 if (!ptr) {
114 return -1;
115 }
116 *lineptr = ptr;
117 *n = 256;
118 }
119
120 strcpy(*lineptr, line);
121 return len;
122}
123
124#endif
125
Michal Vasko5aa44c02020-06-29 11:47:02 +0200126#ifndef HAVE_STRNDUP
127char *
128strndup(const char *s, size_t n)
129{
130 char *buf;
131 size_t len = 0;
132
133 /* strnlen */
Michal Vaskod989ba02020-08-24 10:59:24 +0200134 for ( ; (len < n) && (s[len] != '\0'); ++len) {}
Michal Vasko5aa44c02020-06-29 11:47:02 +0200135
136 if (!(buf = malloc(len + 1U))) {
137 return NULL;
138 }
139
140 memcpy(buf, s, len);
141 buf[len] = '\0';
142 return buf;
143}
Radek Krejci0f969882020-08-21 16:56:47 +0200144
Michal Vasko5aa44c02020-06-29 11:47:02 +0200145#endif
146
147#ifndef HAVE_STRNSTR
148char *
149strnstr(const char *s, const char *find, size_t slen)
150{
151 char c, sc;
152 size_t len;
153
154 if ((c = *find++) != '\0') {
155 len = strlen(find);
156 do {
157 do {
158 if ((slen-- < 1) || ((sc = *s++) == '\0')) {
159 return NULL;
160 }
161 } while (sc != c);
162 if (len > slen) {
163 return NULL;
164 }
165 } while (strncmp(s, find, len));
166 s--;
167 }
168 return (char *)s;
169}
Radek Krejci0f969882020-08-21 16:56:47 +0200170
Michal Vasko5aa44c02020-06-29 11:47:02 +0200171#endif
172
Michal Vaskod304a082021-05-19 16:36:10 +0200173#ifndef HAVE_STRCHRNUL
174char *
175strchrnul(const char *s, int c)
Michal Vasko5aa44c02020-06-29 11:47:02 +0200176{
Michal Vaskod304a082021-05-19 16:36:10 +0200177 char *p = strchr(s, c);
178 return p ? p : (char *)s + strlen(s);
Michal Vasko5aa44c02020-06-29 11:47:02 +0200179}
Radek Krejci0f969882020-08-21 16:56:47 +0200180
Michal Vasko5aa44c02020-06-29 11:47:02 +0200181#endif
182
183#ifndef HAVE_GET_CURRENT_DIR_NAME
184char *
185get_current_dir_name(void)
186{
187 char tmp[_POSIX_PATH_MAX];
188 char *retval = NULL;
189
190 if (getcwd(tmp, sizeof(tmp))) {
191 retval = strdup(tmp);
192 if (!retval) {
193 errno = ENOMEM;
194 }
195 }
196
197 return retval;
198}
Radek Krejci0f969882020-08-21 16:56:47 +0200199
Michal Vasko5aa44c02020-06-29 11:47:02 +0200200#endif
Michal Vaskod304a082021-05-19 16:36:10 +0200201
202#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
203int
204pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
205{
206 int64_t nsec_diff;
207 int32_t diff;
208 struct timespec cur, dur;
209 int rc;
210
211 /* try to acquire the lock and, if we fail, sleep for 5ms. */
212 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
213 /* get real time */
214#ifdef CLOCK_REALTIME
215 clock_gettime(CLOCK_REALTIME, &cur);
216#else
217 struct timeval tv;
218
219 gettimeofday(&tv, NULL);
220 cur.tv_sec = (time_t)tv.tv_sec;
221 cur.tv_nsec = 1000L * (long)tv.tv_usec;
222#endif
223
224 /* get time diff */
225 nsec_diff = 0;
226 nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
227 nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);
228 diff = (nsec_diff ? nsec_diff / 1000000L : 0);
229
230 if (diff < 1) {
231 /* timeout */
232 break;
233 } else if (diff < 5) {
234 /* sleep until timeout */
235 dur.tv_sec = 0;
236 dur.tv_nsec = (long)diff * 1000000;
237 } else {
238 /* sleep 5 ms */
239 dur.tv_sec = 0;
240 dur.tv_nsec = 5000000;
241 }
242
243 nanosleep(&dur, NULL);
244 }
245
246 return rc;
247}
248
249#endif