Debian Codegraph @

lib/libcompat.h

raw
1 /*
2 * Check: a unit test framework for C
3 * Copyright (C) 2001, 2002 Arien Malec
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 * MA 02110-1301, USA.
19 */
20
21 #ifndef LIBCOMPAT_H
22 #define LIBCOMPAT_H
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 /**
29 * __GNUC_PATCHLEVEL__ is new to GCC 3.0;
30 * it is also present in the widely-used development snapshots leading up to 3.0
31 * (which identify themselves as GCC 2.96 or 2.97, depending on which snapshot you have).
32 *
33 * https://stackoverflow.com/questions/1936719/what-are-the-gcc-predefined-macros-for-the-compilers-version-number/1936745#1936745
34 */
35
36 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
37 #define GCC_VERSION_AT_LEAST(major, minor, patch) \
38 ((__GNUC__ > (major)) || \
39 (__GNUC__ == (major) && __GNUC_MINOR__ > (minor)) || \
40 (__GNUC__ == (major) && __GNUC_MINOR__ == (minor) && __GNUC_PATCHLEVEL__ >= (patch)) )
41 #elif defined(__GNUC__) && defined(__GNUC_MINOR__)
42 #define GCC_VERSION_AT_LEAST(major, minor, patch) \
43 ((__GNUC__ > (major)) || \
44 (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
45 #else
46 #define GCC_VERSION_AT_LEAST(major, minor, patch) 0
47 #endif
48
49 #if GCC_VERSION_AT_LEAST(2,95,3)
50 #define CK_ATTRIBUTE_UNUSED __attribute__ ((unused))
51 #define CK_ATTRIBUTE_FORMAT(a, b, c) __attribute__ ((format (a, b, c)))
52 #else
53 #define CK_ATTRIBUTE_UNUSED
54 #define CK_ATTRIBUTE_FORMAT(a, b, c)
55 #endif /* GCC 2.95 */
56
57 #if GCC_VERSION_AT_LEAST(2,5,0)
58 #define CK_ATTRIBUTE_NORETURN __attribute__ ((noreturn))
59 #else
60 #define CK_ATTRIBUTE_NORETURN
61 #endif /* GCC 2.5 */
62
63 #if GCC_VERSION_AT_LEAST(4,7,4) && (__STDC_VERSION__ >= 199901L)
64 /* Operator _Pragma introduced in C99 */
65 #define CK_DIAGNOSTIC_STRINGIFY(x) #x
66 #define CK_DIAGNOSTIC_HELPER1(y) CK_DIAGNOSTIC_STRINGIFY(GCC diagnostic ignored y)
67 #define CK_DIAGNOSTIC_HELPER2(z) CK_DIAGNOSTIC_HELPER1(#z)
68 #define CK_DIAGNOSTIC_PUSH_IGNORE(w) \
69 _Pragma("GCC diagnostic push") \
70 _Pragma(CK_DIAGNOSTIC_HELPER2(w))
71 #define CK_DIAGNOSTIC_POP(w) _Pragma ("GCC diagnostic pop")
72 #else
73 #define CK_DIAGNOSTIC_PUSH_IGNORE(w)
74 #define CK_DIAGNOSTIC_POP(w)
75 #endif /* GCC 4.7.4 */
76
77 /*
78 * Used for MSVC to create the export attribute
79 * CK_DLL_EXP is defined during the compilation of the library
80 * on the command line.
81 */
82 #ifndef CK_DLL_EXP
83 #define CK_DLL_EXP
84 #endif
85
86 #if defined(_MSC_VER)
87 #include <WinSock2.h> /* struct timeval, API used in gettimeofday implementation */
88 #include <io.h> /* read, write */
89 #include <process.h> /* getpid */
90 #endif /* _MSC_VER */
91
92 /* defines size_t */
93 #include <sys/types.h>
94
95 /* provides assert */
96 #include <assert.h>
97
98 /* defines FILE */
99 #include <stdio.h>
100
101 /* defines exit() */
102 #include <stdlib.h>
103
104 /* defines NAN, INFINITY, isnan(), isinf(), isfinite() */
105 #include <math.h>
106
107 /* However, some older Visual Studio Versions do not */
108 #if !defined(INFINITY) || !defined(NAN)
109 extern double DOUBLE_ZERO;
110 #define INFINITY (1.0/DOUBLE_ZERO)
111 #define NAN (DOUBLE_ZERO/DOUBLE_ZERO)
112 #endif
113 #if !defined(isnan) || !defined(isinf) || !defined(isfinite)
114 #define NEED_fpclassify
115 extern int fpclassify(double d);
116 #define FP_INFINITE (1)
117 #define FP_NAN (2)
118 #define FP_ZERO (4)
119 #define FP_NORMAL (8)
120 #define FP_SUBNORMAL (16)
121 #define isnan(x) ((fpclassify((double)(x)) & FP_NAN) == FP_NAN)
122 #define isinf(x) ((fpclassify((double)(x)) & FP_INFINITE) == FP_INFINITE)
123 #define isfinite(x) ((fpclassify((double)(x)) & (FP_NAN|FP_INFINITE)) == 0)
124 #endif
125
126
127 /* provides localtime and struct tm */
128 #ifdef HAVE_SYS_TIME_H
129 #include <sys/time.h>
130 #endif /* !HAVE_SYS_TIME_H */
131 #include <time.h>
132
133 /* declares fork(), _POSIX_VERSION. according to Autoconf.info,
134 unistd.h defines _POSIX_VERSION if the system is POSIX-compliant,
135 so we will use this as a test for all things uniquely provided by
136 POSIX like sigaction() and fork() */
137 #ifdef HAVE_UNISTD_H
138 #include <unistd.h>
139 #endif
140
141 #ifdef HAVE_SYS_WAIT_H
142 #include <sys/wait.h>
143 #endif
144
145 #if defined(HAVE_INIT_ONCE_BEGIN_INITIALIZE) && defined(HAVE_INIT_ONCE_COMPLETE)
146 #define HAVE_WIN32_INIT_ONCE 1
147 #endif
148
149 /* declares pthread_create and friends */
150 #if defined HAVE_PTHREAD
151 #include <pthread.h>
152 #elif defined HAVE_WIN32_INIT_ONCE
153 typedef void pthread_mutexattr_t;
154
155 typedef struct
156 {
157 INIT_ONCE init;
158 HANDLE mutex;
159 } pthread_mutex_t;
160
161 #define PTHREAD_MUTEX_INITIALIZER { \
162 INIT_ONCE_STATIC_INIT, \
163 NULL, \
164 }
165
166 int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
167 int pthread_mutex_destroy(pthread_mutex_t *mutex);
168 int pthread_mutex_lock(pthread_mutex_t *mutex);
169 int pthread_mutex_unlock(pthread_mutex_t *mutex);
170 #endif
171
172 #ifdef HAVE_STDINT_H
173 #include <stdint.h>
174 #endif
175
176 /* replacement functions for broken originals */
177 #if !HAVE_DECL_ALARM
178 CK_DLL_EXP unsigned int alarm(unsigned int seconds);
179 #endif /* !HAVE_DECL_ALARM */
180
181 #if !HAVE_MALLOC
182 CK_DLL_EXP void *rpl_malloc(size_t n);
183 #endif /* !HAVE_MALLOC */
184
185 #if !HAVE_REALLOC
186 CK_DLL_EXP void *rpl_realloc(void *p, size_t n);
187 #endif /* !HAVE_REALLOC */
188
189 #if !HAVE_GETPID && HAVE__GETPID
190 #define getpid _getpid
191 #endif /* !HAVE_GETPID && HAVE__GETPID */
192
193 #if !HAVE_GETTIMEOFDAY
194 CK_DLL_EXP int gettimeofday(struct timeval *tv, void *tz);
195 #endif /* !HAVE_GETTIMEOFDAY */
196
197 #if !HAVE_DECL_LOCALTIME_R
198 #if !defined(localtime_r)
199 CK_DLL_EXP struct tm *localtime_r(const time_t * clock, struct tm *result);
200 #endif
201 #endif /* !HAVE_DECL_LOCALTIME_R */
202
203 #if !HAVE_DECL_STRDUP && !HAVE__STRDUP
204 CK_DLL_EXP char *strdup(const char *str);
205 #elif !HAVE_DECL_STRDUP && HAVE__STRDUP
206 #define strdup _strdup
207 #endif /* !HAVE_DECL_STRDUP && HAVE__STRDUP */
208
209 #if !HAVE_DECL_STRSIGNAL
210 CK_DLL_EXP char *strsignal(int sig);
211 #endif /* !HAVE_DECL_STRSIGNAL */
212
213 /*
214 * On systems where clock_gettime() is not available, or
215 * on systems where some clocks may not be supported, the
216 * definition for CLOCK_MONOTONIC and CLOCK_REALTIME may not
217 * be available. These should define which type of clock
218 * clock_gettime() should use. We define it here if it is
219 * not defined simply so the reimplementation can ignore it.
220 *
221 * We set the values of these clocks to some (hopefully)
222 * invalid value, to avoid the case where we define a
223 * clock with a valid value, and unintentionally use
224 * an actual good clock by accident.
225 */
226 #ifndef CLOCK_MONOTONIC
227 #define CLOCK_MONOTONIC -1
228 #endif
229 #ifndef CLOCK_REALTIME
230 #define CLOCK_REALTIME -1
231 #endif
232
233 #ifndef HAVE_LIBRT
234
235 #ifdef STRUCT_TIMESPEC_DEFINITION_MISSING
236 /*
237 * The following structure is defined in POSIX 1003.1 for times
238 * specified in seconds and nanoseconds. If it is not defined in
239 * time.g, then we need to define it here
240 */
241 struct timespec
242 {
243 time_t tv_sec;
244 long tv_nsec;
245 };
246 #endif /* STRUCT_TIMESPEC_DEFINITION_MISSING */
247
248 #ifdef STRUCT_ITIMERSPEC_DEFINITION_MISSING
249 /*
250 * The following structure is defined in POSIX.1b for timer start values and intervals.
251 * If it is not defined in time.h, then we need to define it here.
252 */
253 struct itimerspec
254 {
255 struct timespec it_interval;
256 struct timespec it_value;
257 };
258 #endif /* STRUCT_ITIMERSPEC_DEFINITION_MISSING */
259
260 /*
261 * Do a simple forward declaration in case the struct is not defined.
262 * In the versions of timer_create in libcompat, sigevent is never
263 * used.
264 */
265 struct sigevent;
266
267 CK_DLL_EXP int clock_gettime(clockid_t clk_id, struct timespec *ts);
268 CK_DLL_EXP int timer_create(clockid_t clockid, struct sigevent *sevp,
269 timer_t * timerid);
270 CK_DLL_EXP int timer_settime(timer_t timerid, int flags,
271 const struct itimerspec *new_value,
272 struct itimerspec *old_value);
273 CK_DLL_EXP int timer_delete(timer_t timerid);
274 #endif /* HAVE_LIBRT */
275
276 /*
277 * The following checks are to determine if the system's
278 * snprintf (or its variants) should be replaced with
279 * the C99 compliant version in libcompat.
280 */
281 #if HAVE_CONFIG_H
282 #include <config.h>
283 #endif
284 #if HAVE_STDARG_H
285 #include <stdarg.h>
286
287 #if !HAVE_VSNPRINTF
288 CK_DLL_EXP int rpl_vsnprintf(char *, size_t, const char *, va_list);
289
290 #define vsnprintf rpl_vsnprintf
291 #endif
292 #if !HAVE_SNPRINTF
293 CK_DLL_EXP int rpl_snprintf(char *, size_t, const char *, ...);
294
295 #define snprintf rpl_snprintf
296 #endif
297 #endif /* HAVE_STDARG_H */
298
299 #if !HAVE_GETLINE
300 CK_DLL_EXP ssize_t getline(char **lineptr, size_t *n, FILE *stream);
301 #endif
302
303 /* silence warnings about an empty library */
304 CK_DLL_EXP void ck_do_nothing(void) CK_ATTRIBUTE_NORETURN;
305
306 #endif /* !LIBCOMPAT_H */