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