• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1dnl From http://autoconf-archive.cryp.to/
2
3dnl @synopsis AC_FUNC_SNPRINTF
4dnl
5dnl Checks for a fully C99 compliant snprintf, in particular checks
6dnl whether it does bounds checking and returns the correct string
7dnl length; does the same check for vsnprintf. If no working snprintf
8dnl or vsnprintf is found, request a replacement and warn the user
9dnl about it. Note: the mentioned replacement is freely available and
10dnl may be used in any project regardless of it's licence (just like
11dnl the autoconf special exemption).
12dnl
13dnl @category C
14dnl @author R�diger Kuhlmann <info@ruediger-kuhlmann.de>
15dnl @version 2002-09-26
16dnl @license AllPermissive
17
18AC_DEFUN([AC_FUNC_SNPRINTF],
19[AC_CHECK_FUNCS(snprintf vsnprintf)
20AC_MSG_CHECKING(for working snprintf)
21AC_CACHE_VAL(ac_cv_have_working_snprintf,
22[AC_TRY_RUN(
23[#include <stdio.h>
24
25int main(void)
26{
27    char bufs[5] = { 'x', 'x', 'x', '\0', '\0' };
28    char bufd[5] = { 'x', 'x', 'x', '\0', '\0' };
29    int i;
30    i = snprintf (bufs, 2, "%s", "111");
31    if (strcmp (bufs, "1")) exit (1);
32    if (i != 3) exit (1);
33    i = snprintf (bufd, 2, "%d", 111);
34    if (strcmp (bufd, "1")) exit (1);
35    if (i != 3) exit (1);
36    exit(0);
37}], ac_cv_have_working_snprintf=yes, ac_cv_have_working_snprintf=no, ac_cv_have_working_snprintf=cross)])
38AC_MSG_RESULT([$ac_cv_have_working_snprintf])
39AC_MSG_CHECKING(for working vsnprintf)
40AC_CACHE_VAL(ac_cv_have_working_vsnprintf,
41[AC_TRY_RUN(
42[#include <stdio.h>
43#include <stdarg.h>
44
45int my_vsnprintf (char *buf, const char *tmpl, ...)
46{
47    int i;
48    va_list args;
49    va_start (args, tmpl);
50    i = vsnprintf (buf, 2, tmpl, args);
51    va_end (args);
52    return i;
53}
54
55int main(void)
56{
57    char bufs[5] = { 'x', 'x', 'x', '\0', '\0' };
58    char bufd[5] = { 'x', 'x', 'x', '\0', '\0' };
59    int i;
60    i = my_vsnprintf (bufs, "%s", "111");
61    if (strcmp (bufs, "1")) exit (1);
62    if (i != 3) exit (1);
63    i = my_vsnprintf (bufd, "%d", 111);
64    if (strcmp (bufd, "1")) exit (1);
65    if (i != 3) exit (1);
66    exit(0);
67}], ac_cv_have_working_vsnprintf=yes, ac_cv_have_working_vsnprintf=no, ac_cv_have_working_vsnprintf=cross)])
68AC_MSG_RESULT([$ac_cv_have_working_vsnprintf])
69if test x$ac_cv_have_working_snprintf$ac_cv_have_working_vsnprintf != "xyesyes"; then
70  AC_LIBOBJ(snprintf)
71  AC_MSG_WARN([Will use fallback (v)snprintf() implementation.])
72  AC_DEFINE(PREFER_PORTABLE_SNPRINTF, 1, "enable replacement (v)snprintf if system (v)snprintf is broken")
73fi])
74