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