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