blob: c455545ae926f7c273d9fd24c214b1bbe9023112 [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 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
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 */
14#include "compat.h"
15
Radek Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* fdopen, _POSIX_PATH_MAX, strdup */
Michal Vasko5aa44c02020-06-29 11:47:02 +020017#define _ISOC99_SOURCE /* vsnprintf */
Michal Vasko5aa44c02020-06-29 11:47:02 +020018
19#include <errno.h>
20#include <limits.h>
Michal Vasko5aa44c02020-06-29 11:47:02 +020021#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
Michal Vasko69730152020-10-09 16:30:07 +020024#include <string.h>
Michal Vasko5aa44c02020-06-29 11:47:02 +020025#include <unistd.h>
26
27#ifndef HAVE_VDPRINTF
28int
29vdprintf(int fd, const char *format, va_list ap)
30{
31 FILE *stream;
Radek Krejciafb28832020-12-01 12:16:28 +010032 int count = 0;
Michal Vasko5aa44c02020-06-29 11:47:02 +020033
34 stream = fdopen(dup(fd), "a+");
35 if (stream) {
36 count = vfprintf(stream, format, ap);
37 fclose(stream);
38 }
39 return count;
40}
Radek Krejci0f969882020-08-21 16:56:47 +020041
Michal Vasko5aa44c02020-06-29 11:47:02 +020042#endif
43
44#ifndef HAVE_ASPRINTF
45int
46asprintf(char **strp, const char *fmt, ...)
47{
48 int ret;
49 va_list ap;
50
51 va_start(ap, fmt);
52 ret = vasprintf(strp, fmt, ap);
53 va_end(ap);
54 return ret;
55}
Radek Krejci0f969882020-08-21 16:56:47 +020056
Michal Vasko5aa44c02020-06-29 11:47:02 +020057#endif
58
59#ifndef HAVE_VASPRINTF
60int
61vasprintf(char **strp, const char *fmt, va_list ap)
62{
63 va_list ap2;
Michal Vasko69730152020-10-09 16:30:07 +020064
Michal Vasko5aa44c02020-06-29 11:47:02 +020065 va_copy(ap2, ap);
66 int l = vsnprintf(0, 0, fmt, ap2);
Michal Vasko69730152020-10-09 16:30:07 +020067
Michal Vasko5aa44c02020-06-29 11:47:02 +020068 va_end(ap2);
69
Michal Vasko69730152020-10-09 16:30:07 +020070 if ((l < 0) || !(*strp = malloc(l + 1U))) {
Michal Vasko5aa44c02020-06-29 11:47:02 +020071 return -1;
72 }
73
74 return vsnprintf(*strp, l + 1U, fmt, ap);
75}
Radek Krejci0f969882020-08-21 16:56:47 +020076
Michal Vasko5aa44c02020-06-29 11:47:02 +020077#endif
78
79#ifndef HAVE_STRNDUP
80char *
81strndup(const char *s, size_t n)
82{
83 char *buf;
84 size_t len = 0;
85
86 /* strnlen */
Michal Vaskod989ba02020-08-24 10:59:24 +020087 for ( ; (len < n) && (s[len] != '\0'); ++len) {}
Michal Vasko5aa44c02020-06-29 11:47:02 +020088
89 if (!(buf = malloc(len + 1U))) {
90 return NULL;
91 }
92
93 memcpy(buf, s, len);
94 buf[len] = '\0';
95 return buf;
96}
Radek Krejci0f969882020-08-21 16:56:47 +020097
Michal Vasko5aa44c02020-06-29 11:47:02 +020098#endif
99
100#ifndef HAVE_STRNSTR
101char *
102strnstr(const char *s, const char *find, size_t slen)
103{
104 char c, sc;
105 size_t len;
106
107 if ((c = *find++) != '\0') {
108 len = strlen(find);
109 do {
110 do {
111 if ((slen-- < 1) || ((sc = *s++) == '\0')) {
112 return NULL;
113 }
114 } while (sc != c);
115 if (len > slen) {
116 return NULL;
117 }
118 } while (strncmp(s, find, len));
119 s--;
120 }
121 return (char *)s;
122}
Radek Krejci0f969882020-08-21 16:56:47 +0200123
Michal Vasko5aa44c02020-06-29 11:47:02 +0200124#endif
125
126#ifndef HAVE_GETLINE
127ssize_t
128getline(char **lineptr, size_t *n, FILE *stream)
129{
130 static char line[256];
131 char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200132 ssize_t len;
Michal Vasko5aa44c02020-06-29 11:47:02 +0200133
134 if (!lineptr || !n) {
135 errno = EINVAL;
136 return -1;
137 }
138
139 if (ferror(stream) || feof(stream)) {
140 return -1;
141 }
142
143 if (!fgets(line, 256, stream)) {
144 return -1;
145 }
146
147 ptr = strchr(line, '\n');
148 if (ptr) {
149 *ptr = '\0';
150 }
151
152 len = strlen(line);
153
154 if (len + 1 < 256) {
155 ptr = realloc(*lineptr, 256);
156 if (!ptr) {
157 return -1;
158 }
159 *lineptr = ptr;
160 *n = 256;
161 }
162
163 strcpy(*lineptr, line);
164 return len;
165}
Radek Krejci0f969882020-08-21 16:56:47 +0200166
Michal Vasko5aa44c02020-06-29 11:47:02 +0200167#endif
168
169#ifndef HAVE_GET_CURRENT_DIR_NAME
170char *
171get_current_dir_name(void)
172{
173 char tmp[_POSIX_PATH_MAX];
174 char *retval = NULL;
175
176 if (getcwd(tmp, sizeof(tmp))) {
177 retval = strdup(tmp);
178 if (!retval) {
179 errno = ENOMEM;
180 }
181 }
182
183 return retval;
184}
Radek Krejci0f969882020-08-21 16:56:47 +0200185
Michal Vasko5aa44c02020-06-29 11:47:02 +0200186#endif