X-Git-Url: https://git.tcpdump.org/libpcap/blobdiff_plain/886485550ae6ccdd50e7271c06ac954203648405..09b51d326c38ea8e10ce4da09c09d50e08c5aeb8:/portability.h diff --git a/portability.h b/portability.h index 8a6bf40e..84d0778a 100644 --- a/portability.h +++ b/portability.h @@ -38,114 +38,108 @@ * Helpers for portability between Windows and UN*X and between different * flavors of UN*X. */ +#include /* we declare varargs functions on some platforms */ + +#include "pcap/funcattrs.h" #ifdef __cplusplus extern "C" { #endif -#ifndef HAVE_STRLCPY - /* - * Macro that does the same thing as strlcpy(). - */ - #ifdef _MSC_VER - /* - * strncpy_s() is supported at least back to Visual - * Studio 2005. - */ - #define strlcpy(x, y, z) \ - strncpy_s((x), (z), (y), _TRUNCATE) - - #else - #define strlcpy(x, y, z) \ - (strncpy((x), (y), (z)), \ - ((z) <= 0 ? 0 : ((x)[(z) - 1] = '\0')), \ - (void) strlen((y))) - #endif -#endif - -/* - * For flagging arguments as format strings in MSVC. - */ -#if _MSC_VER >= 1400 - #include - #if _MSC_VER > 1400 - #define FORMAT_STRING(p) _Printf_format_string_ p - #else - #define FORMAT_STRING(p) __format_string p - #endif +#ifdef HAVE_STRLCAT + #define pcap_strlcat strlcat #else - #define FORMAT_STRING(p) p -#endif - -#ifdef _MSC_VER - #define strdup _strdup - #define sscanf sscanf_s - #define setbuf(x, y) \ - setvbuf((x), (y), _IONBF, 0) - #define fopen(x, y) \ - fopen_safe((x), (y)) - FILE *fopen_safe(const char *filename, const char* mode); + #if defined(_MSC_VER) || defined(__MINGW32__) + /* + * strncat_s() is supported at least back to Visual + * Studio 2005; we require Visual Studio 2015 or later. + */ + #define pcap_strlcat(x, y, z) \ + strncat_s((x), (z), (y), _TRUNCATE) + #else + /* + * Define it ourselves. + */ + extern size_t pcap_strlcat(char * restrict dst, const char * restrict src, size_t dstsize); + #endif #endif -#if defined(_MSC_VER) || defined(__MINGW32__) - #define strlcat(x, y, z) \ - strncat_s((x), (z), (y), _TRUNCATE) +#ifdef HAVE_STRLCPY + #define pcap_strlcpy strlcpy +#else + #if defined(_MSC_VER) || defined(__MINGW32__) + /* + * strncpy_s() is supported at least back to Visual + * Studio 2005; we require Visual Studio 2015 or later. + */ + #define pcap_strlcpy(x, y, z) \ + strncpy_s((x), (z), (y), _TRUNCATE) + #else + /* + * Define it ourselves. + */ + extern size_t pcap_strlcpy(char * restrict dst, const char * restrict src, size_t dstsize); + #endif #endif #ifdef _MSC_VER /* - * MSVC. + * If has been included, and _DEBUG is defined, and + * __STDC__ is zero, will define strdup() to call + * _strdup_dbg(). So if it's already defined, don't redefine + * it. */ - #if _MSC_VER >= 1900 - /* - * VS 2015 or newer; we have snprintf() function. - */ - #define HAVE_SNPRINTF + #ifndef strdup + #define strdup _strdup #endif #endif /* - * On Windows, snprintf(), with that name and with C99 behavior - i.e., - * guaranteeing that the formatted string is null-terminated - didn't - * appear until Visual Studio 2015. Prior to that, the C runtime had - * only _snprintf(), which *doesn't* guarantee that the string is - * null-terminated if it is truncated due to the buffer being too - * small. We therefore can't just define snprintf to be _snprintf - * and define vsnprintf to be _vsnprintf, as we're relying on null- - * termination of strings in all cases. - * - * We also want to allow this to be built with versions of Visual Studio - * prior to VS 2015, so we can't rely on snprintf() being present. - * - * And we want to make sure that, if we support plugins in the future, - * a routine with C99 snprintf() behavior will be available to them. - * We also don't want it to collide with the C library snprintf() if - * there is one. - * - * So we make pcap_snprintf() and pcap_vsnprintf() available, either by - * #defining them to be snprintf or vsnprintf, respectively, or by - * defining our own versions and exporting them. + * We want asprintf(), for some cases where we use it to construct + * dynamically-allocated variable-length strings; it's present on + * some, but not all, platforms. */ -#ifdef HAVE_SNPRINTF -#define pcap_snprintf snprintf +#ifdef HAVE_ASPRINTF +#define pcap_asprintf asprintf #else -extern int pcap_snprintf(char *, size_t, FORMAT_STRING(const char *), ...) -#ifdef __ATTRIBUTE___FORMAT_OK - __attribute__((format (printf, 3, 4))) -#endif /* __ATTRIBUTE___FORMAT_OK */ - ; +extern int pcap_asprintf(char **, PCAP_FORMAT_STRING(const char *), ...) + PCAP_PRINTFLIKE(2, 3); #endif -#ifdef HAVE_VSNPRINTF -#define pcap_vsnprintf vsnprintf +#ifdef HAVE_VASPRINTF +#define pcap_vasprintf vasprintf #else -extern int pcap_vsnprintf(char *, size_t, const char *, va_list ap); +extern int pcap_vasprintf(char **, const char *, va_list ap); #endif +/* For Solaris before 11. */ +#ifndef timeradd +#define timeradd(a, b, result) \ + do { \ + (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \ + (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \ + if ((result)->tv_usec >= 1000000) { \ + ++(result)->tv_sec; \ + (result)->tv_usec -= 1000000; \ + } \ + } while (0) +#endif /* timeradd */ +#ifndef timersub +#define timersub(a, b, result) \ + do { \ + (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ + (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ + if ((result)->tv_usec < 0) { \ + --(result)->tv_sec; \ + (result)->tv_usec += 1000000; \ + } \ + } while (0) +#endif /* timersub */ + #ifdef HAVE_STRTOK_R #define pcap_strtok_r strtok_r #else - #ifdef _MSC_VER + #ifdef _WIN32 /* * Microsoft gives it a different name. */ @@ -154,56 +148,11 @@ extern int pcap_vsnprintf(char *, size_t, const char *, va_list ap); /* * Define it ourselves. */ - #define NEED_STRTOK_R - extern int pcap_strtok_r(char *, const char *, char **); + extern char *pcap_strtok_r(char *, const char *, char **); #endif #endif /* HAVE_STRTOK_R */ #ifdef _WIN32 - /* - * These may be defined by . - * - * XXX - for MSVC, we always want the _MSC_EXTENSIONS versions. - * What about other compilers? If, as the MinGW Web site says MinGW - * does, the other compilers just use Microsoft's run-time library, - * then they should probably use the _MSC_EXTENSIONS even if the - * compiler doesn't define _MSC_EXTENSIONS. - * - * XXX - we currently aren't using any of these, but this allows - * their use in the future. - */ - #ifndef PRId64 - #ifdef _MSC_EXTENSIONS - #define PRId64 "I64d" - #else - #define PRId64 "lld" - #endif - #endif /* PRId64 */ - - #ifndef PRIo64 - #ifdef _MSC_EXTENSIONS - #define PRIo64 "I64o" - #else - #define PRIo64 "llo" - #endif - #endif /* PRIo64 */ - - #ifndef PRIx64 - #ifdef _MSC_EXTENSIONS - #define PRIx64 "I64x" - #else - #define PRIx64 "llx" - #endif - #endif - - #ifndef PRIu64 - #ifdef _MSC_EXTENSIONS - #define PRIu64 "I64u" - #else - #define PRIu64 "llu" - #endif - #endif - #if !defined(__cplusplus) #define inline __inline #endif