2
0

curl_setup_once.h 15 KB

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