Debian Codegraph @

m4/ax_func_snprintf.m4

raw
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 <[email protected]>
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
29 AU_ALIAS([AC_FUNC_SNPRINTF], [AX_FUNC_SNPRINTF])
30 AC_DEFUN([AX_FUNC_SNPRINTF],
31 [AC_CHECK_FUNCS(snprintf vsnprintf)
32 AC_MSG_CHECKING(for working snprintf)
33 AC_CACHE_VAL(ac_cv_have_working_snprintf,
34 [AC_TRY_RUN(
35 [#include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 int main(void)
40 {
41 char bufs[5] = { 'x', 'x', 'x', '\0', '\0' };
42 char bufd[5] = { 'x', 'x', 'x', '\0', '\0' };
43 int i;
44 i = snprintf (bufs, 2, "%s", "111");
45 if (strcmp (bufs, "1")) exit (1);
46 if (i != 3) exit (1);
47 i = snprintf (bufd, 2, "%d", 111);
48 if (strcmp (bufd, "1")) exit (1);
49 if (i != 3) exit (1);
50 exit(0);
51 }], ac_cv_have_working_snprintf=yes, ac_cv_have_working_snprintf=no, ac_cv_have_working_snprintf=cross)])
52 AC_MSG_RESULT([$ac_cv_have_working_snprintf])
53 AC_MSG_CHECKING(for working vsnprintf)
54 AC_CACHE_VAL(ac_cv_have_working_vsnprintf,
55 [AC_TRY_RUN(
56 [#include <stdio.h>
57 #include <stdarg.h>
58 #include <stdlib.h>
59 #include <string.h>
60
61 int my_vsnprintf (char *buf, const char *tmpl, ...)
62 {
63 int i;
64 va_list args;
65 va_start (args, tmpl);
66 i = vsnprintf (buf, 2, tmpl, args);
67 va_end (args);
68 return i;
69 }
70
71 int main(void)
72 {
73 char bufs[5] = { 'x', 'x', 'x', '\0', '\0' };
74 char bufd[5] = { 'x', 'x', 'x', '\0', '\0' };
75 int i;
76 i = my_vsnprintf (bufs, "%s", "111");
77 if (strcmp (bufs, "1")) exit (1);
78 if (i != 3) exit (1);
79 i = my_vsnprintf (bufd, "%d", 111);
80 if (strcmp (bufd, "1")) exit (1);
81 if (i != 3) exit (1);
82 exit(0);
83 }], ac_cv_have_working_vsnprintf=yes, ac_cv_have_working_vsnprintf=no, ac_cv_have_working_vsnprintf=cross)])
84 AC_MSG_RESULT([$ac_cv_have_working_vsnprintf])
85 if test x$ac_cv_have_working_snprintf$ac_cv_have_working_vsnprintf != "xyesyes"; then
86 AC_LIBOBJ(snprintf)
87 AC_MSG_WARN([Replacing missing/broken (v)snprintf() with version from http://www.ijs.si/software/snprintf/.])
88 AC_DEFINE(PREFER_PORTABLE_SNPRINTF, 1, "enable replacement (v)snprintf if system (v)snprintf is broken")
89 fi])