1 | dnl Test for a working C99 snprintf. |
---|
2 | dnl |
---|
3 | dnl Check for a working snprintf. Some systems have an snprintf that doesn't |
---|
4 | dnl nul-terminate if the buffer isn't large enough. Others return -1 if the |
---|
5 | dnl string doesn't fit into the buffer instead of returning the number of |
---|
6 | dnl characters that would have been formatted. Still others don't support |
---|
7 | dnl NULL as the buffer argument (just to get a count of the formatted length). |
---|
8 | dnl |
---|
9 | dnl Provides RRA_FUNC_SNPRINTF, which adds snprintf.o to LIBOBJS unless a |
---|
10 | dnl fully working snprintf is found. |
---|
11 | dnl |
---|
12 | dnl Written by Russ Allbery <rra@stanford.edu> |
---|
13 | dnl Copyright 2006, 2008, 2009 |
---|
14 | dnl Board of Trustees, Leland Stanford Jr. University |
---|
15 | dnl |
---|
16 | dnl See LICENSE for licensing terms. |
---|
17 | |
---|
18 | dnl Source used by RRA_FUNC_SNPRINTF. |
---|
19 | AC_DEFUN([_RRA_FUNC_SNPRINTF_SOURCE], [[ |
---|
20 | #include <stdio.h> |
---|
21 | #include <stdarg.h> |
---|
22 | |
---|
23 | char buf[2]; |
---|
24 | |
---|
25 | int |
---|
26 | test(char *format, ...) |
---|
27 | { |
---|
28 | va_list args; |
---|
29 | int count; |
---|
30 | |
---|
31 | va_start(args, format); |
---|
32 | count = vsnprintf(buf, sizeof buf, format, args); |
---|
33 | va_end(args); |
---|
34 | return count; |
---|
35 | } |
---|
36 | |
---|
37 | int |
---|
38 | main() |
---|
39 | { |
---|
40 | return ((test("%s", "abcd") == 4 && buf[0] == 'a' && buf[1] == '\0' |
---|
41 | && snprintf(NULL, 0, "%s", "abcd") == 4) ? 0 : 1); |
---|
42 | } |
---|
43 | ]]) |
---|
44 | |
---|
45 | dnl The user-callable test. |
---|
46 | AC_DEFUN([RRA_FUNC_SNPRINTF], |
---|
47 | [AC_CACHE_CHECK([for working snprintf], [rra_cv_func_snprintf_works], |
---|
48 | [AC_RUN_IFELSE([AC_LANG_SOURCE([_RRA_FUNC_SNPRINTF_SOURCE])], |
---|
49 | [rra_cv_func_snprintf_works=yes], |
---|
50 | [rra_cv_func_snprintf_works=no], |
---|
51 | [rra_cv_func_snprintf_works=no])]) |
---|
52 | AS_IF([test "$rra_cv_func_snprintf_works" = yes], |
---|
53 | [AC_DEFINE([HAVE_SNPRINTF], 1, |
---|
54 | [Define if your system has a working snprintf function.])], |
---|
55 | [AC_LIBOBJ([snprintf])])]) |
---|