[v9_10] add %z format options to printf

4123.	[port]		Added %z (size_t) format options to the portable
			internal printf/sprintf implementation. [RT #39586]

(cherry picked from commit cadf8d687b)
This commit is contained in:
Evan Hunt
2015-05-21 14:55:38 -07:00
parent 1f71e84ce5
commit 54b00a777b
9 changed files with 302 additions and 106 deletions

View File

@@ -91,7 +91,8 @@ sockaddr_test@EXEEXT@: sockaddr_test.@O@ isctest.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
sockaddr_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS}
print_test@EXEEXT@: print_test.@O@ ${ISCDEPLIBS} ${top_srcdir}/lib/isc/print.c
print_test.@O@: ${top_srcdir}/lib/isc/print.c
print_test@EXEEXT@: print_test.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
print_test.@O@ ${ISCLIBS} ${LIBS}

View File

@@ -28,11 +28,17 @@
* it on all platforms even if we don't include it in libisc.
*/
#include <isc/platform.h>
#if !defined(ISC_PLATFORM_NEEDVSNPRINTF) && !defined(ISC_PLATFORM_NEEDSPRINTF)
#if !defined(ISC_PLATFORM_NEEDPRINTF) && \
!defined(ISC_PLATFORM_NEEDFPRINTF) && \
!defined(ISC_PLATFORM_NEEDSPRINTF) && \
!defined(ISC_PLATFORM_NEEDVSNPRINTF)
#define ISC__PRINT_SOURCE
#include "../print.c"
#else
#if !defined(ISC_PLATFORM_NEEDVSNPRINTF) || !defined(ISC_PLATFORM_NEEDSPRINTF)
#if !defined(ISC_PLATFORM_NEEDPRINTF) || \
!defined(ISC_PLATFORM_NEEDFPRINTF) || \
!defined(ISC_PLATFORM_NEEDSPRINTF) || \
!defined(ISC_PLATFORM_NEEDVSNPRINTF)
#define ISC__PRINT_SOURCE
#endif
#include <isc/print.h>
@@ -48,6 +54,7 @@ ATF_TC_BODY(snprintf, tc) {
char buf[10000];
isc_uint64_t ll = 8589934592ULL;
int n;
size_t size;
UNUSED(tc);
@@ -65,6 +72,57 @@ ATF_TC_BODY(snprintf, tc) {
n = isc_print_snprintf(buf, sizeof(buf), "%llu", ll);
ATF_CHECK_EQ(n, 10);
ATF_CHECK_STREQ(buf, "8589934592");
size = 1000;
memset(buf, 0xff, sizeof(buf));
n = isc_print_snprintf(buf, sizeof(buf), "%zu", size);
ATF_CHECK_EQ(n, 4);
ATF_CHECK_STREQ(buf, "1000");
size = 1000;
memset(buf, 0xff, sizeof(buf));
n = isc_print_snprintf(buf, sizeof(buf), "%zx", size);
ATF_CHECK_EQ(n, 3);
ATF_CHECK_STREQ(buf, "3e8");
size = 1000;
memset(buf, 0xff, sizeof(buf));
n = isc_print_snprintf(buf, sizeof(buf), "%zo", size);
ATF_CHECK_EQ(n, 4);
ATF_CHECK_STREQ(buf, "1750");
}
ATF_TC(fprintf);
ATF_TC_HEAD(fprintf, tc) {
atf_tc_set_md_var(tc, "descr", "fprintf implementation");
}
ATF_TC_BODY(fprintf, tc) {
FILE *f;
int n;
size_t size;
char buf[10000];
UNUSED(tc);
f = fopen("fprintf.test", "w+");
ATF_REQUIRE(f != NULL);
size = 1000;
n = isc_print_fprintf(f, "%zu", size);
ATF_CHECK_EQ(n, 4);
rewind(f);
memset(buf, 0, sizeof(buf));
n = fread(buf, 1, sizeof(buf), f);
ATF_CHECK_EQ(n, 4);
fclose(f);
ATF_CHECK_STREQ(buf, "1000");
if ((n > 0) && (!strcmp(buf, "1000")))
unlink("fprintf.test");
}
/*
@@ -72,5 +130,6 @@ ATF_TC_BODY(snprintf, tc) {
*/
ATF_TP_ADD_TCS(tp) {
ATF_TP_ADD_TC(tp, snprintf);
ATF_TP_ADD_TC(tp, fprintf);
return (atf_no_error());
}