dpkg (1.18.25) stretch; urgency=medium
[dpkg] / lib / compat / compat.h
CommitLineData
1479465f
GJ
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
22#ifndef COMPAT_H
23#define COMPAT_H
24
25#ifndef TEST_LIBCOMPAT
26#define TEST_LIBCOMPAT 0
27#endif
28
29#if TEST_LIBCOMPAT || !defined(HAVE_STRNLEN) || !defined(HAVE_STRNDUP) || \
30 !defined(HAVE_C99_SNPRINTF)
31#include <stddef.h>
32#endif
33
34#if TEST_LIBCOMPAT || !defined(HAVE_ASPRINTF) || !defined(HAVE_C99_SNPRINTF)
35#include <stdarg.h>
36#endif
37
38#if TEST_LIBCOMPAT || !defined(HAVE_VA_COPY)
39#include <string.h>
40#endif
41
42/* Language definitions. */
43
44#ifdef __GNUC__
45#define LIBCOMPAT_GCC_VERSION (__GNUC__ << 8 | __GNUC_MINOR__)
46#else
47#define LIBCOMPAT_GCC_VERSION 0
48#endif
49
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)))
53#else
54#define LIBCOMPAT_ATTR_PRINTF(n)
55#define LIBCOMPAT_ATTR_VPRINTF(n)
56#endif
57
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__
62# else
63# define __func__ __FUNCTION__
64# endif
65#endif
66
67#if defined(__cplusplus) && __cplusplus < 201103L
68#define nullptr 0
69#endif
70
71#ifdef __cplusplus
72extern "C" {
73#endif
74
75#ifndef HAVE_OFFSETOF
76#define offsetof(st, m) ((size_t)&((st *)NULL)->m)
77#endif
78
79#ifndef HAVE_MAKEDEV
80#define makedev(maj, min) ((((maj) & 0xff) << 8) | ((min) & 0xff))
81#endif
82
83#ifndef HAVE_O_NOFOLLOW
84#define O_NOFOLLOW 0
85#endif
86
87#ifndef HAVE_P_TMPDIR
88#define P_tmpdir "/tmp"
89#endif
90
91/*
92 * Define WCOREDUMP if we don't have it already, coredumps won't be
93 * detected, though.
94 */
95#ifndef HAVE_WCOREDUMP
96#define WCOREDUMP(x) 0
97#endif
98
99#ifndef HAVE_VA_COPY
100#define va_copy(dest, src) memcpy(&(dest), &(src), sizeof(va_list))
101#endif
102
103#if TEST_LIBCOMPAT
104#undef snprintf
105#define snprintf test_snprintf
106#undef vsnprintf
107#define vsnprintf test_vsnprintf
108#undef asprintf
109#define asprintf test_asprintf
110#undef vasprintf
111#define vasprintf test_vasprintf
112#undef strndup
113#define strndup test_strndup
114#undef strnlen
115#define strnlen test_strnlen
116#undef strerror
117#define strerror test_strerror
118#undef strsignal
119#define strsignal test_strsignal
120#undef scandir
121#define scandir test_scandir
122#undef alphasort
123#define alphasort test_alphasort
124#undef unsetenv
125#define unsetenv test_unsetenv
126#endif
127
128#if TEST_LIBCOMPAT || !defined(HAVE_C99_SNPRINTF)
129int snprintf(char *str, size_t n, char const *fmt, ...)
130 LIBCOMPAT_ATTR_PRINTF(3);
131int vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list args)
132 LIBCOMPAT_ATTR_VPRINTF(3);
133#endif
134
135#if TEST_LIBCOMPAT || !defined(HAVE_ASPRINTF)
136int asprintf(char **str, char const *fmt, ...)
137 LIBCOMPAT_ATTR_PRINTF(2);
138int vasprintf(char **str, const char *fmt, va_list args)
139 LIBCOMPAT_ATTR_VPRINTF(2);
140#endif
141
142#if TEST_LIBCOMPAT || !defined(HAVE_STRNLEN)
143size_t strnlen(const char *s, size_t n);
144#endif
145
146#if TEST_LIBCOMPAT || !defined(HAVE_STRNDUP)
147char *strndup(const char *s, size_t n);
148#endif
149
150#if TEST_LIBCOMPAT || !defined(HAVE_STRERROR)
151const char *strerror(int);
152#endif
153
154#if TEST_LIBCOMPAT || !defined(HAVE_STRSIGNAL)
155const char *strsignal(int);
156#endif
157
158#if TEST_LIBCOMPAT || !defined(HAVE_SCANDIR)
159struct dirent;
160int scandir(const char *dir, struct dirent ***namelist,
161 int (*filter)(const struct dirent *),
162 int (*cmp)(const void *, const void *));
163#endif
164
165#if TEST_LIBCOMPAT || !defined(HAVE_ALPHASORT)
166int alphasort(const void *a, const void *b);
167#endif
168
169#if TEST_LIBCOMPAT || !defined(HAVE_UNSETENV)
170int unsetenv(const char *x);
171#endif
172
173#if TEST_LIBCOMPAT || !defined(HAVE_SETEXECFILECON)
174int setexecfilecon(const char *filename, const char *fallback_type);
175#endif
176
177#ifdef __cplusplus
178}
179#endif
180
181#endif /* COMPAT_H */