1# ===========================================================================
2#     http://www.gnu.org/software/autoconf-archive/ax_func_snprintf.html
3# ===========================================================================
4#
5# SYNOPSIS
6#
7#   AX_FUNC_SNPRINTF
8#
9# DESCRIPTION
10#
11#   Checks for a fully C99 compliant snprintf, in particular checks whether
12#   it does bounds checking and returns the correct string length; does the
13#   same check for vsnprintf. If no working snprintf or vsnprintf is found,
14#   request a replacement and warn the user about it. Note: the mentioned
15#   replacement is freely available and may be used in any project
16#   regardless of it's license.
17#
18# LICENSE
19#
20#   Copyright (c) 2008 Ruediger Kuhlmann <info@ruediger-kuhlmann.de>
21#
22#   Copying and distribution of this file, with or without modification, are
23#   permitted in any medium without royalty provided the copyright notice
24#   and this notice are preserved. This file is offered as-is, without any
25#   warranty.
26
27#serial 5
28
29AC_DEFUN([AX_FUNC_SNPRINTF],
30[AC_CHECK_FUNCS(snprintf vsnprintf)
31AC_MSG_CHECKING(for working snprintf)
32AC_CACHE_VAL(ac_cv_have_working_snprintf,
33[AC_RUN_IFELSE([AC_LANG_SOURCE([[#include <stdio.h>
34#include <string.h>
35
36int main(void)
37{
38    char bufs[5] = { 'x', 'x', 'x', '\0', '\0' };
39    char bufd[5] = { 'x', 'x', 'x', '\0', '\0' };
40    int i;
41    i = snprintf (bufs, 2, "%s", "111");
42    if (strcmp (bufs, "1")) return (1);
43    if (i != 3) return (1);
44    i = snprintf (bufd, 2, "%d", 111);
45    if (strcmp (bufd, "1")) return (1);
46    if (i != 3) return (1);
47    return(0);
48}]])],[ac_cv_have_working_snprintf=yes],[ac_cv_have_working_snprintf=no],[ac_cv_have_working_snprintf=cross])])
49AC_MSG_RESULT([$ac_cv_have_working_snprintf])
50AC_MSG_CHECKING(for working vsnprintf)
51AC_CACHE_VAL(ac_cv_have_working_vsnprintf,
52[AC_RUN_IFELSE([AC_LANG_SOURCE([[#include <stdio.h>
53#include <stdarg.h>
54#include <string.h>
55
56int my_vsnprintf (char *buf, const char *tmpl, ...)
57{
58    int i;
59    va_list args;
60    va_start (args, tmpl);
61    i = vsnprintf (buf, 2, tmpl, args);
62    va_end (args);
63    return i;
64}
65
66int main(void)
67{
68    char bufs[5] = { 'x', 'x', 'x', '\0', '\0' };
69    char bufd[5] = { 'x', 'x', 'x', '\0', '\0' };
70    int i;
71    i = my_vsnprintf (bufs, "%s", "111");
72    if (strcmp (bufs, "1")) return (1);
73    if (i != 3) return (1);
74    i = my_vsnprintf (bufd, "%d", 111);
75    if (strcmp (bufd, "1")) return (1);
76    if (i != 3) return (1);
77    return(0);
78}]])],[ac_cv_have_working_vsnprintf=yes],[ac_cv_have_working_vsnprintf=no],[ac_cv_have_working_vsnprintf=cross])])
79AC_MSG_RESULT([$ac_cv_have_working_vsnprintf])
80if test x$ac_cv_have_working_snprintf$ac_cv_have_working_vsnprintf != "xyesyes"; then
81  AC_LIBOBJ(snprintf)
82  AC_MSG_WARN([Replacing missing/broken (v)snprintf() with sudo's version.])
83  AC_DEFINE(PREFER_PORTABLE_SNPRINTF, 1, [Enable replacement (v)snprintf if system (v)snprintf is broken.])
84fi])
85