• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __SETUP_ONCE_H
2 #define __SETUP_ONCE_H
3 
4 
5 /* Copyright (C) 2004 - 2013 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) (ares_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) (ares_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) (ares_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) (ares_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) (ares_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 #elif defined(HAVE_CLOSE_S)
264 #  define sclose(x)  close_s((x))
265 #else
266 #  define sclose(x)  close((x))
267 #endif
268 
269 
270 /*
271  * Uppercase macro versions of ANSI/ISO is*() functions/macros which
272  * avoid negative number inputs with argument byte codes > 127.
273  */
274 
275 #define ISSPACE(x)  (isspace((int)  ((unsigned char)x)))
276 #define ISDIGIT(x)  (isdigit((int)  ((unsigned char)x)))
277 #define ISALNUM(x)  (isalnum((int)  ((unsigned char)x)))
278 #define ISXDIGIT(x) (isxdigit((int) ((unsigned char)x)))
279 #define ISGRAPH(x)  (isgraph((int)  ((unsigned char)x)))
280 #define ISALPHA(x)  (isalpha((int)  ((unsigned char)x)))
281 #define ISPRINT(x)  (isprint((int)  ((unsigned char)x)))
282 #define ISUPPER(x)  (isupper((int)  ((unsigned char)x)))
283 #define ISLOWER(x)  (islower((int)  ((unsigned char)x)))
284 #define ISASCII(x)  (isascii((int)  ((unsigned char)x)))
285 
286 #define ISBLANK(x)  (int)((((unsigned char)x) == ' ') || \
287                           (((unsigned char)x) == '\t'))
288 
289 #define TOLOWER(x)  (tolower((int)  ((unsigned char)x)))
290 
291 
292 /*
293  * 'bool' stuff compatible with HP-UX headers.
294  */
295 
296 #if defined(__hpux) && !defined(HAVE_BOOL_T)
297    typedef int bool;
298 #  define false 0
299 #  define true 1
300 #  define HAVE_BOOL_T
301 #endif
302 
303 
304 /*
305  * 'bool' exists on platforms with <stdbool.h>, i.e. C99 platforms.
306  * On non-C99 platforms there's no bool, so define an enum for that.
307  * On C99 platforms 'false' and 'true' also exist. Enum uses a
308  * global namespace though, so use bool_false and bool_true.
309  */
310 
311 #ifndef HAVE_BOOL_T
312   typedef enum {
313       bool_false = 0,
314       bool_true  = 1
315   } bool;
316 
317 /*
318  * Use a define to let 'true' and 'false' use those enums.  There
319  * are currently no use of true and false in libcurl proper, but
320  * there are some in the examples. This will cater for any later
321  * code happening to use true and false.
322  */
323 #  define false bool_false
324 #  define true  bool_true
325 #  define HAVE_BOOL_T
326 #endif
327 
328 
329 /*
330  * Redefine TRUE and FALSE too, to catch current use. With this
331  * change, 'bool found = 1' will give a warning on MIPSPro, but
332  * 'bool found = TRUE' will not. Change tested on IRIX/MIPSPro,
333  * AIX 5.1/Xlc, Tru64 5.1/cc, w/make test too.
334  */
335 
336 #ifndef TRUE
337 #define TRUE true
338 #endif
339 #ifndef FALSE
340 #define FALSE false
341 #endif
342 
343 
344 /*
345  * Macro WHILE_FALSE may be used to build single-iteration do-while loops,
346  * avoiding compiler warnings. Mostly intended for other macro definitions.
347  */
348 
349 #define WHILE_FALSE  while(0)
350 
351 #if defined(_MSC_VER) && !defined(__POCC__)
352 #  undef WHILE_FALSE
353 #  if (_MSC_VER < 1500)
354 #    define WHILE_FALSE  while(1, 0)
355 #  else
356 #    define WHILE_FALSE \
357 __pragma(warning(push)) \
358 __pragma(warning(disable:4127)) \
359 while(0) \
360 __pragma(warning(pop))
361 #  endif
362 #endif
363 
364 
365 /*
366  * Typedef to 'int' if sig_atomic_t is not an available 'typedefed' type.
367  */
368 
369 #ifndef HAVE_SIG_ATOMIC_T
370 typedef int sig_atomic_t;
371 #define HAVE_SIG_ATOMIC_T
372 #endif
373 
374 
375 /*
376  * Convenience SIG_ATOMIC_T definition
377  */
378 
379 #ifdef HAVE_SIG_ATOMIC_T_VOLATILE
380 #define SIG_ATOMIC_T static sig_atomic_t
381 #else
382 #define SIG_ATOMIC_T static volatile sig_atomic_t
383 #endif
384 
385 
386 /*
387  * Default return type for signal handlers.
388  */
389 
390 #ifndef RETSIGTYPE
391 #define RETSIGTYPE void
392 #endif
393 
394 
395 /*
396  * Macro used to include code only in debug builds.
397  */
398 
399 #ifdef DEBUGBUILD
400 #define DEBUGF(x) x
401 #else
402 #define DEBUGF(x) do { } WHILE_FALSE
403 #endif
404 
405 
406 /*
407  * Macro used to include assertion code only in debug builds.
408  */
409 
410 #if defined(DEBUGBUILD) && defined(HAVE_ASSERT_H)
411 #define DEBUGASSERT(x) assert(x)
412 #else
413 #define DEBUGASSERT(x) do { } WHILE_FALSE
414 #endif
415 
416 
417 /*
418  * Macro SOCKERRNO / SET_SOCKERRNO() returns / sets the *socket-related* errno
419  * (or equivalent) on this platform to hide platform details to code using it.
420  */
421 
422 #ifdef USE_WINSOCK
423 #define SOCKERRNO         ((int)WSAGetLastError())
424 #define SET_SOCKERRNO(x)  (WSASetLastError((int)(x)))
425 #else
426 #define SOCKERRNO         (errno)
427 #define SET_SOCKERRNO(x)  (errno = (x))
428 #endif
429 
430 
431 /*
432  * Macro ERRNO / SET_ERRNO() returns / sets the NOT *socket-related* errno
433  * (or equivalent) on this platform to hide platform details to code using it.
434  */
435 
436 #if defined(WIN32) && !defined(WATT32)
437 #define ERRNO         ((int)GetLastError())
438 #define SET_ERRNO(x)  (SetLastError((DWORD)(x)))
439 #else
440 #define ERRNO         (errno)
441 #define SET_ERRNO(x)  (errno = (x))
442 #endif
443 
444 
445 /*
446  * Portable error number symbolic names defined to Winsock error codes.
447  */
448 
449 #ifdef USE_WINSOCK
450 #undef  EBADF            /* override definition in errno.h */
451 #define EBADF            WSAEBADF
452 #undef  EINTR            /* override definition in errno.h */
453 #define EINTR            WSAEINTR
454 #undef  EINVAL           /* override definition in errno.h */
455 #define EINVAL           WSAEINVAL
456 #undef  EWOULDBLOCK      /* override definition in errno.h */
457 #define EWOULDBLOCK      WSAEWOULDBLOCK
458 #undef  EINPROGRESS      /* override definition in errno.h */
459 #define EINPROGRESS      WSAEINPROGRESS
460 #undef  EALREADY         /* override definition in errno.h */
461 #define EALREADY         WSAEALREADY
462 #undef  ENOTSOCK         /* override definition in errno.h */
463 #define ENOTSOCK         WSAENOTSOCK
464 #undef  EDESTADDRREQ     /* override definition in errno.h */
465 #define EDESTADDRREQ     WSAEDESTADDRREQ
466 #undef  EMSGSIZE         /* override definition in errno.h */
467 #define EMSGSIZE         WSAEMSGSIZE
468 #undef  EPROTOTYPE       /* override definition in errno.h */
469 #define EPROTOTYPE       WSAEPROTOTYPE
470 #undef  ENOPROTOOPT      /* override definition in errno.h */
471 #define ENOPROTOOPT      WSAENOPROTOOPT
472 #undef  EPROTONOSUPPORT  /* override definition in errno.h */
473 #define EPROTONOSUPPORT  WSAEPROTONOSUPPORT
474 #define ESOCKTNOSUPPORT  WSAESOCKTNOSUPPORT
475 #undef  EOPNOTSUPP       /* override definition in errno.h */
476 #define EOPNOTSUPP       WSAEOPNOTSUPP
477 #define EPFNOSUPPORT     WSAEPFNOSUPPORT
478 #undef  EAFNOSUPPORT     /* override definition in errno.h */
479 #define EAFNOSUPPORT     WSAEAFNOSUPPORT
480 #undef  EADDRINUSE       /* override definition in errno.h */
481 #define EADDRINUSE       WSAEADDRINUSE
482 #undef  EADDRNOTAVAIL    /* override definition in errno.h */
483 #define EADDRNOTAVAIL    WSAEADDRNOTAVAIL
484 #undef  ENETDOWN         /* override definition in errno.h */
485 #define ENETDOWN         WSAENETDOWN
486 #undef  ENETUNREACH      /* override definition in errno.h */
487 #define ENETUNREACH      WSAENETUNREACH
488 #undef  ENETRESET        /* override definition in errno.h */
489 #define ENETRESET        WSAENETRESET
490 #undef  ECONNABORTED     /* override definition in errno.h */
491 #define ECONNABORTED     WSAECONNABORTED
492 #undef  ECONNRESET       /* override definition in errno.h */
493 #define ECONNRESET       WSAECONNRESET
494 #undef  ENOBUFS          /* override definition in errno.h */
495 #define ENOBUFS          WSAENOBUFS
496 #undef  EISCONN          /* override definition in errno.h */
497 #define EISCONN          WSAEISCONN
498 #undef  ENOTCONN         /* override definition in errno.h */
499 #define ENOTCONN         WSAENOTCONN
500 #define ESHUTDOWN        WSAESHUTDOWN
501 #define ETOOMANYREFS     WSAETOOMANYREFS
502 #undef  ETIMEDOUT        /* override definition in errno.h */
503 #define ETIMEDOUT        WSAETIMEDOUT
504 #undef  ECONNREFUSED     /* override definition in errno.h */
505 #define ECONNREFUSED     WSAECONNREFUSED
506 #undef  ELOOP            /* override definition in errno.h */
507 #define ELOOP            WSAELOOP
508 #ifndef ENAMETOOLONG     /* possible previous definition in errno.h */
509 #define ENAMETOOLONG     WSAENAMETOOLONG
510 #endif
511 #define EHOSTDOWN        WSAEHOSTDOWN
512 #undef  EHOSTUNREACH     /* override definition in errno.h */
513 #define EHOSTUNREACH     WSAEHOSTUNREACH
514 #ifndef ENOTEMPTY        /* possible previous definition in errno.h */
515 #define ENOTEMPTY        WSAENOTEMPTY
516 #endif
517 #define EPROCLIM         WSAEPROCLIM
518 #define EUSERS           WSAEUSERS
519 #define EDQUOT           WSAEDQUOT
520 #define ESTALE           WSAESTALE
521 #define EREMOTE          WSAEREMOTE
522 #endif
523 
524 
525 /*
526  *  Actually use __32_getpwuid() on 64-bit VMS builds for getpwuid()
527  */
528 
529 #if defined(__VMS) && \
530     defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
531 #define getpwuid __32_getpwuid
532 #endif
533 
534 
535 /*
536  * Macro argv_item_t hides platform details to code using it.
537  */
538 
539 #ifdef __VMS
540 #define argv_item_t  __char_ptr32
541 #else
542 #define argv_item_t  char *
543 #endif
544 
545 
546 /*
547  * We use this ZERO_NULL to avoid picky compiler warnings,
548  * when assigning a NULL pointer to a function pointer var.
549  */
550 
551 #define ZERO_NULL 0
552 
553 
554 #endif /* __SETUP_ONCE_H */
555