portability.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 1994, 1995, 1996
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the Computer Systems
  16. * Engineering Group at Lawrence Berkeley Laboratory.
  17. * 4. Neither the name of the University nor of the Laboratory may be used
  18. * to endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #ifndef portability_h
  34. #define portability_h
  35. /*
  36. * Helpers for portability between Windows and UN*X and between different
  37. * flavors of UN*X.
  38. */
  39. #include "pcap/funcattrs.h"
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. #ifndef HAVE_STRLCPY
  44. /*
  45. * Macro that does the same thing as strlcpy().
  46. */
  47. #if defined(_MSC_VER) || defined(__MINGW32__)
  48. /*
  49. * strncpy_s() is supported at least back to Visual
  50. * Studio 2005.
  51. */
  52. #define strlcpy(x, y, z) \
  53. strncpy_s((x), (z), (y), _TRUNCATE)
  54. #else
  55. #define strlcpy(x, y, z) \
  56. (strncpy((x), (y), (z)), \
  57. ((z) <= 0 ? 0 : ((x)[(z) - 1] = '\0')), \
  58. (void) strlen((y)))
  59. #endif
  60. #endif
  61. #ifndef HAVE_STRLCAT
  62. /*
  63. * Macro that does the same thing as strlcat().
  64. */
  65. #if defined(_MSC_VER) || defined(__MINGW32__)
  66. /*
  67. * strncat_s() is supported at least back to Visual
  68. * Studio 2005.
  69. */
  70. #define strlcat(x, y, z) \
  71. strncat_s((x), (z), (y), _TRUNCATE)
  72. #else
  73. /*
  74. * ANSI C says strncat() always null-terminates its first argument,
  75. * so 1) we don't need to explicitly null-terminate the string
  76. * ourselves and 2) we need to leave room for the null terminator.
  77. */
  78. #define strlcat(x, y, z) \
  79. strncat((x), (y), (z) - strlen((x)) - 1)
  80. #endif
  81. #endif
  82. #ifdef _MSC_VER
  83. /*
  84. * If <crtdbg.h> has been included, and _DEBUG is defined, and
  85. * __STDC__ is zero, <crtdbg.h> will define strdup() to call
  86. * _strdup_dbg(). So if it's already defined, don't redefine
  87. * it.
  88. */
  89. #ifndef strdup
  90. #define strdup _strdup
  91. #endif
  92. #endif
  93. /*
  94. * On Windows, snprintf(), with that name and with C99 behavior - i.e.,
  95. * guaranteeing that the formatted string is null-terminated - didn't
  96. * appear until Visual Studio 2015. Prior to that, the C runtime had
  97. * only _snprintf(), which *doesn't* guarantee that the string is
  98. * null-terminated if it is truncated due to the buffer being too
  99. * small. We therefore can't just define snprintf to be _snprintf
  100. * and define vsnprintf to be _vsnprintf, as we're relying on null-
  101. * termination of strings in all cases.
  102. *
  103. * We also want to allow this to be built with versions of Visual Studio
  104. * prior to VS 2015, so we can't rely on snprintf() being present.
  105. *
  106. * And we want to make sure that, if we support plugins in the future,
  107. * a routine with C99 snprintf() behavior will be available to them.
  108. * We also don't want it to collide with the C library snprintf() if
  109. * there is one.
  110. *
  111. * So we make pcap_snprintf() and pcap_vsnprintf() available, either by
  112. * #defining them to be snprintf or vsnprintf, respectively, or by
  113. * defining our own versions and exporting them.
  114. */
  115. #ifdef HAVE_SNPRINTF
  116. #define pcap_snprintf snprintf
  117. #else
  118. extern int pcap_snprintf(char *, size_t, PCAP_FORMAT_STRING(const char *), ...)
  119. PCAP_PRINTFLIKE(3, 4);
  120. #endif
  121. #ifdef HAVE_VSNPRINTF
  122. #define pcap_vsnprintf vsnprintf
  123. #else
  124. extern int pcap_vsnprintf(char *, size_t, const char *, va_list ap);
  125. #endif
  126. #ifdef HAVE_STRTOK_R
  127. #define pcap_strtok_r strtok_r
  128. #else
  129. #ifdef _WIN32
  130. /*
  131. * Microsoft gives it a different name.
  132. */
  133. #define pcap_strtok_r strtok_s
  134. #else
  135. /*
  136. * Define it ourselves.
  137. */
  138. #define NEED_STRTOK_R
  139. extern char *pcap_strtok_r(char *, const char *, char **);
  140. #endif
  141. #endif /* HAVE_STRTOK_R */
  142. #ifdef _WIN32
  143. #if !defined(__cplusplus)
  144. #define inline __inline
  145. #endif
  146. #endif /* _WIN32 */
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif