mirror of
https://github.com/transmission/transmission
synced 2025-03-03 10:15:45 +00:00
Add asprintf implementation for systems which lack it.
This commit is contained in:
parent
ffeef6083a
commit
adc0d4ac92
3 changed files with 87 additions and 0 deletions
41
configure
vendored
41
configure
vendored
|
@ -180,6 +180,45 @@ EOF
|
|||
rm -f testconf*
|
||||
}
|
||||
|
||||
asprintf_test()
|
||||
{
|
||||
verbose asprintf_test
|
||||
cat > testconf.c <<EOF
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
asprintf( NULL, NULL );
|
||||
return 0;
|
||||
}
|
||||
EOF
|
||||
if runcmd $CC -o testconf testconf.c
|
||||
then
|
||||
CFLAGS="$CFLAGS -DHAVE_ASPRINTF"
|
||||
fi
|
||||
rm -f testconf*
|
||||
}
|
||||
|
||||
snprintf_test()
|
||||
{
|
||||
verbose snprintf_test
|
||||
cat > testconf.c <<EOF
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
char buf[] = "blueberry";
|
||||
return ( 6 != snprintf( buf, 4, "%s%s", "foo", "bar" ) ||
|
||||
0 != memcmp( buf, "foo\0berry", 9 ) );
|
||||
}
|
||||
EOF
|
||||
if ! runcmd $CC -o testconf testconf.c || ! runcmd ./testconf
|
||||
then
|
||||
echo "error: broken snprintf() found"
|
||||
exit 1
|
||||
fi
|
||||
rm -f testconf*
|
||||
}
|
||||
|
||||
libgen_test()
|
||||
{
|
||||
verbose libgen_test
|
||||
|
@ -467,6 +506,8 @@ lrintf_test
|
|||
#
|
||||
strlcpy_test
|
||||
strlcat_test
|
||||
asprintf_test
|
||||
snprintf_test
|
||||
libgen_test
|
||||
|
||||
#
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#ifndef TRCOMPAT_H
|
||||
#define TRCOMPAT_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h> /* for size_t */
|
||||
|
||||
#ifndef HAVE_STRLCPY
|
||||
|
@ -36,6 +37,11 @@ size_t
|
|||
strlcat(char *dst, const char *src, size_t siz);
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_ASPRINTF
|
||||
int asprintf( char **, const char *, ... );
|
||||
int vasprintf( char **, const char *, va_list );
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_LIBGEN
|
||||
# include <libgen.h>
|
||||
#else
|
||||
|
|
|
@ -28,7 +28,9 @@
|
|||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h> /* usleep, stat */
|
||||
|
||||
#include "transmission.h"
|
||||
#include "trcompat.h"
|
||||
|
||||
#define SPRINTF_BUFSIZE 100
|
||||
|
||||
|
@ -319,6 +321,44 @@ int tr_vsprintf( char ** buf, int * used, int * max, const char * fmt,
|
|||
return 0;
|
||||
}
|
||||
|
||||
#ifndef HAVE_ASPRINTF
|
||||
|
||||
int
|
||||
asprintf( char ** buf, const char * format, ... )
|
||||
{
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
va_start( ap, format );
|
||||
ret = vasprintf( buf, format, ap );
|
||||
va_end( ap );
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
vasprintf( char ** buf, const char * format, va_list ap )
|
||||
{
|
||||
va_list ap2;
|
||||
int ret, used, max;
|
||||
|
||||
va_copy( ap2, ap );
|
||||
|
||||
*buf = NULL;
|
||||
used = 0;
|
||||
max = 0;
|
||||
|
||||
if( tr_vsprintf( buf, &used, &max, format, ap, ap2 ) )
|
||||
{
|
||||
free( *buf );
|
||||
return -1;
|
||||
}
|
||||
|
||||
return used;
|
||||
}
|
||||
|
||||
#endif /* HAVE_ASPRINTF */
|
||||
|
||||
int tr_concat( char ** buf, int * used, int * max, const char * data, int len )
|
||||
{
|
||||
int newmax;
|
||||
|
|
Loading…
Reference in a new issue