setup_once.h 14 KB

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