compat.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  14. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef COMPAT_H
  17. #define COMPAT_H
  18. #ifndef __GNUC__
  19. #define __attribute__(a)
  20. #endif
  21. #ifndef __unused
  22. #define __unused __attribute__ ((__unused__))
  23. #endif
  24. #ifndef __dead
  25. #define __dead __attribute__ ((__noreturn__))
  26. #endif
  27. #ifndef __packed
  28. #define __packed __attribute__ ((__packed__))
  29. #endif
  30. #ifndef ECHOPRT
  31. #define ECHOPRT 0
  32. #endif
  33. #ifndef HAVE_BSD_TYPES
  34. typedef uint8_t u_int8_t;
  35. typedef uint16_t u_int16_t;
  36. typedef uint32_t u_int32_t;
  37. typedef uint64_t u_int64_t;
  38. #endif
  39. #ifndef HAVE_PATHS_H
  40. #define _PATH_BSHELL "/bin/sh"
  41. #define _PATH_TMP "/tmp/"
  42. #define _PATH_DEVNULL "/dev/null"
  43. #define _PATH_TTY "/dev/tty"
  44. #define _PATH_DEV "/dev/"
  45. #endif
  46. #ifdef HAVE_QUEUE_H
  47. #include <sys/queue.h>
  48. #else
  49. #include "compat/queue.h"
  50. #endif
  51. #ifdef HAVE_TREE_H
  52. #include <sys/tree.h>
  53. #else
  54. #include "compat/tree.h"
  55. #endif
  56. #ifdef HAVE_BITSTRING_H
  57. #include <bitstring.h>
  58. #else
  59. #include "compat/bitstring.h"
  60. #endif
  61. #ifdef HAVE_PATHS_H
  62. #include <paths.h>
  63. #endif
  64. #ifdef HAVE_FORKPTY
  65. #ifdef HAVE_LIBUTIL_H
  66. #include <libutil.h>
  67. #endif
  68. #ifdef HAVE_PTY_H
  69. #include <pty.h>
  70. #endif
  71. #ifdef HAVE_UTIL_H
  72. #include <util.h>
  73. #endif
  74. #endif
  75. #ifdef HAVE_VIS
  76. #include <vis.h>
  77. #else
  78. #include "compat/vis.h"
  79. #endif
  80. #ifdef HAVE_IMSG
  81. #include <imsg.h>
  82. #else
  83. #include "compat/imsg.h"
  84. #endif
  85. #ifdef HAVE_STDINT_H
  86. #include <stdint.h>
  87. #else
  88. #include <inttypes.h>
  89. #endif
  90. #ifdef BROKEN_CMSG_FIRSTHDR
  91. #undef CMSG_FIRSTHDR
  92. #define CMSG_FIRSTHDR(mhdr) \
  93. ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
  94. (struct cmsghdr *)(mhdr)->msg_control : \
  95. (struct cmsghdr *)NULL)
  96. #endif
  97. #ifndef CMSG_ALIGN
  98. #ifdef _CMSG_DATA_ALIGN
  99. #define CMSG_ALIGN _CMSG_DATA_ALIGN
  100. #else
  101. #define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1))
  102. #endif
  103. #endif
  104. #ifndef CMSG_SPACE
  105. #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
  106. #endif
  107. #ifndef CMSG_LEN
  108. #define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
  109. #endif
  110. #ifndef O_DIRECTORY
  111. #define O_DIRECTORY 0
  112. #endif
  113. #ifndef INFTIM
  114. #define INFTIM -1
  115. #endif
  116. #ifndef WAIT_ANY
  117. #define WAIT_ANY -1
  118. #endif
  119. #ifndef SUN_LEN
  120. #define SUN_LEN(sun) (sizeof (sun)->sun_path)
  121. #endif
  122. #ifndef timercmp
  123. #define timercmp(tvp, uvp, cmp) \
  124. (((tvp)->tv_sec == (uvp)->tv_sec) ? \
  125. ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
  126. ((tvp)->tv_sec cmp (uvp)->tv_sec))
  127. #endif
  128. #ifndef timeradd
  129. #define timeradd(tvp, uvp, vvp) \
  130. do { \
  131. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  132. (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
  133. if ((vvp)->tv_usec >= 1000000) { \
  134. (vvp)->tv_sec++; \
  135. (vvp)->tv_usec -= 1000000; \
  136. } \
  137. } while (0)
  138. #endif
  139. #ifndef timersub
  140. #define timersub(tvp, uvp, vvp) \
  141. do { \
  142. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  143. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  144. if ((vvp)->tv_usec < 0) { \
  145. (vvp)->tv_sec--; \
  146. (vvp)->tv_usec += 1000000; \
  147. } \
  148. } while (0)
  149. #endif
  150. #ifndef TTY_NAME_MAX
  151. #define TTY_NAME_MAX 32
  152. #endif
  153. #ifndef HOST_NAME_MAX
  154. #define HOST_NAME_MAX 255
  155. #endif
  156. #ifndef HAVE_FLOCK
  157. #define LOCK_SH 0
  158. #define LOCK_EX 0
  159. #define LOCK_NB 0
  160. #define flock(fd, op) (0)
  161. #endif
  162. #ifndef HAVE_CLOSEFROM
  163. /* closefrom.c */
  164. void closefrom(int);
  165. #endif
  166. #ifndef HAVE_STRCASESTR
  167. /* strcasestr.c */
  168. char *strcasestr(const char *, const char *);
  169. #endif
  170. #ifndef HAVE_STRSEP
  171. /* strsep.c */
  172. char *strsep(char **, const char *);
  173. #endif
  174. #ifndef HAVE_STRTONUM
  175. /* strtonum.c */
  176. long long strtonum(const char *, long long, long long, const char **);
  177. #endif
  178. #ifndef HAVE_STRLCPY
  179. /* strlcpy.c */
  180. size_t strlcpy(char *, const char *, size_t);
  181. #endif
  182. #ifndef HAVE_STRLCAT
  183. /* strlcat.c */
  184. size_t strlcat(char *, const char *, size_t);
  185. #endif
  186. #ifndef HAVE_DAEMON
  187. /* daemon.c */
  188. int daemon(int, int);
  189. #endif
  190. #ifndef HAVE_B64_NTOP
  191. /* b64_ntop.c */
  192. #undef b64_ntop /* for Cygwin */
  193. int b64_ntop(const char *, size_t, char *, size_t);
  194. #endif
  195. #ifndef HAVE_FORKPTY
  196. /* forkpty.c */
  197. #include <sys/ioctl.h>
  198. pid_t forkpty(int *, char *, struct termios *, struct winsize *);
  199. #endif
  200. #ifndef HAVE_ASPRINTF
  201. /* asprintf.c */
  202. int asprintf(char **, const char *, ...);
  203. int vasprintf(char **, const char *, va_list);
  204. #endif
  205. #ifndef HAVE_FGETLN
  206. /* fgetln.c */
  207. char *fgetln(FILE *, size_t *);
  208. #endif
  209. #ifndef HAVE_FPARSELN
  210. char *fparseln(FILE *, size_t *, size_t *, const char *, int);
  211. #endif
  212. #ifndef HAVE_SETENV
  213. /* setenv.c */
  214. int setenv(const char *, const char *, int);
  215. int unsetenv(const char *);
  216. #endif
  217. #ifndef HAVE_CFMAKERAW
  218. /* cfmakeraw.c */
  219. void cfmakeraw(struct termios *);
  220. #endif
  221. #ifndef HAVE_OPENAT
  222. /* openat.c */
  223. #define AT_FDCWD -100
  224. int openat(int, const char *, int, ...);
  225. #endif
  226. #ifndef HAVE_REALLOCARRAY
  227. /* reallocarray.c */
  228. void *reallocarray(void *, size_t, size_t size);
  229. #endif
  230. #ifdef HAVE_GETOPT
  231. #include <getopt.h>
  232. #else
  233. /* getopt.c */
  234. extern int BSDopterr;
  235. extern int BSDoptind;
  236. extern int BSDoptopt;
  237. extern int BSDoptreset;
  238. extern char *BSDoptarg;
  239. int BSDgetopt(int, char *const *, const char *);
  240. #define getopt(ac, av, o) BSDgetopt(ac, av, o)
  241. #define opterr BSDopterr
  242. #define optind BSDoptind
  243. #define optopt BSDoptopt
  244. #define optreset BSDoptreset
  245. #define optarg BSDoptarg
  246. #endif
  247. #endif /* COMPAT_H */