Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2599ef9fa7a03360abecdf2f8bce646f82cdb257
[simgrid.git] / acmacro / ac_func_asprintf.m4
1 dnl @synopsis AC_FUNC_ASPRINTF
2 dnl
3 dnl Checks for a compilable asprintf. 
4
5 dnl Define NEED_ASPRINTF  (to 1) if no working asprintf is found
6 dnl Define NEED_VASPRINTF (to 1) if no working vasprintf is found
7 dnl
8 dnl Note: the mentioned replacement is freely available and
9 dnl may be used in any project regardless of it's licence (just like
10 dnl the autoconf special exemption).
11 dnl
12 dnl @category C
13 dnl @author Martin Quinson <Martin.Quinson@loria.fr>
14 dnl @version 2009-12-11
15 dnl @license AllPermissive
16
17 AC_DEFUN([AC_FUNC_ASPRINTF],
18 [AC_CHECK_FUNCS(asprintf vasprintf)
19 AC_MSG_CHECKING(for working asprintf)
20 AC_CACHE_VAL(ac_cv_have_working_asprintf,
21 [AC_TRY_RUN(
22 [#include <stdio.h>
23
24 int main(void)
25 {
26     char *buff;
27     int i = asprintf(&buff, "%s","toto");
28     if (strcmp (buff, "toto")) exit (2);
29     if (i != 4) exit (3);
30     exit(0);
31 }], ac_cv_have_working_asprintf=yes, ac_cv_have_working_asprintf=no, ac_cv_have_working_asprintf=cross)])
32 AC_MSG_RESULT([$ac_cv_have_working_asprintf])
33 AC_MSG_CHECKING(for working vasprintf)
34 AC_CACHE_VAL(ac_cv_have_working_vasprintf,
35 [AC_TRY_RUN(
36 [#include <stdio.h>
37 #include <stdarg.h>
38
39 int my_vasprintf (char **buf, const char *tmpl, ...)
40 {
41     int i;
42     va_list args;
43     va_start (args, tmpl);
44     i = vasprintf (buf, tmpl, args);
45     va_end (args);
46     return i;
47 }
48
49 int main(void)
50 {
51     char *buff;
52     int i = my_vasprintf(&buff, "%s","toto");
53     if (strcmp (buff, "toto")) exit (2);
54     if (i != 4) exit (3);
55     exit(0);
56 }], ac_cv_have_working_vasprintf=yes, ac_cv_have_working_vasprintf=no, ac_cv_have_working_vasprintf=cross)])
57 AC_MSG_RESULT([$ac_cv_have_working_vasprintf])
58 if test x$ac_cv_have_working_asprintf$ac_cv_have_working_vasprintf != "xyesyes"; then
59 #  AC_LIBOBJ(asprintf)
60   AC_MSG_WARN([Replacing missing/broken (v)asprintf() with internal version.])
61   AC_DEFINE(NEED_ASPRINTF,  1, enable the asprintf replacement)
62   AC_DEFINE(NEED_VASPRINTF, 1, enable the vasprintf replacement)
63 AC_DEFINE(PREFER_PORTABLE_ASPRINTF, 1, "enable replacement (v)asprintf if system (v)asprintf is broken")
64 fi])
65