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