Debian Codegraph @

m4/ax_func_snprintf.m4

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