Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Utilities / cmcurl / lib / curl_setup_once.h
1 #ifndef HEADER_CURL_SETUP_ONCE_H
2 #define HEADER_CURL_SETUP_ONCE_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at https://curl.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  * SPDX-License-Identifier: curl
24  *
25  ***************************************************************************/
26
27
28 /*
29  * Inclusion of common header files.
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <time.h>
37
38 #ifdef HAVE_ERRNO_H
39 #include <errno.h>
40 #endif
41
42 #ifdef HAVE_SYS_TYPES_H
43 #include <sys/types.h>
44 #endif
45
46 #ifdef NEED_MALLOC_H
47 #include <malloc.h>
48 #endif
49
50 #ifdef NEED_MEMORY_H
51 #include <memory.h>
52 #endif
53
54 #ifdef HAVE_SYS_STAT_H
55 #include <sys/stat.h>
56 #endif
57
58 #ifdef HAVE_SYS_TIME_H
59 #include <sys/time.h>
60 #endif
61
62 #ifdef WIN32
63 #include <io.h>
64 #include <fcntl.h>
65 #endif
66
67 #if defined(HAVE_STDBOOL_H) && defined(HAVE_BOOL_T)
68 #include <stdbool.h>
69 #endif
70
71 #ifdef HAVE_UNISTD_H
72 #include <unistd.h>
73 #endif
74
75 #ifdef __hpux
76 #  if !defined(_XOPEN_SOURCE_EXTENDED) || defined(_KERNEL)
77 #    ifdef _APP32_64BIT_OFF_T
78 #      define OLD_APP32_64BIT_OFF_T _APP32_64BIT_OFF_T
79 #      undef _APP32_64BIT_OFF_T
80 #    else
81 #      undef OLD_APP32_64BIT_OFF_T
82 #    endif
83 #  endif
84 #endif
85
86 #ifdef HAVE_SYS_SOCKET_H
87 #include <sys/socket.h>
88 #endif
89
90 #include "functypes.h"
91
92 #ifdef __hpux
93 #  if !defined(_XOPEN_SOURCE_EXTENDED) || defined(_KERNEL)
94 #    ifdef OLD_APP32_64BIT_OFF_T
95 #      define _APP32_64BIT_OFF_T OLD_APP32_64BIT_OFF_T
96 #      undef OLD_APP32_64BIT_OFF_T
97 #    endif
98 #  endif
99 #endif
100
101 /*
102  * Definition of timeval struct for platforms that don't have it.
103  */
104
105 #ifndef HAVE_STRUCT_TIMEVAL
106 struct timeval {
107  long tv_sec;
108  long tv_usec;
109 };
110 #endif
111
112
113 /*
114  * If we have the MSG_NOSIGNAL define, make sure we use
115  * it as the fourth argument of function send()
116  */
117
118 #ifdef HAVE_MSG_NOSIGNAL
119 #define SEND_4TH_ARG MSG_NOSIGNAL
120 #else
121 #define SEND_4TH_ARG 0
122 #endif
123
124
125 #if defined(__minix)
126 /* Minix doesn't support recv on TCP sockets */
127 #define sread(x,y,z) (ssize_t)read((RECV_TYPE_ARG1)(x), \
128                                    (RECV_TYPE_ARG2)(y), \
129                                    (RECV_TYPE_ARG3)(z))
130
131 #elif defined(HAVE_RECV)
132 /*
133  * The definitions for the return type and arguments types
134  * of functions recv() and send() belong and come from the
135  * configuration file. Do not define them in any other place.
136  *
137  * HAVE_RECV is defined if you have a function named recv()
138  * which is used to read incoming data from sockets. If your
139  * function has another name then don't define HAVE_RECV.
140  *
141  * If HAVE_RECV is defined then RECV_TYPE_ARG1, RECV_TYPE_ARG2,
142  * RECV_TYPE_ARG3, RECV_TYPE_ARG4 and RECV_TYPE_RETV must also
143  * be defined.
144  *
145  * HAVE_SEND is defined if you have a function named send()
146  * which is used to write outgoing data on a connected socket.
147  * If yours has another name then don't define HAVE_SEND.
148  *
149  * If HAVE_SEND is defined then SEND_TYPE_ARG1, SEND_QUAL_ARG2,
150  * SEND_TYPE_ARG2, SEND_TYPE_ARG3, SEND_TYPE_ARG4 and
151  * SEND_TYPE_RETV must also be defined.
152  */
153
154 #define sread(x,y,z) (ssize_t)recv((RECV_TYPE_ARG1)(x), \
155                                    (RECV_TYPE_ARG2)(y), \
156                                    (RECV_TYPE_ARG3)(z), \
157                                    (RECV_TYPE_ARG4)(0))
158 #else /* HAVE_RECV */
159 #ifndef sread
160   /* */
161   Error Missing_definition_of_macro_sread
162   /* */
163 #endif
164 #endif /* HAVE_RECV */
165
166
167 #if defined(__minix)
168 /* Minix doesn't support send on TCP sockets */
169 #define swrite(x,y,z) (ssize_t)write((SEND_TYPE_ARG1)(x), \
170                                     (SEND_TYPE_ARG2)(y), \
171                                     (SEND_TYPE_ARG3)(z))
172
173 #elif defined(HAVE_SEND)
174 #define swrite(x,y,z) (ssize_t)send((SEND_TYPE_ARG1)(x), \
175                                     (SEND_QUAL_ARG2 SEND_TYPE_ARG2)(y), \
176                                     (SEND_TYPE_ARG3)(z), \
177                                     (SEND_TYPE_ARG4)(SEND_4TH_ARG))
178 #else /* HAVE_SEND */
179 #ifndef swrite
180   /* */
181   Error Missing_definition_of_macro_swrite
182   /* */
183 #endif
184 #endif /* HAVE_SEND */
185
186
187 /*
188  * Function-like macro definition used to close a socket.
189  */
190
191 #if defined(HAVE_CLOSESOCKET)
192 #  define sclose(x)  closesocket((x))
193 #elif defined(HAVE_CLOSESOCKET_CAMEL)
194 #  define sclose(x)  CloseSocket((x))
195 #elif defined(HAVE_CLOSE_S)
196 #  define sclose(x)  close_s((x))
197 #elif defined(USE_LWIPSOCK)
198 #  define sclose(x)  lwip_close((x))
199 #else
200 #  define sclose(x)  close((x))
201 #endif
202
203 /*
204  * Stack-independent version of fcntl() on sockets:
205  */
206 #if defined(USE_LWIPSOCK)
207 #  define sfcntl  lwip_fcntl
208 #else
209 #  define sfcntl  fcntl
210 #endif
211
212 /*
213  * 'bool' stuff compatible with HP-UX headers.
214  */
215
216 #if defined(__hpux) && !defined(HAVE_BOOL_T)
217    typedef int bool;
218 #  define false 0
219 #  define true 1
220 #  define HAVE_BOOL_T
221 #endif
222
223
224 /*
225  * 'bool' exists on platforms with <stdbool.h>, i.e. C99 platforms.
226  * On non-C99 platforms there's no bool, so define an enum for that.
227  * On C99 platforms 'false' and 'true' also exist. Enum uses a
228  * global namespace though, so use bool_false and bool_true.
229  */
230
231 #ifndef HAVE_BOOL_T
232   typedef enum {
233       bool_false = 0,
234       bool_true  = 1
235   } bool;
236
237 /*
238  * Use a define to let 'true' and 'false' use those enums.  There
239  * are currently no use of true and false in libcurl proper, but
240  * there are some in the examples. This will cater for any later
241  * code happening to use true and false.
242  */
243 #  define false bool_false
244 #  define true  bool_true
245 #  define HAVE_BOOL_T
246 #endif
247
248 /* the type we use for storing a single boolean bit */
249 #ifdef _MSC_VER
250 typedef bool bit;
251 #define BIT(x) bool x
252 #else
253 typedef unsigned int bit;
254 #define BIT(x) bit x:1
255 #endif
256
257 /*
258  * Redefine TRUE and FALSE too, to catch current use. With this
259  * change, 'bool found = 1' will give a warning on MIPSPro, but
260  * 'bool found = TRUE' will not. Change tested on IRIX/MIPSPro,
261  * AIX 5.1/Xlc, Tru64 5.1/cc, w/make test too.
262  */
263
264 #ifndef TRUE
265 #define TRUE true
266 #endif
267 #ifndef FALSE
268 #define FALSE false
269 #endif
270
271 #include "curl_ctype.h"
272
273
274 /*
275  * Macro used to include code only in debug builds.
276  */
277
278 #ifdef DEBUGBUILD
279 #define DEBUGF(x) x
280 #else
281 #define DEBUGF(x) do { } while(0)
282 #endif
283
284
285 /*
286  * Macro used to include assertion code only in debug builds.
287  */
288
289 #undef DEBUGASSERT
290 #if defined(DEBUGBUILD) && defined(HAVE_ASSERT_H)
291 #define DEBUGASSERT(x) assert(x)
292 #else
293 #define DEBUGASSERT(x) do { } while(0)
294 #endif
295
296
297 /*
298  * Macro SOCKERRNO / SET_SOCKERRNO() returns / sets the *socket-related* errno
299  * (or equivalent) on this platform to hide platform details to code using it.
300  */
301
302 #ifdef USE_WINSOCK
303 #define SOCKERRNO         ((int)WSAGetLastError())
304 #define SET_SOCKERRNO(x)  (WSASetLastError((int)(x)))
305 #else
306 #define SOCKERRNO         (errno)
307 #define SET_SOCKERRNO(x)  (errno = (x))
308 #endif
309
310
311 /*
312  * Portable error number symbolic names defined to Winsock error codes.
313  */
314
315 #ifdef USE_WINSOCK
316 #undef  EBADF            /* override definition in errno.h */
317 #define EBADF            WSAEBADF
318 #undef  EINTR            /* override definition in errno.h */
319 #define EINTR            WSAEINTR
320 #undef  EINVAL           /* override definition in errno.h */
321 #define EINVAL           WSAEINVAL
322 #undef  EWOULDBLOCK      /* override definition in errno.h */
323 #define EWOULDBLOCK      WSAEWOULDBLOCK
324 #undef  EINPROGRESS      /* override definition in errno.h */
325 #define EINPROGRESS      WSAEINPROGRESS
326 #undef  EALREADY         /* override definition in errno.h */
327 #define EALREADY         WSAEALREADY
328 #undef  ENOTSOCK         /* override definition in errno.h */
329 #define ENOTSOCK         WSAENOTSOCK
330 #undef  EDESTADDRREQ     /* override definition in errno.h */
331 #define EDESTADDRREQ     WSAEDESTADDRREQ
332 #undef  EMSGSIZE         /* override definition in errno.h */
333 #define EMSGSIZE         WSAEMSGSIZE
334 #undef  EPROTOTYPE       /* override definition in errno.h */
335 #define EPROTOTYPE       WSAEPROTOTYPE
336 #undef  ENOPROTOOPT      /* override definition in errno.h */
337 #define ENOPROTOOPT      WSAENOPROTOOPT
338 #undef  EPROTONOSUPPORT  /* override definition in errno.h */
339 #define EPROTONOSUPPORT  WSAEPROTONOSUPPORT
340 #define ESOCKTNOSUPPORT  WSAESOCKTNOSUPPORT
341 #undef  EOPNOTSUPP       /* override definition in errno.h */
342 #define EOPNOTSUPP       WSAEOPNOTSUPP
343 #define EPFNOSUPPORT     WSAEPFNOSUPPORT
344 #undef  EAFNOSUPPORT     /* override definition in errno.h */
345 #define EAFNOSUPPORT     WSAEAFNOSUPPORT
346 #undef  EADDRINUSE       /* override definition in errno.h */
347 #define EADDRINUSE       WSAEADDRINUSE
348 #undef  EADDRNOTAVAIL    /* override definition in errno.h */
349 #define EADDRNOTAVAIL    WSAEADDRNOTAVAIL
350 #undef  ENETDOWN         /* override definition in errno.h */
351 #define ENETDOWN         WSAENETDOWN
352 #undef  ENETUNREACH      /* override definition in errno.h */
353 #define ENETUNREACH      WSAENETUNREACH
354 #undef  ENETRESET        /* override definition in errno.h */
355 #define ENETRESET        WSAENETRESET
356 #undef  ECONNABORTED     /* override definition in errno.h */
357 #define ECONNABORTED     WSAECONNABORTED
358 #undef  ECONNRESET       /* override definition in errno.h */
359 #define ECONNRESET       WSAECONNRESET
360 #undef  ENOBUFS          /* override definition in errno.h */
361 #define ENOBUFS          WSAENOBUFS
362 #undef  EISCONN          /* override definition in errno.h */
363 #define EISCONN          WSAEISCONN
364 #undef  ENOTCONN         /* override definition in errno.h */
365 #define ENOTCONN         WSAENOTCONN
366 #define ESHUTDOWN        WSAESHUTDOWN
367 #define ETOOMANYREFS     WSAETOOMANYREFS
368 #undef  ETIMEDOUT        /* override definition in errno.h */
369 #define ETIMEDOUT        WSAETIMEDOUT
370 #undef  ECONNREFUSED     /* override definition in errno.h */
371 #define ECONNREFUSED     WSAECONNREFUSED
372 #undef  ELOOP            /* override definition in errno.h */
373 #define ELOOP            WSAELOOP
374 #ifndef ENAMETOOLONG     /* possible previous definition in errno.h */
375 #define ENAMETOOLONG     WSAENAMETOOLONG
376 #endif
377 #define EHOSTDOWN        WSAEHOSTDOWN
378 #undef  EHOSTUNREACH     /* override definition in errno.h */
379 #define EHOSTUNREACH     WSAEHOSTUNREACH
380 #ifndef ENOTEMPTY        /* possible previous definition in errno.h */
381 #define ENOTEMPTY        WSAENOTEMPTY
382 #endif
383 #define EPROCLIM         WSAEPROCLIM
384 #define EUSERS           WSAEUSERS
385 #define EDQUOT           WSAEDQUOT
386 #define ESTALE           WSAESTALE
387 #define EREMOTE          WSAEREMOTE
388 #endif
389
390 /*
391  * Macro argv_item_t hides platform details to code using it.
392  */
393
394 #ifdef __VMS
395 #define argv_item_t  __char_ptr32
396 #elif defined(_UNICODE)
397 #define argv_item_t wchar_t *
398 #else
399 #define argv_item_t  char *
400 #endif
401
402
403 /*
404  * We use this ZERO_NULL to avoid picky compiler warnings,
405  * when assigning a NULL pointer to a function pointer var.
406  */
407
408 #define ZERO_NULL 0
409
410
411 #endif /* HEADER_CURL_SETUP_ONCE_H */