Home | History | Annotate | Download | only in m4
      1 dnl From http://autoconf-archive.cryp.to/
      2 
      3 dnl @synopsis AC_FUNC_SNPRINTF
      4 dnl
      5 dnl Checks for a fully C99 compliant snprintf, in particular checks
      6 dnl whether it does bounds checking and returns the correct string
      7 dnl length; does the same check for vsnprintf. If no working snprintf
      8 dnl or vsnprintf is found, request a replacement and warn the user
      9 dnl about it. Note: the mentioned replacement is freely available and
     10 dnl may be used in any project regardless of it's licence (just like
     11 dnl the autoconf special exemption).
     12 dnl
     13 dnl @category C
     14 dnl @author Rdiger Kuhlmann <info (a] ruediger-kuhlmann.de>
     15 dnl @version 2002-09-26
     16 dnl @license AllPermissive
     17 
     18 AC_DEFUN([AC_FUNC_SNPRINTF],
     19 [AC_CHECK_FUNCS(snprintf vsnprintf)
     20 AC_MSG_CHECKING(for working snprintf)
     21 AC_CACHE_VAL(ac_cv_have_working_snprintf,
     22 [AC_TRY_RUN(
     23 [#include <stdio.h>
     24 
     25 int 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)])
     38 AC_MSG_RESULT([$ac_cv_have_working_snprintf])
     39 AC_MSG_CHECKING(for working vsnprintf)
     40 AC_CACHE_VAL(ac_cv_have_working_vsnprintf,
     41 [AC_TRY_RUN(
     42 [#include <stdio.h>
     43 #include <stdarg.h>
     44 
     45 int 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 
     55 int 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)])
     68 AC_MSG_RESULT([$ac_cv_have_working_vsnprintf])
     69 if 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")
     73 fi])
     74