• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
29 #define GCC_VERSION_AT_LEAST(major, minor) \
30 ((__GNUC__ > (major)) || \
31  (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
32 #else
33 #define GCC_VERSION_AT_LEAST(major, minor) 0
34 #endif
35 
36 #if GCC_VERSION_AT_LEAST(2,95)
37 #define CK_ATTRIBUTE_UNUSED __attribute__ ((unused))
38 #else
39 #define CK_ATTRIBUTE_UNUSED
40 #endif /* GCC 2.95 */
41 
42 #if GCC_VERSION_AT_LEAST(2,5)
43 #define CK_ATTRIBUTE_NORETURN __attribute__ ((noreturn))
44 #else
45 #define CK_ATTRIBUTE_NORETURN
46 #endif /* GCC 2.5 */
47 
48 /*
49  * Used for MSVC to create the export attribute
50  * CK_DLL_EXP is defined during the compilation of the library
51  * on the command line.
52  */
53 #ifndef CK_DLL_EXP
54 #define CK_DLL_EXP extern
55 #endif
56 
57 #if _MSC_VER
58 #include <WinSock2.h>           /* struct timeval, API used in gettimeofday implementation */
59 #include <io.h>                 /* read, write */
60 #include <process.h>            /* getpid */
61 #include <BaseTsd.h>            /* for ssize_t */
62 typedef SSIZE_T ssize_t;
63 #endif /* _MSC_VER */
64 
65 /* defines size_t */
66 #include <sys/types.h>
67 
68 /* provides assert */
69 #include <assert.h>
70 
71 /* defines FILE */
72 #include <stdio.h>
73 
74 /* defines exit() */
75 #include <stdlib.h>
76 
77 /* provides localtime and struct tm */
78 #ifdef HAVE_SYS_TIME_H
79 #include <sys/time.h>
80 #endif /* !HAVE_SYS_TIME_H */
81 #include <time.h>
82 
83 /* declares fork(), _POSIX_VERSION.  according to Autoconf.info,
84    unistd.h defines _POSIX_VERSION if the system is POSIX-compliant,
85    so we will use this as a test for all things uniquely provided by
86    POSIX like sigaction() and fork() */
87 #ifdef HAVE_UNISTD_H
88 #include <unistd.h>
89 #endif
90 
91 #ifdef HAVE_SYS_WAIT_H
92 #include <sys/wait.h>
93 #endif
94 
95 /* declares pthread_create and friends */
96 #ifdef HAVE_PTHREAD
97 #include <pthread.h>
98 #endif
99 
100 #ifdef HAVE_STDINT_H
101 #include <stdint.h>
102 #endif
103 
104 /* replacement functions for broken originals */
105 #if !HAVE_DECL_ALARM
106 CK_DLL_EXP unsigned int alarm (unsigned int seconds);
107 #endif /* !HAVE_DECL_ALARM */
108 
109 #if !HAVE_MALLOC
110 CK_DLL_EXP void *rpl_malloc (size_t n);
111 #endif /* !HAVE_MALLOC */
112 
113 #if !HAVE_REALLOC
114 CK_DLL_EXP void *rpl_realloc (void *p, size_t n);
115 #endif /* !HAVE_REALLOC */
116 
117 #if !HAVE_GETPID && HAVE__GETPID
118 #define getpid _getpid
119 #endif /* !HAVE_GETPID && HAVE__GETPID */
120 
121 #if !HAVE_GETTIMEOFDAY
122 CK_DLL_EXP int gettimeofday (struct timeval *tv, void *tz);
123 #endif /* !HAVE_GETTIMEOFDAY */
124 
125 #if !HAVE_DECL_LOCALTIME_R
126 #if !defined(localtime_r)
127 CK_DLL_EXP struct tm *localtime_r (const time_t * clock, struct tm *result);
128 #endif
129 #endif /* !HAVE_DECL_LOCALTIME_R */
130 
131 #if !HAVE_DECL_STRDUP && !HAVE__STRDUP
132 CK_DLL_EXP char *strdup (const char *str);
133 #elif !HAVE_DECL_STRDUP && HAVE__STRDUP
134 #define strdup _strdup
135 #endif /* !HAVE_DECL_STRDUP && HAVE__STRDUP */
136 
137 #if !HAVE_DECL_STRSIGNAL
138 CK_DLL_EXP char *strsignal (int sig);
139 #endif /* !HAVE_DECL_STRSIGNAL */
140 
141 /*
142  * On systems where clock_gettime() is not available, or
143  * on systems where some clocks may not be supported, the
144  * definition for CLOCK_MONOTONIC and CLOCK_REALTIME may not
145  * be available. These should define which type of clock
146  * clock_gettime() should use. We define it here if it is
147  * not defined simply so the reimplementation can ignore it.
148  *
149  * We set the values of these clocks to some (hopefully)
150  * invalid value, to avoid the case where we define a
151  * clock with a valid value, and unintentionally use
152  * an actual good clock by accident.
153  */
154 #ifndef CLOCK_MONOTONIC
155 #define CLOCK_MONOTONIC -1
156 #endif
157 #ifndef CLOCK_REALTIME
158 #define CLOCK_REALTIME -1
159 #endif
160 
161 #ifndef HAVE_LIBRT
162 
163 #ifdef STRUCT_TIMESPEC_DEFINITION_MISSING
164 /*
165  * The following structure is defined in POSIX 1003.1 for times
166  * specified in seconds and nanoseconds. If it is not defined in
167  * time.g, then we need to define it here
168  */
169 struct timespec
170 {
171   time_t tv_sec;
172   long tv_nsec;
173 };
174 #endif /* STRUCT_TIMESPEC_DEFINITION_MISSING */
175 
176 #ifdef STRUCT_ITIMERSPEC_DEFINITION_MISSING
177 /*
178  * The following structure is defined in POSIX.1b for timer start values and intervals.
179  * If it is not defined in time.h, then we need to define it here.
180  */
181 struct itimerspec
182 {
183   struct timespec it_interval;
184   struct timespec it_value;
185 };
186 #endif /* STRUCT_ITIMERSPEC_DEFINITION_MISSING */
187 
188 /*
189  * Do a simple forward declaration in case the struct is not defined.
190  * In the versions of timer_create in libcompat, sigevent is never
191  * used.
192  */
193 struct sigevent;
194 
195 #ifndef HAVE_CLOCK_GETTIME
196 CK_DLL_EXP int clock_gettime (clockid_t clk_id, struct timespec *ts);
197 #endif
198 CK_DLL_EXP int timer_create (clockid_t clockid, struct sigevent *sevp,
199     timer_t * timerid);
200 CK_DLL_EXP int timer_settime (timer_t timerid, int flags,
201     const struct itimerspec *new_value, struct itimerspec *old_value);
202 CK_DLL_EXP int timer_delete (timer_t timerid);
203 #endif /* HAVE_LIBRT */
204 
205 /*
206  * The following checks are to determine if the system's
207  * snprintf (or its variants) should be replaced with
208  * the C99 compliant version in libcompat.
209  */
210 #if HAVE_CONFIG_H
211 #include <config.h>
212 #endif
213 #if HAVE_STDARG_H
214 #include <stdarg.h>
215 
216 #if !HAVE_VSNPRINTF
217 CK_DLL_EXP int rpl_vsnprintf (char *, size_t, const char *, va_list);
218 
219 #define vsnprintf rpl_vsnprintf
220 #endif
221 #if !HAVE_SNPRINTF
222 CK_DLL_EXP int rpl_snprintf (char *, size_t, const char *, ...);
223 
224 #define snprintf rpl_snprintf
225 #endif
226 #endif /* HAVE_STDARG_H */
227 
228 #if !HAVE_GETLINE
229 CK_DLL_EXP ssize_t getline (char **lineptr, size_t * n, FILE * stream);
230 #endif
231 
232 /* silence warnings about an empty library */
233 CK_DLL_EXP void
234 ck_do_nothing (void)
235     CK_ATTRIBUTE_NORETURN;
236 
237 #endif /* !LIBCOMPAT_H */
238