blob: bc843055657a0c609cb68fc2af1f9cbcbdc44d2e [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>
Michal Vasko5aa44c02020-06-29 11:47:02 +020019#include <stdio.h>
20#include <sys/types.h>
21
22#ifndef __WORDSIZE
23# if defined __x86_64__ && !defined __ILP32__
24# define __WORDSIZE 64
25# else
26# define __WORDSIZE 32
27# endif
28#endif
29
30#ifndef __INT64_C
31# if __WORDSIZE == 64
32# define __INT64_C(c) c ## L
33# define __UINT64_C(c) c ## UL
34# else
35# define __INT64_C(c) c ## LL
36# define __UINT64_C(c) c ## ULL
37# endif
38#endif
39
Michal Vaskoc5a22832020-08-20 13:21:33 +020040#if (@CMAKE_C_COMPILER_ID@ == GNU) || (@CMAKE_C_COMPILER_ID@ == Clang)
41# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
42# define _PACKED __attribute__((__packed__))
43#else
44# define UNUSED(x) UNUSED_ ## x
45# define _PACKED
46#endif
47
Michal Vasko5aa44c02020-06-29 11:47:02 +020048#cmakedefine HAVE_VDPRINTF
49#cmakedefine HAVE_ASPRINTF
50#cmakedefine HAVE_VASPRINTF
51#cmakedefine HAVE_STRNDUP
52#cmakedefine HAVE_STRNSTR
53#cmakedefine HAVE_GETLINE
54#cmakedefine HAVE_GET_CURRENT_DIR_NAME
55
56#define bswap64(val) \
57 ( (((val) >> 56) & 0x00000000000000FF) | (((val) >> 40) & 0x000000000000FF00) | \
58 (((val) >> 24) & 0x0000000000FF0000) | (((val) >> 8) & 0x00000000FF000000) | \
59 (((val) << 8) & 0x000000FF00000000) | (((val) << 24) & 0x0000FF0000000000) | \
60 (((val) << 40) & 0x00FF000000000000) | (((val) << 56) & 0xFF00000000000000) )
61
62#undef le64toh
63#undef htole64
64
65#cmakedefine IS_BIG_ENDIAN
66
67#ifdef IS_BIG_ENDIAN
68# define le64toh(x) bswap64(x)
69# define htole64(x) bswap64(x)
70#else
71# define le64toh(x) (x)
72# define htole64(x) (x)
73#endif
74
75#ifndef HAVE_VDPRINTF
76int vdprintf(int fd, const char *format, va_list ap);
77#endif
78
79#ifndef HAVE_ASPRINTF
80int asprintf(char **strp, const char *fmt, ...);
81#endif
82
83#ifndef HAVE_VASPRINTF
84int vasprintf(char **strp, const char *fmt, va_list ap);
85#endif
86
87#ifndef HAVE_STRNDUP
88char *strndup(const char *s, size_t n);
89#endif
90
91#ifndef HAVE_STRNSTR
92char *strnstr(const char *s, const char *find, size_t slen);
93#endif
94
95#ifndef HAVE_GETLINE
96ssize_t getline(char **lineptr, size_t *n, FILE *stream);
97#endif
98
99#ifndef HAVE_GET_CURRENT_DIR_NAME
100char *get_current_dir_name(void);
101#endif
102
103#endif /* _COMPAT_H_ */