1 dnl Test for a working C99 snprintf.
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).
9 dnl Provides RRA_FUNC_SNPRINTF, which adds snprintf.o to LIBOBJS unless a
10 dnl fully working snprintf is found.
12 dnl The canonical version of this file is maintained in the rra-c-util
13 dnl package, available at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
15 dnl Written by Russ Allbery <eagle@eyrie.org>
16 dnl Copyright 2006, 2008-2009
17 dnl The Board of Trustees of the Leland Stanford Junior University
19 dnl This file is free software; the authors give unlimited permission to copy
20 dnl and/or distribute it, with or without modifications, as long as this
21 dnl notice is preserved.
23 dnl SPDX-License-Identifier: FSFULLR
25 dnl Source used by RRA_FUNC_SNPRINTF.
26 AC_DEFUN([_RRA_FUNC_SNPRINTF_SOURCE], [[
33 test(char *format, ...)
38 va_start(args, format);
39 count = vsnprintf(buf, sizeof buf, format, args);
47 return ((test("%s", "abcd") == 4 && buf[0] == 'a' && buf[1] == '\0'
48 && snprintf(NULL, 0, "%s", "abcd") == 4) ? 0 : 1);
52 dnl The user-callable test.
53 AC_DEFUN([RRA_FUNC_SNPRINTF],
54 [AC_CACHE_CHECK([for working snprintf], [rra_cv_func_snprintf_works],
55 [AC_RUN_IFELSE([AC_LANG_SOURCE([_RRA_FUNC_SNPRINTF_SOURCE])],
56 [rra_cv_func_snprintf_works=yes],
57 [rra_cv_func_snprintf_works=no],
58 [rra_cv_func_snprintf_works=no])])
59 AS_IF([test x"$rra_cv_func_snprintf_works" = xyes],
60 [AC_DEFINE([HAVE_SNPRINTF], 1,
61 [Define if your system has a working snprintf function.])],
62 [AC_LIBOBJ([snprintf])])])