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