develooper Front page | perl.perl5.porters | Postings from April 2007

vsprintf

Thread Next
From:
Nicholas Clark
Date:
April 12, 2007 04:46
Subject:
vsprintf
Message ID:
20070412114555.GA69960@plum.flirble.org
So we have this to determine the return value of vsprintf:

: see if vprintf exists
echo " "
if set vprintf val -f d_vprintf; eval $csym; $val; then
	echo 'vprintf() found.' >&4
	val="$define"
	$cat >try.c <<EOF
#include <varargs.h>
#$i_stdlib I_STDLIB
#ifdef I_STDLIB
#include <stdlib.h>
#endif

int main() { xxx("foo"); }

xxx(va_alist)
va_dcl
{
	va_list args;
	char buf[10];

	va_start(args);
	exit((unsigned long)vsprintf(buf,"%s",args) > 10L);
}
EOF
	set try
	if eval $compile && $run ./try; then
		echo "Your vsprintf() returns (int)." >&4
		val2="$undef"
	else
		echo "Your vsprintf() returns (char*)." >&4
		val2="$define"
	fi
else
	echo 'vprintf() NOT found.' >&4
		val="$undef"
		val2="$undef"
fi



And I get:

vprintf() found.
Your vsprintf() returns (char*).


Which is poppycock, because if I try to compile that program, I get:

$ gcc -o try try.c 
In file included from try.c:1:
/usr/include/varargs.h:34:2: #error "<varargs.h> is obsolete with this version of GCC."
/usr/include/varargs.h:35:2: #error "Change your code to use <stdarg.h> instead."
try.c:10: error: syntax error before "va_dcl"
try.c:11: error: syntax error before '{' token
try.c:12: warning: data definition has no type or storage class
try.c:15: warning: parameter names (without types) in function declaration
try.c:15: warning: data definition has no type or storage class
try.c:16: error: syntax error before '(' token




In turn, that symbol is only used here in util.c:

#ifndef HAS_VPRINTF

#ifdef USE_CHAR_VSPRINTF
char *
#else
int
#endif
vsprintf(char *dest, const char *pat, char *args)
{

...

#ifdef USE_CHAR_VSPRINTF
    return(dest);
#else
    return 0;		/* perl doesn't use return value */
#endif
}

#endif /* HAS_VPRINTF */

which is not exactly useful, as USE_CHAR_VSPRINTF is always going to be undef
if HAS_VPRINTF is undef.

So, what to do?

Ignore the cruft (or at least update the glossary to document it) and clone
the sprintf return value test to make a vsprintf return value test?

Or also fix the existing code?

Part of the issue is that really we want a 3 way (or even 4 way) of

0: you don't have vsprintf
1: you have it, but can't figure out the return type
2: you have it, and it's char *
3: you have it, and it's int. (the C89 version)

and really, these days we're only interested in 3 or !3

Nicholas Clark

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About