Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 1 | /** |
| 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 | |
| 16 | #define _POSIX_C_SOURCE 1 /* fdopen, _POSIX_PATH_MAX */ |
| 17 | #define _ISOC99_SOURCE /* vsnprintf */ |
| 18 | #define _XOPEN_SOURCE 500 /* strdup */ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <limits.h> |
| 22 | #include <string.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #ifndef HAVE_VDPRINTF |
| 29 | int |
| 30 | vdprintf(int fd, const char *format, va_list ap) |
| 31 | { |
| 32 | FILE *stream; |
| 33 | int count; |
| 34 | |
| 35 | stream = fdopen(dup(fd), "a+"); |
| 36 | if (stream) { |
| 37 | count = vfprintf(stream, format, ap); |
| 38 | fclose(stream); |
| 39 | } |
| 40 | return count; |
| 41 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 42 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | #ifndef HAVE_ASPRINTF |
| 46 | int |
| 47 | asprintf(char **strp, const char *fmt, ...) |
| 48 | { |
| 49 | int ret; |
| 50 | va_list ap; |
| 51 | |
| 52 | va_start(ap, fmt); |
| 53 | ret = vasprintf(strp, fmt, ap); |
| 54 | va_end(ap); |
| 55 | return ret; |
| 56 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 57 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 58 | #endif |
| 59 | |
| 60 | #ifndef HAVE_VASPRINTF |
| 61 | int |
| 62 | vasprintf(char **strp, const char *fmt, va_list ap) |
| 63 | { |
| 64 | va_list ap2; |
| 65 | va_copy(ap2, ap); |
| 66 | int l = vsnprintf(0, 0, fmt, ap2); |
| 67 | va_end(ap2); |
| 68 | |
| 69 | if (l < 0 || !(*strp = malloc(l + 1U))) { |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | return vsnprintf(*strp, l + 1U, fmt, ap); |
| 74 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 75 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 76 | #endif |
| 77 | |
| 78 | #ifndef HAVE_STRNDUP |
| 79 | char * |
| 80 | strndup(const char *s, size_t n) |
| 81 | { |
| 82 | char *buf; |
| 83 | size_t len = 0; |
| 84 | |
| 85 | /* strnlen */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 86 | for (; (len < n) && (s[len] != '\0'); ++len) {} |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 87 | |
| 88 | if (!(buf = malloc(len + 1U))) { |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | memcpy(buf, s, len); |
| 93 | buf[len] = '\0'; |
| 94 | return buf; |
| 95 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 96 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 97 | #endif |
| 98 | |
| 99 | #ifndef HAVE_STRNSTR |
| 100 | char * |
| 101 | strnstr(const char *s, const char *find, size_t slen) |
| 102 | { |
| 103 | char c, sc; |
| 104 | size_t len; |
| 105 | |
| 106 | if ((c = *find++) != '\0') { |
| 107 | len = strlen(find); |
| 108 | do { |
| 109 | do { |
| 110 | if ((slen-- < 1) || ((sc = *s++) == '\0')) { |
| 111 | return NULL; |
| 112 | } |
| 113 | } while (sc != c); |
| 114 | if (len > slen) { |
| 115 | return NULL; |
| 116 | } |
| 117 | } while (strncmp(s, find, len)); |
| 118 | s--; |
| 119 | } |
| 120 | return (char *)s; |
| 121 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 122 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 123 | #endif |
| 124 | |
| 125 | #ifndef HAVE_GETLINE |
| 126 | ssize_t |
| 127 | getline(char **lineptr, size_t *n, FILE *stream) |
| 128 | { |
| 129 | static char line[256]; |
| 130 | char *ptr; |
| 131 | unsigned int len; |
| 132 | |
| 133 | if (!lineptr || !n) { |
| 134 | errno = EINVAL; |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | if (ferror(stream) || feof(stream)) { |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | if (!fgets(line, 256, stream)) { |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | ptr = strchr(line, '\n'); |
| 147 | if (ptr) { |
| 148 | *ptr = '\0'; |
| 149 | } |
| 150 | |
| 151 | len = strlen(line); |
| 152 | |
| 153 | if (len + 1 < 256) { |
| 154 | ptr = realloc(*lineptr, 256); |
| 155 | if (!ptr) { |
| 156 | return -1; |
| 157 | } |
| 158 | *lineptr = ptr; |
| 159 | *n = 256; |
| 160 | } |
| 161 | |
| 162 | strcpy(*lineptr, line); |
| 163 | return len; |
| 164 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 165 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 166 | #endif |
| 167 | |
| 168 | #ifndef HAVE_GET_CURRENT_DIR_NAME |
| 169 | char * |
| 170 | get_current_dir_name(void) |
| 171 | { |
| 172 | char tmp[_POSIX_PATH_MAX]; |
| 173 | char *retval = NULL; |
| 174 | |
| 175 | if (getcwd(tmp, sizeof(tmp))) { |
| 176 | retval = strdup(tmp); |
| 177 | if (!retval) { |
| 178 | errno = ENOMEM; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return retval; |
| 183 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 184 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 185 | #endif |