snprintf.m4 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # $Id: snprintf.m4,v 1.1.1.1 2008/01/06 03:24:00 holger Exp $
  2. # Copyright (c) 2008 Holger Weiss <holger@jhweiss.de>.
  3. #
  4. # This code may freely be used, modified and/or redistributed for any purpose.
  5. # It would be nice if additions and fixes to this file (including trivial code
  6. # cleanups) would be sent back in order to let me include them in the version
  7. # available at <http://www.jhweiss.de/software/snprintf.html>. However, this is
  8. # not a requirement for using or redistributing (possibly modified) versions of
  9. # this file, nor is leaving this notice intact mandatory.
  10. # HW_HEADER_STDARG_H
  11. # ------------------
  12. # Define HAVE_STDARG_H to 1 if <stdarg.h> is available.
  13. AC_DEFUN([HW_HEADER_STDARG_H],
  14. [
  15. AC_PREREQ([2.60])dnl Older releases should work if AC_CHECK_HEADERS is used.
  16. AC_CHECK_HEADERS_ONCE([stdarg.h])
  17. ])# HW_HEADER_STDARG_H
  18. # HW_HEADER_VARARGS_H
  19. # -------------------
  20. # Define HAVE_VARARGS_H to 1 if <varargs.h> is available.
  21. AC_DEFUN([HW_HEADER_VARARGS_H],
  22. [
  23. AC_PREREQ([2.60])dnl Older releases should work if AC_CHECK_HEADERS is used.
  24. AC_CHECK_HEADERS_ONCE([varargs.h])
  25. ])# HW_HEADER_VARARGS_H
  26. # HW_FUNC_VA_COPY
  27. # ---------------
  28. # Set $hw_cv_func_va_copy to "yes" or "no". Define HAVE_VA_COPY to 1 if
  29. # $hw_cv_func_va_copy is set to "yes". Note that it's "unspecified whether
  30. # va_copy and va_end are macros or identifiers declared with external linkage."
  31. # (C99: 7.15.1, 1) Therefore, the presence of va_copy(3) cannot simply "be
  32. # tested with #ifdef", as suggested by the Autoconf manual (5.5.1).
  33. AC_DEFUN([HW_FUNC_VA_COPY],
  34. [
  35. AC_REQUIRE([HW_HEADER_STDARG_H])dnl Our check evaluates HAVE_STDARG_H.
  36. AC_REQUIRE([HW_HEADER_VARARGS_H])dnl Our check evaluates HAVE_VARARGS_H.
  37. AC_CACHE_CHECK([for va_copy],
  38. [hw_cv_func_va_copy],
  39. [AC_RUN_IFELSE(
  40. [AC_LANG_PROGRAM(
  41. [[#if HAVE_STDARG_H
  42. #include <stdarg.h>
  43. #elif HAVE_VARARGS_H
  44. #include <varargs.h>
  45. #endif]],
  46. [[va_list ap, aq; va_copy(aq, ap);]])],
  47. [hw_cv_func_va_copy=yes],
  48. [hw_cv_func_va_copy=no],
  49. [hw_cv_func_va_copy=no])])
  50. AS_IF([test "$hw_cv_func_va_copy" = yes],
  51. [AC_DEFINE([HAVE_VA_COPY], [1],
  52. [Define to 1 if you have the `va_copy' function or macro.])])
  53. ])# HW_FUNC_VA_COPY
  54. # HW_FUNC___VA_COPY
  55. # -----------------
  56. # Set $hw_cv_func___va_copy to "yes" or "no". Define HAVE___VA_COPY to 1 if
  57. # $hw_cv_func___va_copy is set to "yes".
  58. AC_DEFUN([HW_FUNC___VA_COPY],
  59. [
  60. AC_REQUIRE([HW_HEADER_STDARG_H])dnl Our check evaluates HAVE_STDARG_H.
  61. AC_REQUIRE([HW_HEADER_VARARGS_H])dnl Our check evaluates HAVE_VARARGS_H.
  62. AC_CACHE_CHECK([for __va_copy],
  63. [hw_cv_func___va_copy],
  64. [AC_RUN_IFELSE(
  65. [AC_LANG_PROGRAM(
  66. [[#if HAVE_STDARG_H
  67. #include <stdarg.h>
  68. #elif HAVE_VARARGS_H
  69. #include <varargs.h>
  70. #endif]],
  71. [[va_list ap, aq; __va_copy(aq, ap);]])],
  72. [hw_cv_func___va_copy=yes],
  73. [hw_cv_func___va_copy=no],
  74. [hw_cv_func___va_copy=no])])
  75. AS_IF([test "$hw_cv_func___va_copy" = yes],
  76. [AC_DEFINE([HAVE___VA_COPY], [1],
  77. [Define to 1 if you have the `__va_copy' function or macro.])])
  78. ])# HW_FUNC___VA_COPY
  79. # HW_FUNC_VSNPRINTF
  80. # -----------------
  81. # Set $hw_cv_func_vsnprintf and $hw_cv_func_vsnprintf_c99 to "yes" or "no",
  82. # respectively. Define HAVE_VSNPRINTF to 1 only if $hw_cv_func_vsnprintf_c99
  83. # is set to "yes". Otherwise, define vsnprintf to rpl_vsnprintf and make sure
  84. # the replacement function will be built.
  85. #
  86. # If enable_snprintf_replacement=true, the replacement is forced to be built.
  87. AC_DEFUN([HW_FUNC_VSNPRINTF],
  88. [
  89. AC_PREREQ([2.60])dnl 2.59 should work if some AC_TYPE_* macros are replaced.
  90. AC_REQUIRE([HW_HEADER_STDARG_H])dnl Our check evaluates HAVE_STDARG_H.
  91. if test "xtrue" != x"$enable_snprintf_replacement"; then
  92. AC_CHECK_FUNC([vsnprintf],
  93. [hw_cv_func_vsnprintf=yes],
  94. [hw_cv_func_vsnprintf=no])
  95. AS_IF([test "$hw_cv_func_vsnprintf" = yes],
  96. [AC_CACHE_CHECK([whether vsnprintf is C99 compliant],
  97. [hw_cv_func_vsnprintf_c99],
  98. [AC_RUN_IFELSE(
  99. [AC_LANG_PROGRAM(
  100. [[#if HAVE_STDARG_H
  101. #include <stdarg.h>
  102. #endif
  103. #include <stdio.h>
  104. static int testprintf(char *buf, size_t size, const char *format, ...)
  105. {
  106. int result;
  107. va_list ap;
  108. va_start(ap, format);
  109. result = vsnprintf(buf, size, format, ap);
  110. va_end(ap);
  111. return result;
  112. }]],
  113. [[char buf[43];
  114. if (testprintf(buf, 4, "The answer is %27.2g.", 42.0) != 42 ||
  115. testprintf(buf, 0, "No, it's %32zu.", (size_t)42) != 42 ||
  116. buf[0] != 'T' || buf[3] != '\0')
  117. return 1;]])],
  118. [hw_cv_func_vsnprintf_c99=yes],
  119. [hw_cv_func_vsnprintf_c99=no],
  120. [hw_cv_func_vsnprintf_c99=no])])],
  121. [hw_cv_func_snprintf_c99=no])
  122. AS_IF([test "$hw_cv_func_vsnprintf_c99" = yes],
  123. [AC_DEFINE([HAVE_VSNPRINTF], [1],
  124. [Define to 1 if you have a C99 compliant 'vsnprintf' function.])])
  125. else
  126. hw_cv_func_vsnprintf_c99=no
  127. fi
  128. AS_IF([test "$hw_cv_func_vsnprintf_c99" = no],
  129. [AC_DEFINE([vsnprintf], [rpl_vsnprintf],
  130. [Define to rpl_vsnprintf if the replacement function should be used.])
  131. AC_CHECK_HEADERS([inttypes.h locale.h stddef.h stdint.h])
  132. AC_CHECK_MEMBERS([struct lconv.decimal_point, struct lconv.thousands_sep],
  133. [], [], [#include <locale.h>])
  134. AC_TYPE_LONG_DOUBLE
  135. AC_TYPE_LONG_LONG_INT
  136. AC_TYPE_UNSIGNED_LONG_LONG_INT
  137. AC_TYPE_SIZE_T
  138. AC_TYPE_INTMAX_T
  139. AC_TYPE_UINTMAX_T
  140. AC_TYPE_UINTPTR_T
  141. AC_CHECK_TYPES([ptrdiff_t])
  142. AC_CHECK_FUNCS([localeconv])
  143. _HW_FUNC_XPRINTF_REPLACE])
  144. ])# HW_FUNC_VSNPRINTF
  145. # HW_FUNC_SNPRINTF
  146. # ----------------
  147. # Set $hw_cv_func_snprintf and $hw_cv_func_snprintf_c99 to "yes" or "no",
  148. # respectively. Define HAVE_SNPRINTF to 1 only if $hw_cv_func_snprintf_c99
  149. # is set to "yes". Otherwise, define snprintf to rpl_snprintf and make sure
  150. # the replacement function will be built.
  151. #
  152. # If enable_snprintf_replacement=true, the replacement is forced to be built.
  153. AC_DEFUN([HW_FUNC_SNPRINTF],
  154. [
  155. AC_REQUIRE([HW_FUNC_VSNPRINTF])dnl Our snprintf(3) calls vsnprintf(3).
  156. if test "xtrue" != x"$enable_snprintf_replacement"; then
  157. AC_CHECK_FUNC([snprintf],
  158. [hw_cv_func_snprintf=yes],
  159. [hw_cv_func_snprintf=no])
  160. AS_IF([test "$hw_cv_func_snprintf" = yes],
  161. [AC_CACHE_CHECK([whether snprintf is C99 compliant],
  162. [hw_cv_func_snprintf_c99],
  163. [AC_RUN_IFELSE(
  164. [AC_LANG_PROGRAM([[#include <stdio.h>]],
  165. [[char buf[43];
  166. if (snprintf(buf, 4, "The answer is %27.2g.", 42.0) != 42 ||
  167. snprintf(buf, 0, "No, it's %32zu.", (size_t)42) != 42 ||
  168. buf[0] != 'T' || buf[3] != '\0')
  169. return 1;]])],
  170. [hw_cv_func_snprintf_c99=yes],
  171. [hw_cv_func_snprintf_c99=no],
  172. [hw_cv_func_snprintf_c99=no])])],
  173. [hw_cv_func_snprintf_c99=no])
  174. else
  175. hw_cv_func_snprintf_c99=no
  176. fi
  177. AS_IF([test "$hw_cv_func_snprintf_c99" = yes],
  178. [AC_DEFINE([HAVE_SNPRINTF], [1],
  179. [Define to 1 if you have a C99 compliant 'snprintf' function.])],
  180. [AC_DEFINE([snprintf], [rpl_snprintf],
  181. [Define to rpl_snprintf if the replacement function should be used.])
  182. _HW_FUNC_XPRINTF_REPLACE])
  183. ])# HW_FUNC_SNPRINTF
  184. # HW_FUNC_VASPRINTF
  185. # -----------------
  186. # Set $hw_cv_func_vasprintf to "yes" or "no". Define HAVE_VASPRINTF to 1 if
  187. # $hw_cv_func_vasprintf is set to "yes". Otherwise, define vasprintf to
  188. # rpl_vasprintf and make sure the replacement function will be built.
  189. #
  190. # If enable_snprintf_replacement=true, the replacement is forced to be built.
  191. AC_DEFUN([HW_FUNC_VASPRINTF],
  192. [
  193. AC_REQUIRE([HW_FUNC_VSNPRINTF])dnl Our vasprintf(3) calls vsnprintf(3).
  194. if test "xtrue" != x"$enable_snprintf_replacement"; then
  195. AC_CHECK_FUNCS([vasprintf],
  196. [hw_cv_func_vasprintf=yes],
  197. [hw_cv_func_vasprintf=no])
  198. else
  199. hw_cv_func_vasprintf=no
  200. fi
  201. AS_IF([test "$hw_cv_func_vasprintf" = no],
  202. [AC_DEFINE([vasprintf], [rpl_vasprintf],
  203. [Define to rpl_vasprintf if the replacement function should be used.])
  204. AC_CHECK_HEADERS([stdlib.h])
  205. HW_FUNC_VA_COPY
  206. AS_IF([test "$hw_cv_func_va_copy" = no],
  207. [HW_FUNC___VA_COPY])
  208. _HW_FUNC_XPRINTF_REPLACE])
  209. ])# HW_FUNC_VASPRINTF
  210. # HW_FUNC_ASPRINTF
  211. # ----------------
  212. # Set $hw_cv_func_asprintf to "yes" or "no". Define HAVE_ASPRINTF to 1 if
  213. # $hw_cv_func_asprintf is set to "yes". Otherwise, define asprintf to
  214. # rpl_asprintf and make sure the replacement function will be built.
  215. #
  216. # If enable_snprintf_replacement=true, the replacement is forced to be built.
  217. AC_DEFUN([HW_FUNC_ASPRINTF],
  218. [
  219. AC_REQUIRE([HW_FUNC_VASPRINTF])dnl Our asprintf(3) calls vasprintf(3).
  220. if test "xtrue" != x"$enable_snprintf_replacement"; then
  221. AC_CHECK_FUNCS([asprintf],
  222. [hw_cv_func_asprintf=yes],
  223. [hw_cv_func_asprintf=no])
  224. else
  225. hw_cv_func_asprintf=no
  226. fi
  227. AS_IF([test "$hw_cv_func_asprintf" = no],
  228. [AC_DEFINE([asprintf], [rpl_asprintf],
  229. [Define to rpl_asprintf if the replacement function should be used.])
  230. _HW_FUNC_XPRINTF_REPLACE])
  231. ])# HW_FUNC_ASPRINTF
  232. # _HW_FUNC_XPRINTF_REPLACE
  233. # ------------------------
  234. # Arrange for building snprintf.c. Must be called if one or more of the
  235. # functions provided by snprintf.c are needed.
  236. AC_DEFUN([_HW_FUNC_XPRINTF_REPLACE],
  237. [
  238. AS_IF([test "x$_hw_cv_func_xprintf_replace_done" != xyes],
  239. [AC_C_CONST
  240. HW_HEADER_STDARG_H
  241. AC_LIBOBJ([snprintf])
  242. _hw_cv_func_xprintf_replace_done=yes])
  243. ])# _HW_FUNC_XPRINTF_REPLACE
  244. dnl vim: set joinspaces textwidth=80: