blob: bf75c747dad08f18a887894164d2a6a25becebf6 [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);
Michal Vasko45c4b1a2021-05-19 16:48:12 +0200178
Michal Vaskod304a082021-05-19 16:36:10 +0200179 return p ? p : (char *)s + strlen(s);
Michal Vasko5aa44c02020-06-29 11:47:02 +0200180}
Radek Krejci0f969882020-08-21 16:56:47 +0200181
Michal Vasko5aa44c02020-06-29 11:47:02 +0200182#endif
183
184#ifndef HAVE_GET_CURRENT_DIR_NAME
185char *
186get_current_dir_name(void)
187{
188 char tmp[_POSIX_PATH_MAX];
189 char *retval = NULL;
190
191 if (getcwd(tmp, sizeof(tmp))) {
192 retval = strdup(tmp);
193 if (!retval) {
194 errno = ENOMEM;
195 }
196 }
197
198 return retval;
199}
Radek Krejci0f969882020-08-21 16:56:47 +0200200
Michal Vasko5aa44c02020-06-29 11:47:02 +0200201#endif
Michal Vaskod304a082021-05-19 16:36:10 +0200202
203#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
204int
205pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
206{
207 int64_t nsec_diff;
208 int32_t diff;
209 struct timespec cur, dur;
210 int rc;
211
212 /* try to acquire the lock and, if we fail, sleep for 5ms. */
213 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
214 /* get real time */
215#ifdef CLOCK_REALTIME
216 clock_gettime(CLOCK_REALTIME, &cur);
217#else
218 struct timeval tv;
219
220 gettimeofday(&tv, NULL);
221 cur.tv_sec = (time_t)tv.tv_sec;
222 cur.tv_nsec = 1000L * (long)tv.tv_usec;
223#endif
224
225 /* get time diff */
226 nsec_diff = 0;
227 nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
228 nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);
229 diff = (nsec_diff ? nsec_diff / 1000000L : 0);
230
231 if (diff < 1) {
232 /* timeout */
233 break;
234 } else if (diff < 5) {
235 /* sleep until timeout */
236 dur.tv_sec = 0;
237 dur.tv_nsec = (long)diff * 1000000;
238 } else {
239 /* sleep 5 ms */
240 dur.tv_sec = 0;
241 dur.tv_nsec = 5000000;
242 }
243
244 nanosleep(&dur, NULL);
245 }
246
247 return rc;
248}
249
250#endif