]> eyrie.org Git - kerberos/krb5-strength.git/blob - m4/snprintf.m4
Change CrackLib tests for system CrackLib
[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
23 dnl Source used by RRA_FUNC_SNPRINTF.
24 AC_DEFUN([_RRA_FUNC_SNPRINTF_SOURCE], [[
25 #include <stdio.h>
26 #include <stdarg.h>
27
28 char buf[2];
29
30 int
31 test(char *format, ...)
32 {
33     va_list args;
34     int count;
35
36     va_start(args, format);
37     count = vsnprintf(buf, sizeof buf, format, args);
38     va_end(args);
39     return count;
40 }
41
42 int
43 main()
44 {
45     return ((test("%s", "abcd") == 4 && buf[0] == 'a' && buf[1] == '\0'
46              && snprintf(NULL, 0, "%s", "abcd") == 4) ? 0 : 1);
47 }
48 ]])
49
50 dnl The user-callable test.
51 AC_DEFUN([RRA_FUNC_SNPRINTF],
52 [AC_CACHE_CHECK([for working snprintf], [rra_cv_func_snprintf_works],
53     [AC_RUN_IFELSE([AC_LANG_SOURCE([_RRA_FUNC_SNPRINTF_SOURCE])],
54         [rra_cv_func_snprintf_works=yes],
55         [rra_cv_func_snprintf_works=no],
56         [rra_cv_func_snprintf_works=no])])
57  AS_IF([test x"$rra_cv_func_snprintf_works" = xyes],
58     [AC_DEFINE([HAVE_SNPRINTF], 1,
59         [Define if your system has a working snprintf function.])],
60     [AC_LIBOBJ([snprintf])])])