Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file compat.h |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief compatibility functions header |
| 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 | |
| 15 | #ifndef _COMPAT_H_ |
| 16 | #define _COMPAT_H_ |
| 17 | |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 18 | #include <limits.h> |
Christian Hopps | 6f32621 | 2021-03-23 12:37:29 -0400 | [diff] [blame] | 19 | #include <stdarg.h> |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #ifndef __WORDSIZE |
| 24 | # if defined __x86_64__ && !defined __ILP32__ |
| 25 | # define __WORDSIZE 64 |
| 26 | # else |
| 27 | # define __WORDSIZE 32 |
| 28 | # endif |
| 29 | #endif |
| 30 | |
| 31 | #ifndef __INT64_C |
| 32 | # if __WORDSIZE == 64 |
Michal Vasko | e29489b | 2020-08-24 10:43:54 +0200 | [diff] [blame] | 33 | # define __INT64_C(c) c ## L |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 34 | # define __UINT64_C(c) c ## UL |
| 35 | # else |
Michal Vasko | e29489b | 2020-08-24 10:43:54 +0200 | [diff] [blame] | 36 | # define __INT64_C(c) c ## LL |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 37 | # define __UINT64_C(c) c ## ULL |
| 38 | # endif |
| 39 | #endif |
| 40 | |
Michal Vasko | c5a2283 | 2020-08-20 13:21:33 +0200 | [diff] [blame] | 41 | #if (@CMAKE_C_COMPILER_ID@ == GNU) || (@CMAKE_C_COMPILER_ID@ == Clang) |
| 42 | # define UNUSED(x) UNUSED_ ## x __attribute__((__unused__)) |
| 43 | # define _PACKED __attribute__((__packed__)) |
| 44 | #else |
| 45 | # define UNUSED(x) UNUSED_ ## x |
| 46 | # define _PACKED |
| 47 | #endif |
| 48 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 49 | #cmakedefine HAVE_VDPRINTF |
| 50 | #cmakedefine HAVE_ASPRINTF |
| 51 | #cmakedefine HAVE_VASPRINTF |
| 52 | #cmakedefine HAVE_STRNDUP |
| 53 | #cmakedefine HAVE_STRNSTR |
| 54 | #cmakedefine HAVE_GETLINE |
| 55 | #cmakedefine HAVE_GET_CURRENT_DIR_NAME |
| 56 | |
Christian Hopps | 6f32621 | 2021-03-23 12:37:29 -0400 | [diff] [blame] | 57 | #ifndef bswap64 |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 58 | #define bswap64(val) \ |
| 59 | ( (((val) >> 56) & 0x00000000000000FF) | (((val) >> 40) & 0x000000000000FF00) | \ |
| 60 | (((val) >> 24) & 0x0000000000FF0000) | (((val) >> 8) & 0x00000000FF000000) | \ |
| 61 | (((val) << 8) & 0x000000FF00000000) | (((val) << 24) & 0x0000FF0000000000) | \ |
| 62 | (((val) << 40) & 0x00FF000000000000) | (((val) << 56) & 0xFF00000000000000) ) |
Christian Hopps | 6f32621 | 2021-03-23 12:37:29 -0400 | [diff] [blame] | 63 | #endif |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 64 | |
| 65 | #undef le64toh |
| 66 | #undef htole64 |
| 67 | |
| 68 | #cmakedefine IS_BIG_ENDIAN |
| 69 | |
| 70 | #ifdef IS_BIG_ENDIAN |
| 71 | # define le64toh(x) bswap64(x) |
| 72 | # define htole64(x) bswap64(x) |
| 73 | #else |
| 74 | # define le64toh(x) (x) |
| 75 | # define htole64(x) (x) |
| 76 | #endif |
| 77 | |
| 78 | #ifndef HAVE_VDPRINTF |
| 79 | int vdprintf(int fd, const char *format, va_list ap); |
| 80 | #endif |
| 81 | |
| 82 | #ifndef HAVE_ASPRINTF |
| 83 | int asprintf(char **strp, const char *fmt, ...); |
| 84 | #endif |
| 85 | |
| 86 | #ifndef HAVE_VASPRINTF |
| 87 | int vasprintf(char **strp, const char *fmt, va_list ap); |
| 88 | #endif |
| 89 | |
| 90 | #ifndef HAVE_STRNDUP |
| 91 | char *strndup(const char *s, size_t n); |
| 92 | #endif |
| 93 | |
| 94 | #ifndef HAVE_STRNSTR |
| 95 | char *strnstr(const char *s, const char *find, size_t slen); |
| 96 | #endif |
| 97 | |
| 98 | #ifndef HAVE_GETLINE |
| 99 | ssize_t getline(char **lineptr, size_t *n, FILE *stream); |
| 100 | #endif |
| 101 | |
| 102 | #ifndef HAVE_GET_CURRENT_DIR_NAME |
| 103 | char *get_current_dir_name(void); |
| 104 | #endif |
| 105 | |
| 106 | #endif /* _COMPAT_H_ */ |