42addd7185a01b03db6e47956ec9d3356f188e9a
[platform/upstream/cmake.git] / Utilities / cmcurl / CMake / CurlTests.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2022, 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  ***************************************************************************/
22 #ifdef TIME_WITH_SYS_TIME
23 /* Time with sys/time test */
24
25 #include <sys/types.h>
26 #include <sys/time.h>
27 #include <time.h>
28
29 int
30 main ()
31 {
32 if ((struct tm *) 0)
33 return 0;
34   ;
35   return 0;
36 }
37
38 #endif
39
40 #ifdef HAVE_FCNTL_O_NONBLOCK
41
42 /* headers for FCNTL_O_NONBLOCK test */
43 #include <sys/types.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 /* */
47 #if defined(sun) || defined(__sun__) || \
48     defined(__SUNPRO_C) || defined(__SUNPRO_CC)
49 # if defined(__SVR4) || defined(__srv4__)
50 #  define PLATFORM_SOLARIS
51 # else
52 #  define PLATFORM_SUNOS4
53 # endif
54 #endif
55 #if (defined(_AIX) || defined(__xlC__)) && !defined(_AIX41)
56 # define PLATFORM_AIX_V3
57 #endif
58 /* */
59 #if defined(PLATFORM_SUNOS4) || defined(PLATFORM_AIX_V3)
60 #error "O_NONBLOCK does not work on this platform"
61 #endif
62
63 int
64 main ()
65 {
66       /* O_NONBLOCK source test */
67       int flags = 0;
68       if(0 != fcntl(0, F_SETFL, flags | O_NONBLOCK))
69           return 1;
70       return 0;
71 }
72 #endif
73
74 /* tests for gethostbyname_r */
75 #if defined(HAVE_GETHOSTBYNAME_R_3_REENTRANT) || \
76     defined(HAVE_GETHOSTBYNAME_R_5_REENTRANT) || \
77     defined(HAVE_GETHOSTBYNAME_R_6_REENTRANT)
78 #   define _REENTRANT
79     /* no idea whether _REENTRANT is always set, just invent a new flag */
80 #   define TEST_GETHOSTBYFOO_REENTRANT
81 #endif
82 #if defined(HAVE_GETHOSTBYNAME_R_3) || \
83     defined(HAVE_GETHOSTBYNAME_R_5) || \
84     defined(HAVE_GETHOSTBYNAME_R_6) || \
85     defined(TEST_GETHOSTBYFOO_REENTRANT)
86 #include <sys/types.h>
87 #include <netdb.h>
88 int main(void)
89 {
90   char *address = "example.com";
91   int length = 0;
92   int type = 0;
93   struct hostent h;
94   int rc = 0;
95 #if defined(HAVE_GETHOSTBYNAME_R_3) || \
96     defined(HAVE_GETHOSTBYNAME_R_3_REENTRANT)
97   struct hostent_data hdata;
98 #elif defined(HAVE_GETHOSTBYNAME_R_5) || \
99       defined(HAVE_GETHOSTBYNAME_R_5_REENTRANT) || \
100       defined(HAVE_GETHOSTBYNAME_R_6) || \
101       defined(HAVE_GETHOSTBYNAME_R_6_REENTRANT)
102   char buffer[8192];
103   int h_errnop;
104   struct hostent *hp;
105 #endif
106
107 #if   defined(HAVE_GETHOSTBYNAME_R_3) || \
108       defined(HAVE_GETHOSTBYNAME_R_3_REENTRANT)
109   rc = gethostbyname_r(address, &h, &hdata);
110 #elif defined(HAVE_GETHOSTBYNAME_R_5) || \
111       defined(HAVE_GETHOSTBYNAME_R_5_REENTRANT)
112   rc = gethostbyname_r(address, &h, buffer, 8192, &h_errnop);
113   (void)hp; /* not used for test */
114 #elif defined(HAVE_GETHOSTBYNAME_R_6) || \
115       defined(HAVE_GETHOSTBYNAME_R_6_REENTRANT)
116   rc = gethostbyname_r(address, &h, buffer, 8192, &hp, &h_errnop);
117 #endif
118
119   (void)length;
120   (void)type;
121   (void)rc;
122   return 0;
123 }
124 #endif
125
126 #ifdef HAVE_SOCKLEN_T
127 #ifdef _WIN32
128 #include <ws2tcpip.h>
129 #else
130 #include <sys/types.h>
131 #include <sys/socket.h>
132 #endif
133 int
134 main ()
135 {
136 if ((socklen_t *) 0)
137   return 0;
138 if (sizeof (socklen_t))
139   return 0;
140   ;
141   return 0;
142 }
143 #endif
144 #ifdef HAVE_IN_ADDR_T
145 #include <sys/types.h>
146 #include <sys/socket.h>
147 #include <arpa/inet.h>
148
149 int
150 main ()
151 {
152 if ((in_addr_t *) 0)
153   return 0;
154 if (sizeof (in_addr_t))
155   return 0;
156   ;
157   return 0;
158 }
159 #endif
160
161 #ifdef HAVE_BOOL_T
162 #ifdef HAVE_SYS_TYPES_H
163 #include <sys/types.h>
164 #endif
165 #ifdef HAVE_STDBOOL_H
166 #include <stdbool.h>
167 #endif
168 int
169 main ()
170 {
171 if (sizeof (bool *) )
172   return 0;
173   ;
174   return 0;
175 }
176 #endif
177
178 #ifdef STDC_HEADERS
179 #include <stdlib.h>
180 #include <stdarg.h>
181 #include <string.h>
182 #include <float.h>
183 int main() { return 0; }
184 #endif
185 #ifdef HAVE_GETADDRINFO
186 #include <netdb.h>
187 #include <sys/types.h>
188 #include <sys/socket.h>
189
190 int main(void) {
191     struct addrinfo hints, *ai;
192     int error;
193
194     memset(&hints, 0, sizeof(hints));
195     hints.ai_family = AF_UNSPEC;
196     hints.ai_socktype = SOCK_STREAM;
197 #ifndef getaddrinfo
198     (void)getaddrinfo;
199 #endif
200     error = getaddrinfo("127.0.0.1", "8080", &hints, &ai);
201     if (error) {
202         return 1;
203     }
204     return 0;
205 }
206 #endif
207 #ifdef HAVE_FILE_OFFSET_BITS
208 #ifdef _FILE_OFFSET_BITS
209 #undef _FILE_OFFSET_BITS
210 #endif
211 #define _FILE_OFFSET_BITS 64
212 #include <sys/types.h>
213  /* Check that off_t can represent 2**63 - 1 correctly.
214     We can't simply define LARGE_OFF_T to be 9223372036854775807,
215     since some C++ compilers masquerading as C compilers
216     incorrectly reject 9223372036854775807.  */
217 #define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
218   int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
219                        && LARGE_OFF_T % 2147483647 == 1)
220                       ? 1 : -1];
221 int main () { ; return 0; }
222 #endif
223 #ifdef HAVE_IOCTLSOCKET
224 /* includes start */
225 #ifdef HAVE_WINDOWS_H
226 #  ifndef WIN32_LEAN_AND_MEAN
227 #    define WIN32_LEAN_AND_MEAN
228 #  endif
229 #  include <windows.h>
230 #  ifdef HAVE_WINSOCK2_H
231 #    include <winsock2.h>
232 #  endif
233 #endif
234
235 int
236 main ()
237 {
238
239 /* ioctlsocket source code */
240  int socket;
241  unsigned long flags = ioctlsocket(socket, FIONBIO, &flags);
242
243   ;
244   return 0;
245 }
246
247 #endif
248 #ifdef HAVE_IOCTLSOCKET_CAMEL
249 /* includes start */
250 #ifdef HAVE_WINDOWS_H
251 #  ifndef WIN32_LEAN_AND_MEAN
252 #    define WIN32_LEAN_AND_MEAN
253 #  endif
254 #  include <windows.h>
255 #  ifdef HAVE_WINSOCK2_H
256 #    include <winsock2.h>
257 #  endif
258 #endif
259
260 int
261 main ()
262 {
263
264 /* IoctlSocket source code */
265     if(0 != IoctlSocket(0, 0, 0))
266       return 1;
267   ;
268   return 0;
269 }
270 #endif
271 #ifdef HAVE_IOCTLSOCKET_CAMEL_FIONBIO
272 /* includes start */
273 #ifdef HAVE_WINDOWS_H
274 #  ifndef WIN32_LEAN_AND_MEAN
275 #    define WIN32_LEAN_AND_MEAN
276 #  endif
277 #  include <windows.h>
278 #  ifdef HAVE_WINSOCK2_H
279 #    include <winsock2.h>
280 #  endif
281 #endif
282
283 int
284 main ()
285 {
286
287 /* IoctlSocket source code */
288         long flags = 0;
289         if(0 != IoctlSocket(0, FIONBIO, &flags))
290           return 1;
291   ;
292   return 0;
293 }
294 #endif
295 #ifdef HAVE_IOCTLSOCKET_FIONBIO
296 /* includes start */
297 #ifdef HAVE_WINDOWS_H
298 #  ifndef WIN32_LEAN_AND_MEAN
299 #    define WIN32_LEAN_AND_MEAN
300 #  endif
301 #  include <windows.h>
302 #  ifdef HAVE_WINSOCK2_H
303 #    include <winsock2.h>
304 #  endif
305 #endif
306
307 int
308 main ()
309 {
310
311         int flags = 0;
312         if(0 != ioctlsocket(0, FIONBIO, &flags))
313           return 1;
314
315   ;
316   return 0;
317 }
318 #endif
319 #ifdef HAVE_IOCTL_FIONBIO
320 /* headers for FIONBIO test */
321 /* includes start */
322 #ifdef HAVE_SYS_TYPES_H
323 #  include <sys/types.h>
324 #endif
325 #ifdef HAVE_UNISTD_H
326 #  include <unistd.h>
327 #endif
328 #ifdef HAVE_SYS_SOCKET_H
329 #  include <sys/socket.h>
330 #endif
331 #ifdef HAVE_SYS_IOCTL_H
332 #  include <sys/ioctl.h>
333 #endif
334 #ifdef HAVE_STROPTS_H
335 #  include <stropts.h>
336 #endif
337
338 int
339 main ()
340 {
341
342         int flags = 0;
343         if(0 != ioctl(0, FIONBIO, &flags))
344           return 1;
345
346   ;
347   return 0;
348 }
349 #endif
350 #ifdef HAVE_IOCTL_SIOCGIFADDR
351 /* headers for FIONBIO test */
352 /* includes start */
353 #ifdef HAVE_SYS_TYPES_H
354 #  include <sys/types.h>
355 #endif
356 #ifdef HAVE_UNISTD_H
357 #  include <unistd.h>
358 #endif
359 #ifdef HAVE_SYS_SOCKET_H
360 #  include <sys/socket.h>
361 #endif
362 #ifdef HAVE_SYS_IOCTL_H
363 #  include <sys/ioctl.h>
364 #endif
365 #ifdef HAVE_STROPTS_H
366 #  include <stropts.h>
367 #endif
368 #include <net/if.h>
369
370 int
371 main ()
372 {
373         struct ifreq ifr;
374         if(0 != ioctl(0, SIOCGIFADDR, &ifr))
375           return 1;
376
377   ;
378   return 0;
379 }
380 #endif
381 #ifdef HAVE_SETSOCKOPT_SO_NONBLOCK
382 /* includes start */
383 #ifdef HAVE_WINDOWS_H
384 #  ifndef WIN32_LEAN_AND_MEAN
385 #    define WIN32_LEAN_AND_MEAN
386 #  endif
387 #  include <windows.h>
388 #  ifdef HAVE_WINSOCK2_H
389 #    include <winsock2.h>
390 #  endif
391 #endif
392 /* includes start */
393 #ifdef HAVE_SYS_TYPES_H
394 #  include <sys/types.h>
395 #endif
396 #ifdef HAVE_SYS_SOCKET_H
397 #  include <sys/socket.h>
398 #endif
399 /* includes end */
400
401 int
402 main ()
403 {
404         if(0 != setsockopt(0, SOL_SOCKET, SO_NONBLOCK, 0, 0))
405           return 1;
406   ;
407   return 0;
408 }
409 #endif
410 #ifdef HAVE_GLIBC_STRERROR_R
411 #include <string.h>
412 #include <errno.h>
413
414 void check(char c) {}
415
416 int
417 main () {
418   char buffer[1024];
419   /* This will not compile if strerror_r does not return a char* */
420   check(strerror_r(EACCES, buffer, sizeof(buffer))[0]);
421   return 0;
422 }
423 #endif
424 #ifdef HAVE_POSIX_STRERROR_R
425 #include <string.h>
426 #include <errno.h>
427
428 /* float, because a pointer can't be implicitly cast to float */
429 void check(float f) {}
430
431 int
432 main () {
433   char buffer[1024];
434   /* This will not compile if strerror_r does not return an int */
435   check(strerror_r(EACCES, buffer, sizeof(buffer)));
436   return 0;
437 }
438 #endif
439 #ifdef HAVE_FSETXATTR_6
440 #include <sys/xattr.h> /* header from libc, not from libattr */
441 int
442 main() {
443   fsetxattr(0, 0, 0, 0, 0, 0);
444   return 0;
445 }
446 #endif
447 #ifdef HAVE_FSETXATTR_5
448 #include <sys/xattr.h> /* header from libc, not from libattr */
449 int
450 main() {
451   fsetxattr(0, 0, 0, 0, 0);
452   return 0;
453 }
454 #endif
455 #ifdef HAVE_CLOCK_GETTIME_MONOTONIC
456 #include <time.h>
457 int
458 main() {
459   struct timespec ts = {0, 0};
460   clock_gettime(CLOCK_MONOTONIC, &ts);
461   return 0;
462 }
463 #endif
464 #ifdef HAVE_BUILTIN_AVAILABLE
465 int
466 main() {
467   if(__builtin_available(macOS 10.12, *)) {}
468   return 0;
469 }
470 #endif
471 #ifdef HAVE_VARIADIC_MACROS_C99
472 #define c99_vmacro3(first, ...) fun3(first, __VA_ARGS__)
473 #define c99_vmacro2(first, ...) fun2(first, __VA_ARGS__)
474
475 int fun3(int arg1, int arg2, int arg3);
476 int fun2(int arg1, int arg2);
477
478 int fun3(int arg1, int arg2, int arg3) {
479   return arg1 + arg2 + arg3;
480 }
481 int fun2(int arg1, int arg2) {
482   return arg1 + arg2;
483 }
484
485 int
486 main() {
487   int res3 = c99_vmacro3(1, 2, 3);
488   int res2 = c99_vmacro2(1, 2);
489   (void)res3;
490   (void)res2;
491   return 0;
492 }
493 #endif
494 #ifdef HAVE_VARIADIC_MACROS_GCC
495 #define gcc_vmacro3(first, args...) fun3(first, args)
496 #define gcc_vmacro2(first, args...) fun2(first, args)
497
498 int fun3(int arg1, int arg2, int arg3);
499 int fun2(int arg1, int arg2);
500
501 int fun3(int arg1, int arg2, int arg3) {
502   return arg1 + arg2 + arg3;
503 }
504 int fun2(int arg1, int arg2) {
505   return arg1 + arg2;
506 }
507
508 int
509 main() {
510   int res3 = gcc_vmacro3(1, 2, 3);
511   int res2 = gcc_vmacro2(1, 2);
512   (void)res3;
513   (void)res2;
514   return 0;
515 }
516 #endif