blob: 6d2e3a178301e2566865da17590b3dd819ee77ac [file] [log] [blame]
Michal Vasko5aa44c02020-06-29 11:47:02 +02001/**
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 Krejciad97c5f2020-06-30 09:19:28 +020018#include <limits.h>
Christian Hopps6f326212021-03-23 12:37:29 -040019#include <stdarg.h>
Michal Vasko5aa44c02020-06-29 11:47:02 +020020#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 Vaskoe29489b2020-08-24 10:43:54 +020033# define __INT64_C(c) c ## L
Michal Vasko5aa44c02020-06-29 11:47:02 +020034# define __UINT64_C(c) c ## UL
35# else
Michal Vaskoe29489b2020-08-24 10:43:54 +020036# define __INT64_C(c) c ## LL
Michal Vasko5aa44c02020-06-29 11:47:02 +020037# define __UINT64_C(c) c ## ULL
38# endif
39#endif
40
Michal Vaskoc5a22832020-08-20 13:21:33 +020041#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 Vasko5aa44c02020-06-29 11:47:02 +020049#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 Hopps6f326212021-03-23 12:37:29 -040057#ifndef bswap64
Michal Vasko5aa44c02020-06-29 11:47:02 +020058#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 Hopps6f326212021-03-23 12:37:29 -040063#endif
Michal Vasko5aa44c02020-06-29 11:47:02 +020064
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
79int vdprintf(int fd, const char *format, va_list ap);
80#endif
81
82#ifndef HAVE_ASPRINTF
83int asprintf(char **strp, const char *fmt, ...);
84#endif
85
86#ifndef HAVE_VASPRINTF
87int vasprintf(char **strp, const char *fmt, va_list ap);
88#endif
89
90#ifndef HAVE_STRNDUP
91char *strndup(const char *s, size_t n);
92#endif
93
94#ifndef HAVE_STRNSTR
95char *strnstr(const char *s, const char *find, size_t slen);
96#endif
97
98#ifndef HAVE_GETLINE
99ssize_t getline(char **lineptr, size_t *n, FILE *stream);
100#endif
101
102#ifndef HAVE_GET_CURRENT_DIR_NAME
103char *get_current_dir_name(void);
104#endif
105
106#endif /* _COMPAT_H_ */