1
2dnl @synopsis FC_FUNC_VSNPRINTF
3dnl
4dnl Check whether there is a reasonably sane vsnprintf() function installed.
5dnl "Reasonably sane" in this context means never clobbering memory beyond
6dnl the buffer supplied, and having a sensible return value.  It is
7dnl explicitly allowed not to NUL-terminate the return value, however.
8dnl
9dnl @version $Id$
10dnl @author Gaute Strokkenes <gs234@cam.ac.uk>
11dnl
12AC_DEFUN([FC_FUNC_VSNPRINTF],
13[AC_CACHE_CHECK([for working vsnprintf],
14  [ac_cv_func_working_vsnprintf],
15[AC_RUN_IFELSE([AC_LANG_SOURCE([[#include <stdio.h>
16#include <stdarg.h>
17
18int
19doit(char * s, ...)
20{
21  char buffer[32];
22  va_list args;
23  int r;
24
25  buffer[5] = 'X';
26
27  va_start(args, s);
28  r = vsnprintf(buffer, 5, s, args);
29  va_end(args);
30
31  /* -1 is pre-C99, 7 is C99. */
32
33  if (r != -1 && r != 7)
34    exit(1);
35
36  /* We deliberately do not care if the result is NUL-terminated or
37     not, since this is easy to work around like this.  */
38
39  buffer[4] = 0;
40
41  /* Simple sanity check.  */
42
43  if (strcmp(buffer, "1234"))
44    exit(1);
45
46  if (buffer[5] != 'X')
47    exit(1);
48
49  exit(0);
50}
51
52int
53main(void)
54{
55  doit("1234567");
56  exit(1);
57}]])],[ac_cv_func_working_vsnprintf=yes],[ac_cv_func_working_vsnprintf=no],[ac_cv_func_working_vsnprintf=no])])
58dnl Note that the default is to be pessimistic in the case of cross compilation.
59dnl If you know that the target has a sensible vsnprintf(), you can get around this
60dnl by setting ac_func_vsnprintf to yes, as described in the Autoconf manual.
61if test $ac_cv_func_working_vsnprintf = yes; then
62  AC_DEFINE([HAVE_WORKING_VSNPRINTF], [1],
63            [Define if you have a version of the 'vsnprintf' function
64             that honours the size argument and has a proper return value.])
65fi
66])# FC_FUNC_VSNPRINTF
67