xref: /curl/CMake/CurlTests.c (revision dfd36d3e)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 
25 #ifdef HAVE_FCNTL_O_NONBLOCK
26 /* headers for FCNTL_O_NONBLOCK test */
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 /* */
31 #if defined(sun) || defined(__sun__) || \
32     defined(__SUNPRO_C) || defined(__SUNPRO_CC)
33 # if defined(__SVR4) || defined(__srv4__)
34 #  define PLATFORM_SOLARIS
35 # else
36 #  define PLATFORM_SUNOS4
37 # endif
38 #endif
39 #if (defined(_AIX) || defined(__xlC__)) && !defined(_AIX41)
40 # define PLATFORM_AIX_V3
41 #endif
42 /* */
43 #if defined(PLATFORM_SUNOS4) || defined(PLATFORM_AIX_V3)
44 #error "O_NONBLOCK does not work on this platform"
45 #endif
46 
main(void)47 int main(void)
48 {
49   /* O_NONBLOCK source test */
50   int flags = 0;
51   if(0 != fcntl(0, F_SETFL, flags | O_NONBLOCK))
52     return 1;
53   ;
54   return 0;
55 }
56 #endif
57 
58 /* tests for gethostbyname_r */
59 #if defined(HAVE_GETHOSTBYNAME_R_3_REENTRANT) || \
60     defined(HAVE_GETHOSTBYNAME_R_5_REENTRANT) || \
61     defined(HAVE_GETHOSTBYNAME_R_6_REENTRANT)
62 #   define _REENTRANT
63     /* no idea whether _REENTRANT is always set, just invent a new flag */
64 #   define TEST_GETHOSTBYFOO_REENTRANT
65 #endif
66 #if defined(HAVE_GETHOSTBYNAME_R_3) || \
67     defined(HAVE_GETHOSTBYNAME_R_5) || \
68     defined(HAVE_GETHOSTBYNAME_R_6) || \
69     defined(TEST_GETHOSTBYFOO_REENTRANT)
70 #include <sys/types.h>
71 #include <netdb.h>
main(void)72 int main(void)
73 {
74   char *address = "example.com";
75   int length = 0;
76   int type = 0;
77   struct hostent h;
78   int rc = 0;
79 #if defined(HAVE_GETHOSTBYNAME_R_3) || \
80     defined(HAVE_GETHOSTBYNAME_R_3_REENTRANT)
81   struct hostent_data hdata;
82 #elif defined(HAVE_GETHOSTBYNAME_R_5) || \
83       defined(HAVE_GETHOSTBYNAME_R_5_REENTRANT) || \
84       defined(HAVE_GETHOSTBYNAME_R_6) || \
85       defined(HAVE_GETHOSTBYNAME_R_6_REENTRANT)
86   char buffer[8192];
87   int h_errnop;
88   struct hostent *hp;
89 #endif
90 
91 #if   defined(HAVE_GETHOSTBYNAME_R_3) || \
92       defined(HAVE_GETHOSTBYNAME_R_3_REENTRANT)
93   rc = gethostbyname_r(address, &h, &hdata);
94 #elif defined(HAVE_GETHOSTBYNAME_R_5) || \
95       defined(HAVE_GETHOSTBYNAME_R_5_REENTRANT)
96   rc = gethostbyname_r(address, &h, buffer, 8192, &h_errnop);
97   (void)hp; /* not used for test */
98 #elif defined(HAVE_GETHOSTBYNAME_R_6) || \
99       defined(HAVE_GETHOSTBYNAME_R_6_REENTRANT)
100   rc = gethostbyname_r(address, &h, buffer, 8192, &hp, &h_errnop);
101 #endif
102 
103   (void)length;
104   (void)type;
105   (void)rc;
106   return 0;
107 }
108 #endif
109 
110 #ifdef HAVE_IN_ADDR_T
111 #include <sys/types.h>
112 #include <sys/socket.h>
113 #include <arpa/inet.h>
main(void)114 int main(void)
115 {
116   if((in_addr_t *) 0)
117     return 0;
118   if(sizeof(in_addr_t))
119     return 0;
120   ;
121   return 0;
122 }
123 #endif
124 
125 #ifdef HAVE_BOOL_T
126 #ifdef HAVE_SYS_TYPES_H
127 #include <sys/types.h>
128 #endif
129 #ifdef HAVE_STDBOOL_H
130 #include <stdbool.h>
131 #endif
main(void)132 int main(void)
133 {
134   if(sizeof(bool *))
135     return 0;
136   ;
137   return 0;
138 }
139 #endif
140 
141 #ifdef STDC_HEADERS
142 #include <stdlib.h>
143 #include <stdarg.h>
144 #include <string.h>
145 #include <float.h>
main(void)146 int main(void) { return 0; }
147 #endif
148 
149 #ifdef HAVE_FILE_OFFSET_BITS
150 #ifdef _FILE_OFFSET_BITS
151 #undef _FILE_OFFSET_BITS
152 #endif
153 #define _FILE_OFFSET_BITS 64
154 #include <sys/types.h>
155  /* Check that off_t can represent 2**63 - 1 correctly.
156     We cannot simply define LARGE_OFF_T to be 9223372036854775807,
157     since some C++ compilers masquerading as C compilers
158     incorrectly reject 9223372036854775807.  */
159 #define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
160   int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
161                        && LARGE_OFF_T % 2147483647 == 1)
162                       ? 1 : -1];
main(void)163 int main(void) { return 0; }
164 #endif
165 
166 #ifdef HAVE_IOCTLSOCKET
167 /* includes start */
168 #ifdef _WIN32
169 #  include <winsock2.h>
170 #endif
main(void)171 int main(void)
172 {
173   /* ioctlsocket source code */
174   int socket;
175   unsigned long flags = ioctlsocket(socket, FIONBIO, &flags);
176   ;
177   return 0;
178 }
179 
180 #endif
181 
182 #ifdef HAVE_IOCTLSOCKET_CAMEL
183 /* includes start */
184 #ifdef _WIN32
185 #  include <winsock2.h>
186 #endif
main(void)187 int main(void)
188 {
189   /* IoctlSocket source code */
190   if(0 != IoctlSocket(0, 0, 0))
191     return 1;
192   ;
193   return 0;
194 }
195 #endif
196 
197 #ifdef HAVE_IOCTLSOCKET_CAMEL_FIONBIO
198 /* includes start */
199 #ifdef _WIN32
200 #  include <winsock2.h>
201 #endif
main(void)202 int main(void)
203 {
204   /* IoctlSocket source code */
205   long flags = 0;
206   if(0 != IoctlSocket(0, FIONBIO, &flags))
207     return 1;
208   ;
209   return 0;
210 }
211 #endif
212 
213 #ifdef HAVE_IOCTLSOCKET_FIONBIO
214 /* includes start */
215 #ifdef _WIN32
216 #  include <winsock2.h>
217 #endif
main(void)218 int main(void)
219 {
220   unsigned long flags = 0;
221   if(0 != ioctlsocket(0, FIONBIO, &flags))
222     return 1;
223   ;
224   return 0;
225 }
226 #endif
227 
228 #ifdef HAVE_IOCTL_FIONBIO
229 /* headers for FIONBIO test */
230 /* includes start */
231 #ifdef HAVE_SYS_TYPES_H
232 #  include <sys/types.h>
233 #endif
234 #ifdef HAVE_UNISTD_H
235 #  include <unistd.h>
236 #endif
237 #ifdef HAVE_SYS_SOCKET_H
238 #  include <sys/socket.h>
239 #endif
240 #ifdef HAVE_SYS_IOCTL_H
241 #  include <sys/ioctl.h>
242 #endif
243 #ifdef HAVE_STROPTS_H
244 #  include <stropts.h>
245 #endif
main(void)246 int main(void)
247 {
248   int flags = 0;
249   if(0 != ioctl(0, FIONBIO, &flags))
250     return 1;
251   ;
252   return 0;
253 }
254 #endif
255 
256 #ifdef HAVE_IOCTL_SIOCGIFADDR
257 /* headers for FIONBIO test */
258 /* includes start */
259 #ifdef HAVE_SYS_TYPES_H
260 #  include <sys/types.h>
261 #endif
262 #ifdef HAVE_UNISTD_H
263 #  include <unistd.h>
264 #endif
265 #ifdef HAVE_SYS_SOCKET_H
266 #  include <sys/socket.h>
267 #endif
268 #ifdef HAVE_SYS_IOCTL_H
269 #  include <sys/ioctl.h>
270 #endif
271 #ifdef HAVE_STROPTS_H
272 #  include <stropts.h>
273 #endif
274 #include <net/if.h>
main(void)275 int main(void)
276 {
277   struct ifreq ifr;
278   if(0 != ioctl(0, SIOCGIFADDR, &ifr))
279     return 1;
280   ;
281   return 0;
282 }
283 #endif
284 
285 #ifdef HAVE_SETSOCKOPT_SO_NONBLOCK
286 /* includes start */
287 #ifdef _WIN32
288 #  include <winsock2.h>
289 #endif
290 /* includes start */
291 #ifdef HAVE_SYS_TYPES_H
292 #  include <sys/types.h>
293 #endif
294 #ifdef HAVE_SYS_SOCKET_H
295 #  include <sys/socket.h>
296 #endif
297 /* includes end */
main(void)298 int main(void)
299 {
300   if(0 != setsockopt(0, SOL_SOCKET, SO_NONBLOCK, 0, 0))
301     return 1;
302   ;
303   return 0;
304 }
305 #endif
306 
307 #ifdef HAVE_GLIBC_STRERROR_R
308 #include <string.h>
309 #include <errno.h>
310 
check(char c)311 void check(char c) {}
312 
main(void)313 int main(void)
314 {
315   char buffer[1024];
316   /* This will not compile if strerror_r does not return a char* */
317   check(strerror_r(EACCES, buffer, sizeof(buffer))[0]);
318   return 0;
319 }
320 #endif
321 
322 #ifdef HAVE_POSIX_STRERROR_R
323 #include <string.h>
324 #include <errno.h>
325 
326 /* Float, because a pointer cannot be implicitly cast to float */
check(float f)327 void check(float f) {}
328 
main(void)329 int main(void)
330 {
331   char buffer[1024];
332   /* This will not compile if strerror_r does not return an int */
333   check(strerror_r(EACCES, buffer, sizeof(buffer)));
334   return 0;
335 }
336 #endif
337 
338 #ifdef HAVE_FSETXATTR_6
339 #include <sys/xattr.h> /* header from libc, not from libattr */
main(void)340 int main(void)
341 {
342   fsetxattr(0, 0, 0, 0, 0, 0);
343   return 0;
344 }
345 #endif
346 
347 #ifdef HAVE_FSETXATTR_5
348 #include <sys/xattr.h> /* header from libc, not from libattr */
main(void)349 int main(void)
350 {
351   fsetxattr(0, 0, 0, 0, 0);
352   return 0;
353 }
354 #endif
355 
356 #ifdef HAVE_CLOCK_GETTIME_MONOTONIC
357 #include <time.h>
main(void)358 int main(void)
359 {
360   struct timespec ts = {0, 0};
361   clock_gettime(CLOCK_MONOTONIC, &ts);
362   return 0;
363 }
364 #endif
365 
366 #ifdef HAVE_BUILTIN_AVAILABLE
main(void)367 int main(void)
368 {
369   if(__builtin_available(macOS 10.12, iOS 5.0, *)) {}
370   return 0;
371 }
372 #endif
373 
374 #ifdef HAVE_ATOMIC
375 /* includes start */
376 #ifdef HAVE_SYS_TYPES_H
377 #  include <sys/types.h>
378 #endif
379 #ifdef HAVE_UNISTD_H
380 #  include <unistd.h>
381 #endif
382 #ifdef HAVE_STDATOMIC_H
383 #  include <stdatomic.h>
384 #endif
385 /* includes end */
386 
main(void)387 int main(void)
388 {
389   _Atomic int i = 1;
390   i = 0;  /* Force an atomic-write operation. */
391   return i;
392 }
393 #endif
394 
395 #ifdef HAVE_WIN32_WINNT
396 /* includes start */
397 #ifdef _WIN32
398 #  ifndef NOGDI
399 #    define NOGDI
400 #  endif
401 #  include <windows.h>
402 #endif
403 /* includes end */
404 
405 #define enquote(x) #x
406 #define expand(x) enquote(x)
407 #pragma message("_WIN32_WINNT=" expand(_WIN32_WINNT))
408 
main(void)409 int main(void)
410 {
411   return 0;
412 }
413 #endif
414