compat.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * libcompat - system compatibility library
  3. * compat.h - system compatibility declarations
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #ifndef COMPAT_H
  22. #define COMPAT_H
  23. #ifndef TEST_LIBCOMPAT
  24. #define TEST_LIBCOMPAT 0
  25. #endif
  26. #if TEST_LIBCOMPAT || !defined(HAVE_STRNLEN) || !defined(HAVE_STRNDUP) || \
  27. !defined(HAVE_C99_SNPRINTF)
  28. #include <stddef.h>
  29. #endif
  30. #if TEST_LIBCOMPAT || !defined(HAVE_ASPRINTF) || !defined(HAVE_C99_SNPRINTF)
  31. #include <stdarg.h>
  32. #endif
  33. #if TEST_LIBCOMPAT || !defined(HAVE_VA_COPY)
  34. #include <string.h>
  35. #endif
  36. /* Language definitions. */
  37. #ifdef __GNUC__
  38. #define LIBCOMPAT_GCC_VERSION (__GNUC__ << 8 | __GNUC_MINOR__)
  39. #else
  40. #define LIBCOMPAT_GCC_VERSION 0
  41. #endif
  42. #if LIBCOMPAT_GCC_VERSION >= 0x0300
  43. #define LIBCOMPAT_ATTR_PRINTF(n) __attribute__((format(printf, n, n + 1)))
  44. #define LIBCOMPAT_ATTR_VPRINTF(n) __attribute__((format(printf, n, 0)))
  45. #else
  46. #define LIBCOMPAT_ATTR_PRINTF(n)
  47. #define LIBCOMPAT_ATTR_VPRINTF(n)
  48. #endif
  49. /* For C++, define a __func__ fallback in case it's not natively supported. */
  50. #if defined(__cplusplus) && __cplusplus < 201103L
  51. # if LIBCOMPAT_GCC_VERSION >= 0x0200
  52. # define __func__ __PRETTY_FUNCTION__
  53. # else
  54. # define __func__ __FUNCTION__
  55. # endif
  56. #endif
  57. #if defined(__cplusplus) && __cplusplus < 201103L
  58. #define nullptr 0
  59. #endif
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. #ifndef HAVE_OFFSETOF
  64. #define offsetof(st, m) ((size_t)&((st *)NULL)->m)
  65. #endif
  66. #ifndef HAVE_MAKEDEV
  67. #define makedev(maj, min) ((((maj) & 0xff) << 8) | ((min) & 0xff))
  68. #endif
  69. #ifndef HAVE_O_NOFOLLOW
  70. #define O_NOFOLLOW 0
  71. #endif
  72. #ifndef HAVE_P_TMPDIR
  73. #define P_tmpdir "/tmp"
  74. #endif
  75. /*
  76. * Define WCOREDUMP if we don't have it already, coredumps won't be
  77. * detected, though.
  78. */
  79. #ifndef HAVE_WCOREDUMP
  80. #define WCOREDUMP(x) 0
  81. #endif
  82. #ifndef HAVE_VA_COPY
  83. #define va_copy(dest, src) memcpy(&(dest), &(src), sizeof(va_list))
  84. #endif
  85. #if TEST_LIBCOMPAT
  86. #undef snprintf
  87. #define snprintf test_snprintf
  88. #undef vsnprintf
  89. #define vsnprintf test_vsnprintf
  90. #undef asprintf
  91. #define asprintf test_asprintf
  92. #undef vasprintf
  93. #define vasprintf test_vasprintf
  94. #undef strndup
  95. #define strndup test_strndup
  96. #undef strnlen
  97. #define strnlen test_strnlen
  98. #undef strerror
  99. #define strerror test_strerror
  100. #undef strsignal
  101. #define strsignal test_strsignal
  102. #undef scandir
  103. #define scandir test_scandir
  104. #undef alphasort
  105. #define alphasort test_alphasort
  106. #undef unsetenv
  107. #define unsetenv test_unsetenv
  108. #endif
  109. #if TEST_LIBCOMPAT || !defined(HAVE_C99_SNPRINTF)
  110. int snprintf(char *str, size_t n, char const *fmt, ...)
  111. LIBCOMPAT_ATTR_PRINTF(3);
  112. int vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list args)
  113. LIBCOMPAT_ATTR_VPRINTF(3);
  114. #endif
  115. #if TEST_LIBCOMPAT || !defined(HAVE_ASPRINTF)
  116. int asprintf(char **str, char const *fmt, ...)
  117. LIBCOMPAT_ATTR_PRINTF(2);
  118. int vasprintf(char **str, const char *fmt, va_list args)
  119. LIBCOMPAT_ATTR_VPRINTF(2);
  120. #endif
  121. #if TEST_LIBCOMPAT || !defined(HAVE_STRNLEN)
  122. size_t strnlen(const char *s, size_t n);
  123. #endif
  124. #if TEST_LIBCOMPAT || !defined(HAVE_STRNDUP)
  125. char *strndup(const char *s, size_t n);
  126. #endif
  127. #if TEST_LIBCOMPAT || !defined(HAVE_STRERROR)
  128. const char *strerror(int);
  129. #endif
  130. #if TEST_LIBCOMPAT || !defined(HAVE_STRSIGNAL)
  131. const char *strsignal(int);
  132. #endif
  133. #if TEST_LIBCOMPAT || !defined(HAVE_SCANDIR)
  134. struct dirent;
  135. int scandir(const char *dir, struct dirent ***namelist,
  136. int (*filter)(const struct dirent *),
  137. int (*cmp)(const void *, const void *));
  138. #endif
  139. #if TEST_LIBCOMPAT || !defined(HAVE_ALPHASORT)
  140. int alphasort(const void *a, const void *b);
  141. #endif
  142. #if TEST_LIBCOMPAT || !defined(HAVE_UNSETENV)
  143. int unsetenv(const char *x);
  144. #endif
  145. #if TEST_LIBCOMPAT || !defined(HAVE_SETEXECFILECON)
  146. int setexecfilecon(const char *filename, const char *fallback_type);
  147. #endif
  148. #ifdef __cplusplus
  149. }
  150. #endif
  151. #endif /* COMPAT_H */