2 * libcompat - system compatibility library
3 * compat.h - system compatibility declarations
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
25 #ifndef TEST_LIBCOMPAT
26 #define TEST_LIBCOMPAT 0
29 #if TEST_LIBCOMPAT || !defined(HAVE_STRNLEN) || !defined(HAVE_STRNDUP) || \
30 !defined(HAVE_C99_SNPRINTF)
34 #if TEST_LIBCOMPAT || !defined(HAVE_ASPRINTF) || !defined(HAVE_C99_SNPRINTF)
38 #if TEST_LIBCOMPAT || !defined(HAVE_VA_COPY)
42 /* Language definitions. */
45 #define LIBCOMPAT_GCC_VERSION (__GNUC__ << 8 | __GNUC_MINOR__)
47 #define LIBCOMPAT_GCC_VERSION 0
50 #if LIBCOMPAT_GCC_VERSION >= 0x0300
51 #define LIBCOMPAT_ATTR_PRINTF(n) __attribute__((format(printf, n, n + 1)))
52 #define LIBCOMPAT_ATTR_VPRINTF(n) __attribute__((format(printf, n, 0)))
54 #define LIBCOMPAT_ATTR_PRINTF(n)
55 #define LIBCOMPAT_ATTR_VPRINTF(n)
58 /* For C++, define a __func__ fallback in case it's not natively supported. */
59 #if defined(__cplusplus) && __cplusplus < 201103L
60 # if LIBCOMPAT_GCC_VERSION >= 0x0200
61 # define __func__ __PRETTY_FUNCTION__
63 # define __func__ __FUNCTION__
67 #if defined(__cplusplus) && __cplusplus < 201103L
76 #define offsetof(st, m) ((size_t)&((st *)NULL)->m)
80 #define makedev(maj, min) ((((maj) & 0xff) << 8) | ((min) & 0xff))
83 #ifndef HAVE_O_NOFOLLOW
88 #define P_tmpdir "/tmp"
92 * Define WCOREDUMP if we don't have it already, coredumps won't be
95 #ifndef HAVE_WCOREDUMP
96 #define WCOREDUMP(x) 0
100 #define va_copy(dest, src) memcpy(&(dest), &(src), sizeof(va_list))
105 #define snprintf test_snprintf
107 #define vsnprintf test_vsnprintf
109 #define asprintf test_asprintf
111 #define vasprintf test_vasprintf
113 #define strndup test_strndup
115 #define strnlen test_strnlen
117 #define strerror test_strerror
119 #define strsignal test_strsignal
121 #define scandir test_scandir
123 #define alphasort test_alphasort
125 #define unsetenv test_unsetenv
128 #if TEST_LIBCOMPAT || !defined(HAVE_C99_SNPRINTF)
129 int snprintf(char *str, size_t n, char const *fmt, ...)
130 LIBCOMPAT_ATTR_PRINTF(3);
131 int vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list args)
132 LIBCOMPAT_ATTR_VPRINTF(3);
135 #if TEST_LIBCOMPAT || !defined(HAVE_ASPRINTF)
136 int asprintf(char **str, char const *fmt, ...)
137 LIBCOMPAT_ATTR_PRINTF(2);
138 int vasprintf(char **str, const char *fmt, va_list args)
139 LIBCOMPAT_ATTR_VPRINTF(2);
142 #if TEST_LIBCOMPAT || !defined(HAVE_STRNLEN)
143 size_t strnlen(const char *s, size_t n);
146 #if TEST_LIBCOMPAT || !defined(HAVE_STRNDUP)
147 char *strndup(const char *s, size_t n);
150 #if TEST_LIBCOMPAT || !defined(HAVE_STRERROR)
151 const char *strerror(int);
154 #if TEST_LIBCOMPAT || !defined(HAVE_STRSIGNAL)
155 const char *strsignal(int);
158 #if TEST_LIBCOMPAT || !defined(HAVE_SCANDIR)
160 int scandir(const char *dir, struct dirent ***namelist,
161 int (*filter)(const struct dirent *),
162 int (*cmp)(const void *, const void *));
165 #if TEST_LIBCOMPAT || !defined(HAVE_ALPHASORT)
166 int alphasort(const void *a, const void *b);
169 #if TEST_LIBCOMPAT || !defined(HAVE_UNSETENV)
170 int unsetenv(const char *x);
173 #if TEST_LIBCOMPAT || !defined(HAVE_SETEXECFILECON)
174 int setexecfilecon(const char *filename, const char *fallback_type);
181 #endif /* COMPAT_H */