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