?RCS: Copyright (c) 2006-2007, H.Merijn Brand
?RCS:
?RCS: You may redistribute only under the terms of the Artistic License,
?RCS: as specified in the README file that comes with the distribution.
?RCS: You may reuse parts of this distribution only within the terms of
?RCS: that same Artistic License; a copy of which may be found at the root
?RCS: of the source tree for dist 3.0.
?RCS:
?MAKE:d_snprintf d_vsnprintf : Inlibc Compile cat run rm_try
?MAKE:	-pick add $@ %<
?S:d_snprintf:
?S:	This variable conditionally defines the HAS_SNPRINTF symbol, which
?S:	indicates to the C program that the snprintf () library function
?S:	is available.
?S:.
?S:d_vsnprintf:
?S:	This variable conditionally defines the HAS_VSNPRINTF symbol, which
?S:	indicates to the C program that the vsnprintf () library function
?S:	is available.
?S:.
?C:HAS_SNPRINTF:
?C:	This symbol, if defined, indicates that the snprintf () library
?C:	function is available for use.
?C:.
?C:HAS_VSNPRINTF:
?C:	This symbol, if defined, indicates that the vsnprintf () library
?C:	function is available for use.
?C:.
?H:#$d_snprintf HAS_SNPRINTF	/**/
?H:#$d_vsnprintf HAS_VSNPRINTF	/**/
?H:.
?F:!try
: see if snprintf exists
set snprintf d_snprintf
eval $inlibc

: see if vsnprintf exists
set vsnprintf d_vsnprintf
eval $inlibc

case "$d_snprintf-$d_vsnprintf" in
"$define-$define")
    $cat <<EOM
Checking whether your snprintf() and vsnprintf() work okay...
EOM
    $cat >try.c <<'EOCP'
/* v?snprintf testing logic courtesy of Russ Allbery.
 * According to C99:
 * - if the buffer is too short it still must be \0-terminated
 * - if the buffer is too short the potentially required length
 *   must be returned and not -1
 * - if the buffer is NULL the potentially required length
 *   must be returned and not -1 or core dump
 */
#include <stdio.h>
#include <stdarg.h>

char buf[2];

int test (char *format, ...)
{
    va_list args;
    int count;

    va_start (args, format);
    count = vsnprintf (buf, sizeof buf, format, args);
    va_end (args);
    return count;
}

int main ()
{
    return ((test ("%s", "abcd") == 4 && buf[0] == 'a' && buf[1] == '\0'
             && snprintf (NULL, 0, "%s", "abcd") == 4) ? 0 : 1);
}
EOCP
    set try
    if eval $compile; then
	`$run ./try`
	case "$?" in
	0) echo "Your snprintf() and vsnprintf() seem to be working okay." ;;
	*) cat <<EOM >&4
Your snprintf() and snprintf() don't seem to be working okay.
EOM
	   d_snprintf="$undef"
	   d_vsnprintf="$undef"
	   ;;
	esac
    else
	echo "(I can't seem to compile the test program--assuming they don't)"
	d_snprintf="$undef"
	d_vsnprintf="$undef"
    fi
    $rm_try
    ;;
esac

