summaryrefslogtreecommitdiff
path: root/source4/lib/replace
diff options
context:
space:
mode:
authorSimo Sorce <idra@samba.org>2006-09-04 16:30:40 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 14:17:08 -0500
commit62e60274131ae4968560bbd58fb847cb9f9e8f9f (patch)
treec95e21b6966dc42f64cc1e624ddb4a4529796f67 /source4/lib/replace
parentc13ea9b3a6601406d29c5970734891fbc7eccaac (diff)
downloadsamba-62e60274131ae4968560bbd58fb847cb9f9e8f9f.tar.gz
samba-62e60274131ae4968560bbd58fb847cb9f9e8f9f.tar.bz2
samba-62e60274131ae4968560bbd58fb847cb9f9e8f9f.zip
r18046: Add 'z' specifier support and a configure test.
Jeremy should I backport this to samba3 too? (This used to be commit dc689b5579987da8ee5397b9758a42b01e44fb73)
Diffstat (limited to 'source4/lib/replace')
-rw-r--r--source4/lib/replace/config.m412
-rw-r--r--source4/lib/replace/snprintf.c55
2 files changed, 51 insertions, 16 deletions
diff --git a/source4/lib/replace/config.m4 b/source4/lib/replace/config.m4
index c2e0e5e6f4..8bfd836189 100644
--- a/source4/lib/replace/config.m4
+++ b/source4/lib/replace/config.m4
@@ -91,7 +91,9 @@ AC_CHECK_HEADERS(strings.h)
AC_CACHE_CHECK([for C99 vsnprintf],samba_cv_HAVE_C99_VSNPRINTF,[
AC_TRY_RUN([
#include <sys/types.h>
+#include <stdio.h>
#include <stdarg.h>
+#include <stdlib.h>
void foo(const char *format, ...) {
va_list ap;
int len;
@@ -107,12 +109,14 @@ void foo(const char *format, ...) {
va_start(ap, format);
len = vsnprintf(0, 0, format, ap);
va_end(ap);
- if (len != 5) exit(1);
+ if (len != 5) exit(2);
- if (snprintf(buf, 3, "hello") != 5 || strcmp(buf, "he") != 0) exit(1);
+ if (snprintf(buf, 3, "hello") != 5 || strcmp(buf, "he") != 0) exit(3);
- if (snprintf(buf, 20, "%lld", l) != 12 || strcmp(buf, "123456789000") != 0) exit(1);
- if (snprintf(buf, 20, "%s", 0) < 3) exit(1);
+ if (snprintf(buf, 20, "%lld", l) != 12 || strcmp(buf, "123456789000") != 0) exit(4);
+ if (snprintf(buf, 20, "%zu", 123456789) != 9 || strcmp(buf, "123456789") != 0) exit(5);
+ if (snprintf(buf, 20, "%2\$d %1\$d", 3, 4) != 3 || strcmp(buf, "4 3") != 0) exit(6);
+ if (snprintf(buf, 20, "%s", 0) < 3) exit(7);
exit(0);
}
diff --git a/source4/lib/replace/snprintf.c b/source4/lib/replace/snprintf.c
index 8182f83751..30c2b0a1b7 100644
--- a/source4/lib/replace/snprintf.c
+++ b/source4/lib/replace/snprintf.c
@@ -200,6 +200,7 @@
#define DP_C_LONG 3
#define DP_C_LDOUBLE 4
#define DP_C_LLONG 5
+#define DP_C_SIZET 6
/* Chunk types */
#define CNK_FMT_STR 0
@@ -467,6 +468,10 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
cnk->cflags = DP_C_LDOUBLE;
ch = *format++;
break;
+ case 'z':
+ cnk->cflags = DP_C_SIZET;
+ ch = *format++;
+ break;
default:
break;
}
@@ -575,6 +580,8 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
cnk->value = va_arg (args, long int);
else if (cnk->cflags == DP_C_LLONG)
cnk->value = va_arg (args, LLONG);
+ else if (cnk->cflags == DP_C_SIZET)
+ cnk->value = va_arg (args, ssize_t);
else
cnk->value = va_arg (args, int);
@@ -592,6 +599,8 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
cnk->value = (unsigned long int)va_arg (args, unsigned long int);
else if (cnk->cflags == DP_C_LLONG)
cnk->value = (LLONG)va_arg (args, unsigned LLONG);
+ else if (cnk->cflags == DP_C_SIZET)
+ cnk->value = (size_t)va_arg (args, size_t);
else
cnk->value = (unsigned int)va_arg (args, unsigned int);
@@ -644,6 +653,8 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
cnk->pnum = va_arg (args, long int *);
else if (cnk->cflags == DP_C_LLONG)
cnk->pnum = va_arg (args, LLONG *);
+ else if (cnk->cflags == DP_C_SIZET)
+ cnk->pnum = va_arg (args, ssize_t *);
else
cnk->pnum = va_arg (args, int *);
@@ -725,6 +736,8 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
*((long int *)(cnk->pnum)) = (long int)currlen;
else if (cnk->cflags == DP_C_LLONG)
*((LLONG *)(cnk->pnum)) = (LLONG)currlen;
+ else if (cnk->cflags == DP_C_SIZET)
+ *((ssize_t *)(cnk->pnum)) = (ssize_t)currlen;
else
*((int *)(cnk->pnum)) = (int)currlen;
break;
@@ -1258,6 +1271,7 @@ static int add_cnk_list_entry(struct pr_chunk_x **list,
#ifdef TEST_SNPRINTF
int sprintf(char *str,const char *fmt,...);
+ int printf(const char *fmt,...);
int main (void)
{
@@ -1327,15 +1341,20 @@ static int add_cnk_list_entry(struct pr_chunk_x **list,
int fail = 0;
int num = 0;
int l1, l2;
+ char *ss_fmt[] = {
+ "%zd",
+ "%zu",
+ NULL
+ };
+ size_t ss_nums[] = {134, 91340, 123456789, 0203, 1234567890, 0};
printf ("Testing snprintf format codes against system sprintf...\n");
for (x = 0; fp_fmt[x] ; x++) {
for (y = 0; fp_nums[y] != 0 ; y++) {
buf1[0] = buf2[0] = '\0';
- l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
- l2 = sprintf(buf1, fp_fmt[x], fp_nums[y]);
- sprintf (buf2, fp_fmt[x], fp_nums[y]);
+ l1 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
+ l2 = sprintf (buf2, fp_fmt[x], fp_nums[y]);
buf1[1023] = buf2[1023] = '\0';
if (strcmp (buf1, buf2) || (l1 != l2)) {
printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
@@ -1349,9 +1368,8 @@ static int add_cnk_list_entry(struct pr_chunk_x **list,
for (x = 0; int_fmt[x] ; x++) {
for (y = 0; int_nums[y] != 0 ; y++) {
buf1[0] = buf2[0] = '\0';
- l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
- l2 = sprintf(buf1, int_fmt[x], int_nums[y]);
- sprintf (buf2, int_fmt[x], int_nums[y]);
+ l1 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
+ l2 = sprintf (buf2, int_fmt[x], int_nums[y]);
buf1[1023] = buf2[1023] = '\0';
if (strcmp (buf1, buf2) || (l1 != l2)) {
printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
@@ -1365,9 +1383,8 @@ static int add_cnk_list_entry(struct pr_chunk_x **list,
for (x = 0; str_fmt[x] ; x++) {
for (y = 0; str_vals[y] != 0 ; y++) {
buf1[0] = buf2[0] = '\0';
- l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
- l2 = sprintf(buf1, str_fmt[x], str_vals[y]);
- sprintf (buf2, str_fmt[x], str_vals[y]);
+ l1 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
+ l2 = sprintf (buf2, str_fmt[x], str_vals[y]);
buf1[1023] = buf2[1023] = '\0';
if (strcmp (buf1, buf2) || (l1 != l2)) {
printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
@@ -1382,9 +1399,8 @@ static int add_cnk_list_entry(struct pr_chunk_x **list,
for (x = 0; ll_fmt[x] ; x++) {
for (y = 0; ll_nums[y] != 0 ; y++) {
buf1[0] = buf2[0] = '\0';
- l1 = snprintf(NULL, 0, ll_fmt[x], ll_nums[y]);
- l2 = sprintf(buf1, ll_fmt[x], ll_nums[y]);
- sprintf (buf2, ll_fmt[x], ll_nums[y]);
+ l1 = snprintf(buf1, sizeof(buf1), ll_fmt[x], ll_nums[y]);
+ l2 = sprintf (buf2, ll_fmt[x], ll_nums[y]);
buf1[1023] = buf2[1023] = '\0';
if (strcmp (buf1, buf2) || (l1 != l2)) {
printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
@@ -1431,6 +1447,21 @@ static int add_cnk_list_entry(struct pr_chunk_x **list,
"%4$*1$d %2$s %3$*1$.*1$f", l1, buf1, l2, buf2);
fail++;
}
+
+ for (x = 0; ss_fmt[x] ; x++) {
+ for (y = 0; ss_nums[y] != 0 ; y++) {
+ buf1[0] = buf2[0] = '\0';
+ l1 = snprintf(buf1, sizeof(buf1), ss_fmt[x], ss_nums[y]);
+ l2 = sprintf (buf2, ss_fmt[x], ss_nums[y]);
+ buf1[1023] = buf2[1023] = '\0';
+ if (strcmp (buf1, buf2) || (l1 != l2)) {
+ printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
+ ss_fmt[x], l1, buf1, l2, buf2);
+ fail++;
+ }
+ num++;
+ }
+ }
#if 0
buf1[0] = buf2[0] = '\0';
l1 = snprintf(buf1, sizeof(buf1), "%lld", (LLONG)1234567890);