Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make the check that RPC return something more strict
[simgrid.git] / acmacro / ac_func_snprintf.m4
1 dnl @synopsis AC_FUNC_SNPRINTF
2 dnl
3 dnl Checks for a fully C99 compliant snprintf, in particular checks
4 dnl whether it does bounds checking and returns the correct string
5 dnl length; does the same check for vsnprintf. If no working snprintf
6 dnl or vsnprintf is found, request a replacement and warn the user
7 dnl about it. Note: the mentioned replacement is freely available and
8 dnl may be used in any project regardless of it's licence (just like
9 dnl the autoconf special exemption).
10 dnl
11 dnl @category C
12 dnl @author RĂ¼diger Kuhlmann <info@ruediger-kuhlmann.de>
13 dnl @version 2002-09-26
14 dnl @license AllPermissive
15
16 AC_DEFUN([AC_FUNC_SNPRINTF],
17 [AC_CHECK_FUNCS(snprintf vsnprintf)
18 AC_MSG_CHECKING(for working snprintf)
19 AC_CACHE_VAL(ac_cv_have_working_snprintf,
20 [AC_TRY_RUN(
21 [#include <stdio.h>
22
23 int main(void)
24 {
25     char bufs[5] = { 'x', 'x', 'x', '\0', '\0' };
26     char bufd[5] = { 'x', 'x', 'x', '\0', '\0' };
27     int i;
28     i = snprintf (bufs, 2, "%s", "111");
29     if (strcmp (bufs, "1")) exit (1);
30     if (i != 3) exit (1);
31     i = snprintf (bufd, 2, "%d", 111);
32     if (strcmp (bufd, "1")) exit (1);
33     if (i != 3) exit (1);
34     exit(0);
35 }], ac_cv_have_working_snprintf=yes, ac_cv_have_working_snprintf=no, ac_cv_have_working_snprintf=cross)])
36 AC_MSG_RESULT([$ac_cv_have_working_snprintf])
37 AC_MSG_CHECKING(for working vsnprintf)
38 AC_CACHE_VAL(ac_cv_have_working_vsnprintf,
39 [AC_TRY_RUN(
40 [#include <stdio.h>
41 #include <stdarg.h>
42
43 int my_vsnprintf (char *buf, const char *tmpl, ...)
44 {
45     int i;
46     va_list args;
47     va_start (args, tmpl);
48     i = vsnprintf (buf, 2, tmpl, args);
49     va_end (args);
50     return i;
51 }
52
53 int main(void)
54 {
55     char bufs[5] = { 'x', 'x', 'x', '\0', '\0' };
56     char bufd[5] = { 'x', 'x', 'x', '\0', '\0' };
57     int i;
58     i = my_vsnprintf (bufs, "%s", "111");
59     if (strcmp (bufs, "1")) exit (1);
60     if (i != 3) exit (1);
61     i = my_vsnprintf (bufd, "%d", 111);
62     if (strcmp (bufd, "1")) exit (1);
63     if (i != 3) exit (1);
64     exit(0);
65 }], ac_cv_have_working_vsnprintf=yes, ac_cv_have_working_vsnprintf=no, ac_cv_have_working_vsnprintf=cross)])
66 AC_MSG_RESULT([$ac_cv_have_working_vsnprintf])
67 if test x$ac_cv_have_working_snprintf$ac_cv_have_working_vsnprintf != "xyesyes"; then
68   AC_LIBOBJ(snprintf)
69   AC_MSG_WARN([Replacing missing/broken (v)snprintf() with version from http://www.ijs.si/software/snprintf/.])
70   AC_DEFINE(PREFER_PORTABLE_SNPRINTF, 1, "enable replacement (v)snprintf if system (v)snprintf is broken")
71 fi])
72