Collection of DPF-based plugins for packaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.8KB

  1. # ===========================================================================
  2. # https://www.gnu.org/software/autoconf-archive/ax_func_snprintf.html
  3. # ===========================================================================
  4. #
  5. # SYNOPSIS
  6. #
  7. # AX_FUNC_SNPRINTF
  8. #
  9. # DESCRIPTION
  10. #
  11. # Checks for a fully C99 compliant snprintf, in particular checks whether
  12. # it does bounds checking and returns the correct string length; does the
  13. # same check for vsnprintf. If no working snprintf or vsnprintf is found,
  14. # request a replacement and warn the user about it. Note: the mentioned
  15. # replacement is freely available and may be used in any project
  16. # regardless of it's license.
  17. #
  18. # LICENSE
  19. #
  20. # Copyright (c) 2008 Ruediger Kuhlmann <info@ruediger-kuhlmann.de>
  21. #
  22. # Copying and distribution of this file, with or without modification, are
  23. # permitted in any medium without royalty provided the copyright notice
  24. # and this notice are preserved. This file is offered as-is, without any
  25. # warranty.
  26. #serial 6
  27. AU_ALIAS([AC_FUNC_SNPRINTF], [AX_FUNC_SNPRINTF])
  28. AC_DEFUN([AX_FUNC_SNPRINTF],
  29. [AC_CHECK_FUNCS(snprintf vsnprintf)
  30. AC_MSG_CHECKING(for working snprintf)
  31. AC_CACHE_VAL(ac_cv_have_working_snprintf,
  32. [AC_TRY_RUN(
  33. [#include <stdio.h>
  34. int main(void)
  35. {
  36. char bufs[5] = { 'x', 'x', 'x', '\0', '\0' };
  37. char bufd[5] = { 'x', 'x', 'x', '\0', '\0' };
  38. int i;
  39. i = snprintf (bufs, 2, "%s", "111");
  40. if (strcmp (bufs, "1")) exit (1);
  41. if (i != 3) exit (1);
  42. i = snprintf (bufd, 2, "%d", 111);
  43. if (strcmp (bufd, "1")) exit (1);
  44. if (i != 3) exit (1);
  45. exit(0);
  46. }], ac_cv_have_working_snprintf=yes, ac_cv_have_working_snprintf=no, ac_cv_have_working_snprintf=cross)])
  47. AC_MSG_RESULT([$ac_cv_have_working_snprintf])
  48. AC_MSG_CHECKING(for working vsnprintf)
  49. AC_CACHE_VAL(ac_cv_have_working_vsnprintf,
  50. [AC_TRY_RUN(
  51. [#include <stdio.h>
  52. #include <stdarg.h>
  53. int my_vsnprintf (char *buf, const char *tmpl, ...)
  54. {
  55. int i;
  56. va_list args;
  57. va_start (args, tmpl);
  58. i = vsnprintf (buf, 2, tmpl, args);
  59. va_end (args);
  60. return i;
  61. }
  62. int main(void)
  63. {
  64. char bufs[5] = { 'x', 'x', 'x', '\0', '\0' };
  65. char bufd[5] = { 'x', 'x', 'x', '\0', '\0' };
  66. int i;
  67. i = my_vsnprintf (bufs, "%s", "111");
  68. if (strcmp (bufs, "1")) exit (1);
  69. if (i != 3) exit (1);
  70. i = my_vsnprintf (bufd, "%d", 111);
  71. if (strcmp (bufd, "1")) exit (1);
  72. if (i != 3) exit (1);
  73. exit(0);
  74. }], ac_cv_have_working_vsnprintf=yes, ac_cv_have_working_vsnprintf=no, ac_cv_have_working_vsnprintf=cross)])
  75. AC_MSG_RESULT([$ac_cv_have_working_vsnprintf])
  76. if test x$ac_cv_have_working_snprintf$ac_cv_have_working_vsnprintf != "xyesyes"; then
  77. AC_LIBOBJ(snprintf)
  78. AC_MSG_WARN([Replacing missing/broken (v)snprintf() with version from http://www.ijs.si/software/snprintf/.])
  79. AC_DEFINE(PREFER_PORTABLE_SNPRINTF, 1, "enable replacement (v)snprintf if system (v)snprintf is broken")
  80. fi])