setup_once.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #ifndef __SETUP_ONCE_H
  2. #define __SETUP_ONCE_H
  3. /* $Id$ */
  4. /* Copyright (C) 2004 - 2009 by Daniel Stenberg et al
  5. *
  6. * Permission to use, copy, modify, and distribute this software and its
  7. * documentation for any purpose and without fee is hereby granted, provided
  8. * that the above copyright notice appear in all copies and that both that
  9. * copyright notice and this permission notice appear in supporting
  10. * documentation, and that the name of M.I.T. not be used in advertising or
  11. * publicity pertaining to distribution of the software without specific,
  12. * written prior permission. M.I.T. makes no representations about the
  13. * suitability of this software for any purpose. It is provided "as is"
  14. * without express or implied warranty.
  15. */
  16. /********************************************************************
  17. * NOTICE *
  18. * ======== *
  19. * *
  20. * Content of header files lib/setup_once.h and ares/setup_once.h *
  21. * must be kept in sync. Modify the other one if you change this. *
  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. #include <errno.h>
  33. #ifdef HAVE_SYS_TYPES_H
  34. #include <sys/types.h>
  35. #endif
  36. #ifdef NEED_MALLOC_H
  37. #include <malloc.h>
  38. #endif
  39. #ifdef NEED_MEMORY_H
  40. #include <memory.h>
  41. #endif
  42. #ifdef HAVE_SYS_STAT_H
  43. #include <sys/stat.h>
  44. #endif
  45. #ifdef HAVE_SYS_TIME_H
  46. #include <sys/time.h>
  47. #ifdef TIME_WITH_SYS_TIME
  48. #include <time.h>
  49. #endif
  50. #else
  51. #ifdef HAVE_TIME_H
  52. #include <time.h>
  53. #endif
  54. #endif
  55. #ifdef WIN32
  56. #include <io.h>
  57. #include <fcntl.h>
  58. #endif
  59. #ifdef HAVE_STDBOOL_H
  60. #include <stdbool.h>
  61. #endif
  62. /*
  63. * Definition of timeval struct for platforms that don't have it.
  64. */
  65. #ifndef HAVE_STRUCT_TIMEVAL
  66. struct timeval {
  67. long tv_sec;
  68. long tv_usec;
  69. };
  70. #endif
  71. /*
  72. * If we have the MSG_NOSIGNAL define, make sure we use
  73. * it as the fourth argument of function send()
  74. */
  75. #ifdef HAVE_MSG_NOSIGNAL
  76. #define SEND_4TH_ARG MSG_NOSIGNAL
  77. #else
  78. #define SEND_4TH_ARG 0
  79. #endif
  80. #if defined(__minix)
  81. /* Minix doesn't support recv on TCP sockets */
  82. #define sread(x,y,z) (ssize_t)read((RECV_TYPE_ARG1)(x), \
  83. (RECV_TYPE_ARG2)(y), \
  84. (RECV_TYPE_ARG3)(z))
  85. #elif defined(HAVE_RECV)
  86. /*
  87. * The definitions for the return type and arguments types
  88. * of functions recv() and send() belong and come from the
  89. * configuration file. Do not define them in any other place.
  90. *
  91. * HAVE_RECV is defined if you have a function named recv()
  92. * which is used to read incoming data from sockets. If your
  93. * function has another name then don't define HAVE_RECV.
  94. *
  95. * If HAVE_RECV is defined then RECV_TYPE_ARG1, RECV_TYPE_ARG2,
  96. * RECV_TYPE_ARG3, RECV_TYPE_ARG4 and RECV_TYPE_RETV must also
  97. * be defined.
  98. *
  99. * HAVE_SEND is defined if you have a function named send()
  100. * which is used to write outgoing data on a connected socket.
  101. * If yours has another name then don't define HAVE_SEND.
  102. *
  103. * If HAVE_SEND is defined then SEND_TYPE_ARG1, SEND_QUAL_ARG2,
  104. * SEND_TYPE_ARG2, SEND_TYPE_ARG3, SEND_TYPE_ARG4 and
  105. * SEND_TYPE_RETV must also be defined.
  106. */
  107. #if !defined(RECV_TYPE_ARG1) || \
  108. !defined(RECV_TYPE_ARG2) || \
  109. !defined(RECV_TYPE_ARG3) || \
  110. !defined(RECV_TYPE_ARG4) || \
  111. !defined(RECV_TYPE_RETV)
  112. /* */
  113. Error Missing_definition_of_return_and_arguments_types_of_recv
  114. /* */
  115. #else
  116. #define sread(x,y,z) (ssize_t)recv((RECV_TYPE_ARG1)(x), \
  117. (RECV_TYPE_ARG2)(y), \
  118. (RECV_TYPE_ARG3)(z), \
  119. (RECV_TYPE_ARG4)(0))
  120. #endif
  121. #else /* HAVE_RECV */
  122. #ifndef sread
  123. /* */
  124. Error Missing_definition_of_macro_sread
  125. /* */
  126. #endif
  127. #endif /* HAVE_RECV */
  128. #if defined(__minix)
  129. /* Minix doesn't support send on TCP sockets */
  130. #define swrite(x,y,z) (ssize_t)write((SEND_TYPE_ARG1)(x), \
  131. (SEND_TYPE_ARG2)(y), \
  132. (SEND_TYPE_ARG3)(z))
  133. #elif defined(HAVE_SEND)
  134. #if !defined(SEND_TYPE_ARG1) || \
  135. !defined(SEND_QUAL_ARG2) || \
  136. !defined(SEND_TYPE_ARG2) || \
  137. !defined(SEND_TYPE_ARG3) || \
  138. !defined(SEND_TYPE_ARG4) || \
  139. !defined(SEND_TYPE_RETV)
  140. /* */
  141. Error Missing_definition_of_return_and_arguments_types_of_send
  142. /* */
  143. #else
  144. #define swrite(x,y,z) (ssize_t)send((SEND_TYPE_ARG1)(x), \
  145. (SEND_TYPE_ARG2)(y), \
  146. (SEND_TYPE_ARG3)(z), \
  147. (SEND_TYPE_ARG4)(SEND_4TH_ARG))
  148. #endif
  149. #else /* HAVE_SEND */
  150. #ifndef swrite
  151. /* */
  152. Error Missing_definition_of_macro_swrite
  153. /* */
  154. #endif
  155. #endif /* HAVE_SEND */
  156. #if 0
  157. #if defined(HAVE_RECVFROM)
  158. /*
  159. * Currently recvfrom is only used on udp sockets.
  160. */
  161. #if !defined(RECVFROM_TYPE_ARG1) || \
  162. !defined(RECVFROM_TYPE_ARG2) || \
  163. !defined(RECVFROM_TYPE_ARG3) || \
  164. !defined(RECVFROM_TYPE_ARG4) || \
  165. !defined(RECVFROM_TYPE_ARG5) || \
  166. !defined(RECVFROM_TYPE_ARG6) || \
  167. !defined(RECVFROM_TYPE_RETV)
  168. /* */
  169. Error Missing_definition_of_return_and_arguments_types_of_recvfrom
  170. /* */
  171. #else
  172. #define sreadfrom(s,b,bl,f,fl) (ssize_t)recvfrom((RECVFROM_TYPE_ARG1) (s), \
  173. (RECVFROM_TYPE_ARG2 *)(b), \
  174. (RECVFROM_TYPE_ARG3) (bl), \
  175. (RECVFROM_TYPE_ARG4) (0), \
  176. (RECVFROM_TYPE_ARG5 *)(f), \
  177. (RECVFROM_TYPE_ARG6 *)(fl))
  178. #endif
  179. #else /* HAVE_RECVFROM */
  180. #ifndef sreadfrom
  181. /* */
  182. Error Missing_definition_of_macro_sreadfrom
  183. /* */
  184. #endif
  185. #endif /* HAVE_RECVFROM */
  186. #ifdef RECVFROM_TYPE_ARG6_IS_VOID
  187. # define RECVFROM_ARG6_T int
  188. #else
  189. # define RECVFROM_ARG6_T RECVFROM_TYPE_ARG6
  190. #endif
  191. #endif /* if 0 */
  192. /*
  193. * Uppercase macro versions of ANSI/ISO is*() functions/macros which
  194. * avoid negative number inputs with argument byte codes > 127.
  195. */
  196. #define ISSPACE(x) (isspace((int) ((unsigned char)x)))
  197. #define ISDIGIT(x) (isdigit((int) ((unsigned char)x)))
  198. #define ISALNUM(x) (isalnum((int) ((unsigned char)x)))
  199. #define ISXDIGIT(x) (isxdigit((int) ((unsigned char)x)))
  200. #define ISGRAPH(x) (isgraph((int) ((unsigned char)x)))
  201. #define ISALPHA(x) (isalpha((int) ((unsigned char)x)))
  202. #define ISPRINT(x) (isprint((int) ((unsigned char)x)))
  203. #define ISUPPER(x) (isupper((int) ((unsigned char)x)))
  204. #define ISLOWER(x) (islower((int) ((unsigned char)x)))
  205. #define ISBLANK(x) (int)((((unsigned char)x) == ' ') || \
  206. (((unsigned char)x) == '\t'))
  207. /*
  208. * Typedef to 'unsigned char' if bool is not an available 'typedefed' type.
  209. */
  210. #ifndef HAVE_BOOL_T
  211. typedef unsigned char bool;
  212. #define HAVE_BOOL_T
  213. #endif
  214. /*
  215. * Default definition of uppercase TRUE and FALSE.
  216. */
  217. #ifndef TRUE
  218. #define TRUE 1
  219. #endif
  220. #ifndef FALSE
  221. #define FALSE 0
  222. #endif
  223. /*
  224. * Typedef to 'int' if sig_atomic_t is not an available 'typedefed' type.
  225. */
  226. #ifndef HAVE_SIG_ATOMIC_T
  227. typedef int sig_atomic_t;
  228. #define HAVE_SIG_ATOMIC_T
  229. #endif
  230. /*
  231. * Convenience SIG_ATOMIC_T definition
  232. */
  233. #ifdef HAVE_SIG_ATOMIC_T_VOLATILE
  234. #define SIG_ATOMIC_T static sig_atomic_t
  235. #else
  236. #define SIG_ATOMIC_T static volatile sig_atomic_t
  237. #endif
  238. /*
  239. * Default return type for signal handlers.
  240. */
  241. #ifndef RETSIGTYPE
  242. #define RETSIGTYPE void
  243. #endif
  244. /*
  245. * Macro used to include code only in debug builds.
  246. */
  247. #ifdef CURLDEBUG
  248. #define DEBUGF(x) x
  249. #else
  250. #define DEBUGF(x) do { } while (0)
  251. #endif
  252. /*
  253. * Macro used to include assertion code only in debug builds.
  254. */
  255. #if defined(CURLDEBUG) && defined(HAVE_ASSERT_H)
  256. #define DEBUGASSERT(x) assert(x)
  257. #else
  258. #define DEBUGASSERT(x) do { } while (0)
  259. #endif
  260. /*
  261. * Macro SOCKERRNO / SET_SOCKERRNO() returns / sets the *socket-related* errno
  262. * (or equivalent) on this platform to hide platform details to code using it.
  263. */
  264. #ifdef USE_WINSOCK
  265. #define SOCKERRNO ((int)WSAGetLastError())
  266. #define SET_SOCKERRNO(x) (WSASetLastError((int)(x)))
  267. #else
  268. #define SOCKERRNO (errno)
  269. #define SET_SOCKERRNO(x) (errno = (x))
  270. #endif
  271. /*
  272. * Macro ERRNO / SET_ERRNO() returns / sets the NOT *socket-related* errno
  273. * (or equivalent) on this platform to hide platform details to code using it.
  274. */
  275. #ifdef WIN32
  276. #define ERRNO ((int)GetLastError())
  277. #define SET_ERRNO(x) (SetLastError((DWORD)(x)))
  278. #else
  279. #define ERRNO (errno)
  280. #define SET_ERRNO(x) (errno = (x))
  281. #endif
  282. /*
  283. * Portable error number symbolic names defined to Winsock error codes.
  284. */
  285. #ifdef USE_WINSOCK
  286. #undef EBADF /* override definition in errno.h */
  287. #define EBADF WSAEBADF
  288. #undef EINTR /* override definition in errno.h */
  289. #define EINTR WSAEINTR
  290. #undef EINVAL /* override definition in errno.h */
  291. #define EINVAL WSAEINVAL
  292. #define EWOULDBLOCK WSAEWOULDBLOCK
  293. #define EINPROGRESS WSAEINPROGRESS
  294. #define EALREADY WSAEALREADY
  295. #define ENOTSOCK WSAENOTSOCK
  296. #define EDESTADDRREQ WSAEDESTADDRREQ
  297. #define EMSGSIZE WSAEMSGSIZE
  298. #define EPROTOTYPE WSAEPROTOTYPE
  299. #define ENOPROTOOPT WSAENOPROTOOPT
  300. #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
  301. #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
  302. #define EOPNOTSUPP WSAEOPNOTSUPP
  303. #define EPFNOSUPPORT WSAEPFNOSUPPORT
  304. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  305. #define EADDRINUSE WSAEADDRINUSE
  306. #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
  307. #define ENETDOWN WSAENETDOWN
  308. #define ENETUNREACH WSAENETUNREACH
  309. #define ENETRESET WSAENETRESET
  310. #define ECONNABORTED WSAECONNABORTED
  311. #define ECONNRESET WSAECONNRESET
  312. #define ENOBUFS WSAENOBUFS
  313. #define EISCONN WSAEISCONN
  314. #define ENOTCONN WSAENOTCONN
  315. #define ESHUTDOWN WSAESHUTDOWN
  316. #define ETOOMANYREFS WSAETOOMANYREFS
  317. #define ETIMEDOUT WSAETIMEDOUT
  318. #define ECONNREFUSED WSAECONNREFUSED
  319. #define ELOOP WSAELOOP
  320. #ifndef ENAMETOOLONG /* possible previous definition in errno.h */
  321. #define ENAMETOOLONG WSAENAMETOOLONG
  322. #endif
  323. #define EHOSTDOWN WSAEHOSTDOWN
  324. #define EHOSTUNREACH WSAEHOSTUNREACH
  325. #ifndef ENOTEMPTY /* possible previous definition in errno.h */
  326. #define ENOTEMPTY WSAENOTEMPTY
  327. #endif
  328. #define EPROCLIM WSAEPROCLIM
  329. #define EUSERS WSAEUSERS
  330. #define EDQUOT WSAEDQUOT
  331. #define ESTALE WSAESTALE
  332. #define EREMOTE WSAEREMOTE
  333. #endif
  334. /*
  335. * Actually use __32_getpwuid() on 64-bit VMS builds for getpwuid()
  336. */
  337. #if defined(VMS) && \
  338. defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
  339. #define getpwuid __32_getpwuid
  340. #endif
  341. /*
  342. * Macro argv_item_t hides platform details to code using it.
  343. */
  344. #ifdef VMS
  345. #define argv_item_t __char_ptr32
  346. #else
  347. #define argv_item_t char *
  348. #endif
  349. /*
  350. * We use this ZERO_NULL to avoid picky compiler warnings,
  351. * when assigning a NULL pointer to a function pointer var.
  352. */
  353. #define ZERO_NULL 0
  354. #endif /* __SETUP_ONCE_H */