1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/*
* libcompat - system compatibility library
* compat.h - system compatibility declarations
*
* Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
* Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef COMPAT_H
#define COMPAT_H
#ifndef TEST_LIBCOMPAT
#define TEST_LIBCOMPAT 0
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_STRNLEN) || !defined(HAVE_STRNDUP) || \
!defined(HAVE_C99_SNPRINTF)
#include <stddef.h>
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_ASPRINTF) || !defined(HAVE_C99_SNPRINTF)
#include <stdarg.h>
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_VA_COPY)
#include <string.h>
#endif
/* Language definitions. */
/* Supported since gcc 5.1.0 and clang 2.9.0. For attributes that appeared
* before these versions, in addition we need to do version checks. */
#ifndef __has_attribute
#define __has_attribute(x) 0
#endif
#ifdef __GNUC__
#define LIBCOMPAT_GCC_VERSION (__GNUC__ << 8 | __GNUC_MINOR__)
#else
#define LIBCOMPAT_GCC_VERSION 0
#endif
#if LIBCOMPAT_GCC_VERSION >= 0x0300 || __has_attribute(__format__)
#define LIBCOMPAT_ATTR_FMT(t, f, a) __attribute__((__format__(t, f, a)))
#define LIBCOMPAT_ATTR_PRINTF(n) LIBCOMPAT_ATTR_FMT(__printf__, n, n + 1)
#define LIBCOMPAT_ATTR_VPRINTF(n) LIBCOMPAT_ATTR_FMT(__printf__, n, 0)
#else
#define LIBCOMPAT_ATTR_FMT(t, f, a)
#define LIBCOMPAT_ATTR_PRINTF(n)
#define LIBCOMPAT_ATTR_VPRINTF(n)
#endif
#if LIBCOMPAT_GCC_VERSION >= 0x0300 || __has_attribute(__noreturn__)
#define LIBCOMPAT_ATTR_NORET __attribute__((__noreturn__))
#else
#define LIBCOMPAT_ATTR_NORET
#endif
#if LIBCOMPAT_GCC_VERSION >= 0x0400 || __has_attribute(__sentinel__)
#define LIBCOMPAT_ATTR_SENTINEL __attribute__((__sentinel__))
#else
#define LIBCOMPAT_ATTR_SENTINEL
#endif
/* For C++, define a __func__ fallback in case it's not natively supported. */
#if defined(__cplusplus) && __cplusplus < 201103L
# if LIBCOMPAT_GCC_VERSION >= 0x0200
# define __func__ __PRETTY_FUNCTION__
# else
# define __func__ __FUNCTION__
# endif
#endif
#if defined(__cplusplus) && __cplusplus < 201103L
#define nullptr 0
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifndef HAVE_OFFSETOF
#define offsetof(st, m) ((size_t)&((st *)NULL)->m)
#endif
#ifndef HAVE_MAKEDEV
#define makedev(maj, min) ((((maj) & 0xff) << 8) | ((min) & 0xff))
#endif
#ifndef HAVE_O_NOFOLLOW
#define O_NOFOLLOW 0
#endif
#ifndef HAVE_P_TMPDIR
#define P_tmpdir "/tmp"
#endif
/*
* Define WCOREDUMP if we don't have it already, coredumps won't be
* detected, though.
*/
#ifndef HAVE_WCOREDUMP
#define WCOREDUMP(x) 0
#endif
#ifndef HAVE_VA_COPY
#define va_copy(dest, src) memcpy(&(dest), &(src), sizeof(va_list))
#endif
#if TEST_LIBCOMPAT
#undef snprintf
#define snprintf test_snprintf
#undef vsnprintf
#define vsnprintf test_vsnprintf
#undef asprintf
#define asprintf test_asprintf
#undef vasprintf
#define vasprintf test_vasprintf
#undef strchrnul
#define strchrnul test_strchrnul
#undef strndup
#define strndup test_strndup
#undef strnlen
#define strnlen test_strnlen
#undef strerror
#define strerror test_strerror
#undef strsignal
#define strsignal test_strsignal
#undef scandir
#define scandir test_scandir
#undef alphasort
#define alphasort test_alphasort
#undef unsetenv
#define unsetenv test_unsetenv
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_C99_SNPRINTF)
int snprintf(char *str, size_t n, char const *fmt, ...)
LIBCOMPAT_ATTR_PRINTF(3);
int vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list args)
LIBCOMPAT_ATTR_VPRINTF(3);
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_ASPRINTF)
int asprintf(char **str, char const *fmt, ...)
LIBCOMPAT_ATTR_PRINTF(2);
int vasprintf(char **str, const char *fmt, va_list args)
LIBCOMPAT_ATTR_VPRINTF(2);
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_STRCHRNUL)
char *strchrnul(const char *s, int c);
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_STRNLEN)
size_t strnlen(const char *s, size_t n);
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_STRNDUP)
char *strndup(const char *s, size_t n);
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_STRERROR)
const char *strerror(int);
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_STRSIGNAL)
const char *strsignal(int);
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_SCANDIR)
struct dirent;
int scandir(const char *dir, struct dirent ***namelist,
int (*filter)(const struct dirent *),
int (*cmp)(const void *, const void *));
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_ALPHASORT)
int alphasort(const void *a, const void *b);
#endif
#if TEST_LIBCOMPAT || !defined(HAVE_UNSETENV)
int unsetenv(const char *x);
#endif
#ifdef __cplusplus
}
#endif
#endif /* COMPAT_H */
|