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 | |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 16 | #define _POSIX_C_SOURCE 200809L /* fdopen, _POSIX_PATH_MAX, strdup */ |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 17 | #define _ISOC99_SOURCE /* vsnprintf */ |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 18 | |
| 19 | #include <errno.h> |
| 20 | #include <limits.h> |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 24 | #include <string.h> |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
| 27 | #ifndef HAVE_VDPRINTF |
| 28 | int |
| 29 | vdprintf(int fd, const char *format, va_list ap) |
| 30 | { |
| 31 | FILE *stream; |
Radek Krejci | afb2883 | 2020-12-01 12:16:28 +0100 | [diff] [blame] | 32 | int count = 0; |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 33 | |
| 34 | stream = fdopen(dup(fd), "a+"); |
| 35 | if (stream) { |
| 36 | count = vfprintf(stream, format, ap); |
| 37 | fclose(stream); |
| 38 | } |
| 39 | return count; |
| 40 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 41 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | #ifndef HAVE_ASPRINTF |
| 45 | int |
| 46 | asprintf(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 Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 56 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 57 | #endif |
| 58 | |
| 59 | #ifndef HAVE_VASPRINTF |
| 60 | int |
| 61 | vasprintf(char **strp, const char *fmt, va_list ap) |
| 62 | { |
| 63 | va_list ap2; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 64 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 65 | va_copy(ap2, ap); |
| 66 | int l = vsnprintf(0, 0, fmt, ap2); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 67 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 68 | va_end(ap2); |
| 69 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 70 | if ((l < 0) || !(*strp = malloc(l + 1U))) { |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | return vsnprintf(*strp, l + 1U, fmt, ap); |
| 75 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 76 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | #ifndef HAVE_STRNDUP |
| 80 | char * |
| 81 | strndup(const char *s, size_t n) |
| 82 | { |
| 83 | char *buf; |
| 84 | size_t len = 0; |
| 85 | |
| 86 | /* strnlen */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 87 | for ( ; (len < n) && (s[len] != '\0'); ++len) {} |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 88 | |
| 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 Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 97 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 98 | #endif |
| 99 | |
| 100 | #ifndef HAVE_STRNSTR |
| 101 | char * |
| 102 | strnstr(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 Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 123 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 124 | #endif |
| 125 | |
| 126 | #ifndef HAVE_GETLINE |
| 127 | ssize_t |
| 128 | getline(char **lineptr, size_t *n, FILE *stream) |
| 129 | { |
| 130 | static char line[256]; |
| 131 | char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 132 | ssize_t len; |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 133 | |
| 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 Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 166 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 167 | #endif |
| 168 | |
| 169 | #ifndef HAVE_GET_CURRENT_DIR_NAME |
| 170 | char * |
| 171 | get_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 Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 185 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 186 | #endif |