]> eyrie.org Git - kerberos/krb5-strength.git/blob - m4/snprintf.m4
Declare fast forward from 3.1-2
[kerberos/krb5-strength.git] / m4 / snprintf.m4
1 dnl Test for a working C99 snprintf.
2 dnl
3 dnl Check for a working snprintf.  Some systems have an snprintf that doesn't
4 dnl nul-terminate if the buffer isn't large enough.  Others return -1 if the
5 dnl string doesn't fit into the buffer instead of returning the number of
6 dnl characters that would have been formatted.  Still others don't support
7 dnl NULL as the buffer argument (just to get a count of the formatted length).
8 dnl
9 dnl Provides RRA_FUNC_SNPRINTF, which adds snprintf.o to LIBOBJS unless a
10 dnl fully working snprintf is found.
11 dnl
12 dnl The canonical version of this file is maintained in the rra-c-util
13 dnl package, available at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
14 dnl
15 dnl Written by Russ Allbery <eagle@eyrie.org>
16 dnl Copyright 2006, 2008-2009
17 dnl     The Board of Trustees of the Leland Stanford Junior University
18 dnl
19 dnl This file is free software; the authors give unlimited permission to copy
20 dnl and/or distribute it, with or without modifications, as long as this
21 dnl notice is preserved.
22 dnl
23 dnl SPDX-License-Identifier: FSFULLR
24
25 dnl Source used by RRA_FUNC_SNPRINTF.
26 AC_DEFUN([_RRA_FUNC_SNPRINTF_SOURCE], [[
27 #include <stdio.h>
28 #include <stdarg.h>
29
30 char buf[2];
31
32 int
33 test(char *format, ...)
34 {
35     va_list args;
36     int count;
37
38     va_start(args, format);
39     count = vsnprintf(buf, sizeof buf, format, args);
40     va_end(args);
41     return count;
42 }
43
44 int
45 main()
46 {
47     return ((test("%s", "abcd") == 4 && buf[0] == 'a' && buf[1] == '\0'
48              && snprintf(NULL, 0, "%s", "abcd") == 4) ? 0 : 1);
49 }
50 ]])
51
52 dnl The user-callable test.
53 AC_DEFUN([RRA_FUNC_SNPRINTF],
54 [AC_CACHE_CHECK([for working snprintf], [rra_cv_func_snprintf_works],
55     [AC_RUN_IFELSE([AC_LANG_SOURCE([_RRA_FUNC_SNPRINTF_SOURCE])],
56         [rra_cv_func_snprintf_works=yes],
57         [rra_cv_func_snprintf_works=no],
58         [rra_cv_func_snprintf_works=no])])
59  AS_IF([test x"$rra_cv_func_snprintf_works" = xyes],
60     [AC_DEFINE([HAVE_SNPRINTF], 1,
61         [Define if your system has a working snprintf function.])],
62     [AC_LIBOBJ([snprintf])])])