xref: /freebsd/contrib/libpcap/portability.h (revision afdbf109c6a661a729938f68211054a0a50d38ac)
1ada6f083SXin LI /*
2ada6f083SXin LI  * Copyright (c) 1994, 1995, 1996
3ada6f083SXin LI  *	The Regents of the University of California.  All rights reserved.
4ada6f083SXin LI  *
5ada6f083SXin LI  * Redistribution and use in source and binary forms, with or without
6ada6f083SXin LI  * modification, are permitted provided that the following conditions
7ada6f083SXin LI  * are met:
8ada6f083SXin LI  * 1. Redistributions of source code must retain the above copyright
9ada6f083SXin LI  *    notice, this list of conditions and the following disclaimer.
10ada6f083SXin LI  * 2. Redistributions in binary form must reproduce the above copyright
11ada6f083SXin LI  *    notice, this list of conditions and the following disclaimer in the
12ada6f083SXin LI  *    documentation and/or other materials provided with the distribution.
13ada6f083SXin LI  * 3. All advertising materials mentioning features or use of this software
14ada6f083SXin LI  *    must display the following acknowledgement:
15ada6f083SXin LI  *	This product includes software developed by the Computer Systems
16ada6f083SXin LI  *	Engineering Group at Lawrence Berkeley Laboratory.
17ada6f083SXin LI  * 4. Neither the name of the University nor of the Laboratory may be used
18ada6f083SXin LI  *    to endorse or promote products derived from this software without
19ada6f083SXin LI  *    specific prior written permission.
20ada6f083SXin LI  *
21ada6f083SXin LI  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22ada6f083SXin LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ada6f083SXin LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ada6f083SXin LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25ada6f083SXin LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26ada6f083SXin LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27ada6f083SXin LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28ada6f083SXin LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29ada6f083SXin LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30ada6f083SXin LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ada6f083SXin LI  * SUCH DAMAGE.
32ada6f083SXin LI  */
33ada6f083SXin LI 
34ada6f083SXin LI #ifndef portability_h
35ada6f083SXin LI #define	portability_h
36ada6f083SXin LI 
37ada6f083SXin LI /*
38ada6f083SXin LI  * Helpers for portability between Windows and UN*X and between different
39ada6f083SXin LI  * flavors of UN*X.
40ada6f083SXin LI  */
4157e22627SCy Schubert #include <stdarg.h>	/* we declare varargs functions on some platforms */
42ada6f083SXin LI 
43b00ab754SHans Petter Selasky #include "pcap/funcattrs.h"
44b00ab754SHans Petter Selasky 
45ada6f083SXin LI #ifdef __cplusplus
46ada6f083SXin LI extern "C" {
47ada6f083SXin LI #endif
48ada6f083SXin LI 
4957e22627SCy Schubert #ifdef HAVE_STRLCAT
50*afdbf109SJoseph Mingrone   #define pcapint_strlcat	strlcat
51ada6f083SXin LI #else
52ada6f083SXin LI   #if defined(_MSC_VER) || defined(__MINGW32__)
53b00ab754SHans Petter Selasky     /*
54b00ab754SHans Petter Selasky      * strncat_s() is supported at least back to Visual
556f9cba8fSJoseph Mingrone      * Studio 2005; we require Visual Studio 2015 or later.
56b00ab754SHans Petter Selasky      */
57*afdbf109SJoseph Mingrone     #define pcapint_strlcat(x, y, z) \
58ada6f083SXin LI 	strncat_s((x), (z), (y), _TRUNCATE)
59b00ab754SHans Petter Selasky   #else
60b00ab754SHans Petter Selasky     /*
6157e22627SCy Schubert      * Define it ourselves.
62b00ab754SHans Petter Selasky      */
63*afdbf109SJoseph Mingrone     extern size_t pcapint_strlcat(char * restrict dst, const char * restrict src, size_t dstsize);
6457e22627SCy Schubert   #endif
6557e22627SCy Schubert #endif
6657e22627SCy Schubert 
6757e22627SCy Schubert #ifdef HAVE_STRLCPY
68*afdbf109SJoseph Mingrone   #define pcapint_strlcpy	strlcpy
6957e22627SCy Schubert #else
7057e22627SCy Schubert   #if defined(_MSC_VER) || defined(__MINGW32__)
7157e22627SCy Schubert     /*
7257e22627SCy Schubert      * strncpy_s() is supported at least back to Visual
736f9cba8fSJoseph Mingrone      * Studio 2005; we require Visual Studio 2015 or later.
7457e22627SCy Schubert      */
75*afdbf109SJoseph Mingrone     #define pcapint_strlcpy(x, y, z) \
7657e22627SCy Schubert 	strncpy_s((x), (z), (y), _TRUNCATE)
7757e22627SCy Schubert   #else
7857e22627SCy Schubert     /*
7957e22627SCy Schubert      * Define it ourselves.
8057e22627SCy Schubert      */
81*afdbf109SJoseph Mingrone     extern size_t pcapint_strlcpy(char * restrict dst, const char * restrict src, size_t dstsize);
82b00ab754SHans Petter Selasky   #endif
83ada6f083SXin LI #endif
84ada6f083SXin LI 
85ada6f083SXin LI #ifdef _MSC_VER
86ada6f083SXin LI   /*
87b00ab754SHans Petter Selasky    * If <crtdbg.h> has been included, and _DEBUG is defined, and
88b00ab754SHans Petter Selasky    * __STDC__ is zero, <crtdbg.h> will define strdup() to call
89b00ab754SHans Petter Selasky    * _strdup_dbg().  So if it's already defined, don't redefine
90b00ab754SHans Petter Selasky    * it.
91ada6f083SXin LI    */
92b00ab754SHans Petter Selasky   #ifndef strdup
93b00ab754SHans Petter Selasky   #define strdup	_strdup
94ada6f083SXin LI   #endif
95ada6f083SXin LI #endif
96ada6f083SXin LI 
97ada6f083SXin LI /*
986f9cba8fSJoseph Mingrone  * We want asprintf(), for some cases where we use it to construct
996f9cba8fSJoseph Mingrone  * dynamically-allocated variable-length strings; it's present on
1006f9cba8fSJoseph Mingrone  * some, but not all, platforms.
10157e22627SCy Schubert  */
10257e22627SCy Schubert #ifdef HAVE_ASPRINTF
103*afdbf109SJoseph Mingrone #define pcapint_asprintf asprintf
10457e22627SCy Schubert #else
105*afdbf109SJoseph Mingrone extern int pcapint_asprintf(char **, PCAP_FORMAT_STRING(const char *), ...)
10657e22627SCy Schubert     PCAP_PRINTFLIKE(2, 3);
10757e22627SCy Schubert #endif
10857e22627SCy Schubert 
10957e22627SCy Schubert #ifdef HAVE_VASPRINTF
110*afdbf109SJoseph Mingrone #define pcapint_vasprintf vasprintf
11157e22627SCy Schubert #else
112*afdbf109SJoseph Mingrone extern int pcapint_vasprintf(char **, PCAP_FORMAT_STRING(const char *), va_list ap)
113*afdbf109SJoseph Mingrone     PCAP_PRINTFLIKE(2, 0);
11457e22627SCy Schubert #endif
11557e22627SCy Schubert 
1166f9cba8fSJoseph Mingrone /* For Solaris before 11. */
1176f9cba8fSJoseph Mingrone #ifndef timeradd
1186f9cba8fSJoseph Mingrone #define timeradd(a, b, result)                       \
1196f9cba8fSJoseph Mingrone   do {                                               \
1206f9cba8fSJoseph Mingrone     (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;    \
1216f9cba8fSJoseph Mingrone     (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
1226f9cba8fSJoseph Mingrone     if ((result)->tv_usec >= 1000000) {              \
1236f9cba8fSJoseph Mingrone       ++(result)->tv_sec;                            \
1246f9cba8fSJoseph Mingrone       (result)->tv_usec -= 1000000;                  \
1256f9cba8fSJoseph Mingrone     }                                                \
1266f9cba8fSJoseph Mingrone   } while (0)
1276f9cba8fSJoseph Mingrone #endif /* timeradd */
1286f9cba8fSJoseph Mingrone #ifndef timersub
1296f9cba8fSJoseph Mingrone #define timersub(a, b, result)                       \
1306f9cba8fSJoseph Mingrone   do {                                               \
1316f9cba8fSJoseph Mingrone     (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;    \
1326f9cba8fSJoseph Mingrone     (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
1336f9cba8fSJoseph Mingrone     if ((result)->tv_usec < 0) {                     \
1346f9cba8fSJoseph Mingrone       --(result)->tv_sec;                            \
1356f9cba8fSJoseph Mingrone       (result)->tv_usec += 1000000;                  \
1366f9cba8fSJoseph Mingrone     }                                                \
1376f9cba8fSJoseph Mingrone   } while (0)
1386f9cba8fSJoseph Mingrone #endif /* timersub */
1396f9cba8fSJoseph Mingrone 
140ada6f083SXin LI #ifdef HAVE_STRTOK_R
141*afdbf109SJoseph Mingrone   #define pcapint_strtok_r	strtok_r
142ada6f083SXin LI #else
143b00ab754SHans Petter Selasky   #ifdef _WIN32
144ada6f083SXin LI     /*
145ada6f083SXin LI      * Microsoft gives it a different name.
146ada6f083SXin LI      */
147*afdbf109SJoseph Mingrone     #define pcapint_strtok_r	strtok_s
148ada6f083SXin LI   #else
149ada6f083SXin LI     /*
150ada6f083SXin LI      * Define it ourselves.
151ada6f083SXin LI      */
152*afdbf109SJoseph Mingrone     extern char *pcapint_strtok_r(char *, const char *, char **);
153ada6f083SXin LI   #endif
154ada6f083SXin LI #endif /* HAVE_STRTOK_R */
155ada6f083SXin LI 
156ada6f083SXin LI #ifdef _WIN32
157ada6f083SXin LI   #if !defined(__cplusplus)
158ada6f083SXin LI     #define inline __inline
159ada6f083SXin LI   #endif
160ada6f083SXin LI #endif /* _WIN32 */
161ada6f083SXin LI 
162ada6f083SXin LI #ifdef __cplusplus
163ada6f083SXin LI }
164ada6f083SXin LI #endif
165ada6f083SXin LI 
166ada6f083SXin LI #endif
167