blob: b69a339aa7bb50033dbb245ee319f2c8f016f94e [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
40#cmakedefine HAVE_VDPRINTF
41#cmakedefine HAVE_ASPRINTF
42#cmakedefine HAVE_VASPRINTF
43#cmakedefine HAVE_STRNDUP
44#cmakedefine HAVE_STRNSTR
45#cmakedefine HAVE_GETLINE
46#cmakedefine HAVE_GET_CURRENT_DIR_NAME
47
48#define bswap64(val) \
49 ( (((val) >> 56) & 0x00000000000000FF) | (((val) >> 40) & 0x000000000000FF00) | \
50 (((val) >> 24) & 0x0000000000FF0000) | (((val) >> 8) & 0x00000000FF000000) | \
51 (((val) << 8) & 0x000000FF00000000) | (((val) << 24) & 0x0000FF0000000000) | \
52 (((val) << 40) & 0x00FF000000000000) | (((val) << 56) & 0xFF00000000000000) )
53
54#undef le64toh
55#undef htole64
56
57#cmakedefine IS_BIG_ENDIAN
58
59#ifdef IS_BIG_ENDIAN
60# define le64toh(x) bswap64(x)
61# define htole64(x) bswap64(x)
62#else
63# define le64toh(x) (x)
64# define htole64(x) (x)
65#endif
66
67#ifndef HAVE_VDPRINTF
68int vdprintf(int fd, const char *format, va_list ap);
69#endif
70
71#ifndef HAVE_ASPRINTF
72int asprintf(char **strp, const char *fmt, ...);
73#endif
74
75#ifndef HAVE_VASPRINTF
76int vasprintf(char **strp, const char *fmt, va_list ap);
77#endif
78
79#ifndef HAVE_STRNDUP
80char *strndup(const char *s, size_t n);
81#endif
82
83#ifndef HAVE_STRNSTR
84char *strnstr(const char *s, const char *find, size_t slen);
85#endif
86
87#ifndef HAVE_GETLINE
88ssize_t getline(char **lineptr, size_t *n, FILE *stream);
89#endif
90
91#ifndef HAVE_GET_CURRENT_DIR_NAME
92char *get_current_dir_name(void);
93#endif
94
95#endif /* _COMPAT_H_ */