1 dnl @synopsis AC_FUNC_SNPRINTF
3 dnl Checks for a fully C99 compliant snprintf, in particular checks
4 dnl whether it does bounds checking and returns the correct string
5 dnl length; does the same check for vsnprintf. If no working snprintf
6 dnl or vsnprintf is found, request a replacement and warn the user
7 dnl about it. Note: the mentioned replacement is freely available and
8 dnl may be used in any project regardless of it's licence (just like
9 dnl the autoconf special exemption).
12 dnl @author RĂ¼diger Kuhlmann <info@ruediger-kuhlmann.de>
13 dnl @version 2002-09-26
14 dnl @license AllPermissive
16 AC_DEFUN([AC_FUNC_SNPRINTF],
17 [AC_CHECK_FUNCS(snprintf vsnprintf)
18 AC_MSG_CHECKING(for working snprintf)
19 AC_CACHE_VAL(ac_cv_have_working_snprintf,
25 char bufs[5] = { 'x', 'x', 'x', '\0', '\0' };
26 char bufd[5] = { 'x', 'x', 'x', '\0', '\0' };
28 i = snprintf (bufs, 2, "%s", "111");
29 if (strcmp (bufs, "1")) exit (1);
31 i = snprintf (bufd, 2, "%d", 111);
32 if (strcmp (bufd, "1")) exit (1);
35 }], ac_cv_have_working_snprintf=yes, ac_cv_have_working_snprintf=no, ac_cv_have_working_snprintf=cross)])
36 AC_MSG_RESULT([$ac_cv_have_working_snprintf])
37 AC_MSG_CHECKING(for working vsnprintf)
38 AC_CACHE_VAL(ac_cv_have_working_vsnprintf,
43 int my_vsnprintf (char *buf, const char *tmpl, ...)
47 va_start (args, tmpl);
48 i = vsnprintf (buf, 2, tmpl, args);
55 char bufs[5] = { 'x', 'x', 'x', '\0', '\0' };
56 char bufd[5] = { 'x', 'x', 'x', '\0', '\0' };
58 i = my_vsnprintf (bufs, "%s", "111");
59 if (strcmp (bufs, "1")) exit (1);
61 i = my_vsnprintf (bufd, "%d", 111);
62 if (strcmp (bufd, "1")) exit (1);
65 }], ac_cv_have_working_vsnprintf=yes, ac_cv_have_working_vsnprintf=no, ac_cv_have_working_vsnprintf=cross)])
66 AC_MSG_RESULT([$ac_cv_have_working_vsnprintf])
67 if test x$ac_cv_have_working_snprintf$ac_cv_have_working_vsnprintf != "xyesyes"; then
69 AC_MSG_WARN([Replacing missing/broken (v)snprintf() with version from http://www.ijs.si/software/snprintf/.])
70 AC_DEFINE(PREFER_PORTABLE_SNPRINTF, 1, "enable replacement (v)snprintf if system (v)snprintf is broken")