about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog9
-rw-r--r--stdio-common/Makefile2
-rw-r--r--stdio-common/tst-fphex.c51
-rw-r--r--sysdeps/generic/printf_fphex.c23
4 files changed, 71 insertions, 14 deletions
diff --git a/ChangeLog b/ChangeLog
index 6bac4d5f02..2308121a72 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2002-10-23  Roland McGrath  <roland@redhat.com>
+
+	* stdio-common/tst-fphex.c: New file.
+	* stdio-common/Makefile (tests): Add tst-fphex.
+	* sysdeps/generic/printf_fphex.c (__printf_fphex): Fix initialization
+	of WNUMEND.  Fix counting of decimal point in WIDTH.  Print '0' pad
+	chars always before the value digits.
+	Reported by James Antill <james.antill@redhat.com>.
+
 2002-10-24  Jakub Jelinek  <jakub@redhat.com>
 
 	* posix/regcomp.c (re_comp): Call __regfree on re_comp_buf.
diff --git a/stdio-common/Makefile b/stdio-common/Makefile
index c32a2db428..802e4e37e8 100644
--- a/stdio-common/Makefile
+++ b/stdio-common/Makefile
@@ -54,7 +54,7 @@ tests := tstscanf test_rdwr test-popen tstgetln test-fseek \
 	 scanf1 scanf2 scanf3 scanf4 scanf5 scanf7 scanf8 scanf9 scanf10 \
 	 scanf11 scanf12 tst-tmpnam tst-cookie tst-obprintf tst-sscanf \
 	 tst-swprintf tst-fseek tst-fmemopen test-vfprintf tst-gets \
-	 tst-perror tst-sprintf tst-rndseek tst-fdopen
+	 tst-perror tst-sprintf tst-rndseek tst-fdopen tst-fphex
 
 test-srcs = tst-unbputc tst-printf
 
diff --git a/stdio-common/tst-fphex.c b/stdio-common/tst-fphex.c
new file mode 100644
index 0000000000..aca2c0500d
--- /dev/null
+++ b/stdio-common/tst-fphex.c
@@ -0,0 +1,51 @@
+/* Test program for %a printf formats.  */
+
+#include <stdio.h>
+#include <string.h>
+
+struct testcase
+{
+  double value;
+  const char *fmt;
+  const char *expect;
+};
+
+static const struct testcase testcases[] =
+  {
+    { 0x0.0030p+0, "%a",	"0x1.8p-11" },
+    { 0x0.0040p+0, "%a",	"0x1p-10" },
+    { 0x0.0030p+0, "%040a",	"0x00000000000000000000000000000001.8p-11" },
+    { 0x0.0040p+0, "%040a",	"0x0000000000000000000000000000000001p-10" },
+    { 0x0.0040p+0, "%40a",	"                                 0x1p-10" },
+    { 0x0.0040p+0, "%#40a",	"                                0x1.p-10" },
+    { 0x0.0040p+0, "%-40a",	"0x1p-10                                 " },
+    { 0x0.0040p+0, "%#-40a",	"0x1.p-10                                " },
+    { 0x0.0030p+0, "%040e",	"00000000000000000000000000007.324219e-04" },
+    { 0x0.0040p+0, "%040e",	"00000000000000000000000000009.765625e-04" },
+  };
+
+
+static int
+do_test (int argc, char **argv)
+{
+  const struct testcase *t;
+  int result = 0;
+
+  for (t = testcases;
+       t < &testcases[sizeof testcases / sizeof testcases[0]];
+       ++t)
+    {
+      char buf[1024];
+      int n = snprintf (buf, sizeof buf, t->fmt, t->value);
+      if (n != strlen (t->expect) || strcmp (buf, t->expect) != 0)
+	{
+	  printf ("%s\tExpected \"%s\" (%u)\n\tGot      \"%s\" (%d, %u)\n",
+		  t->fmt, t->expect, strlen (t->expect), buf, n, strlen (buf));
+	  result = 1;
+	}
+    }
+
+  return result;
+}
+
+#include "../test-skeleton.c"
diff --git a/sysdeps/generic/printf_fphex.c b/sysdeps/generic/printf_fphex.c
index 7dfb116aee..5b18c5ebb0 100644
--- a/sysdeps/generic/printf_fphex.c
+++ b/sysdeps/generic/printf_fphex.c
@@ -338,8 +338,8 @@ __printf_fphex (FILE *fp,
   /* Look for trailing zeroes.  */
   if (! zero_mantissa)
     {
-      wnumend = wnumbuf + sizeof wnumbuf;
-      numend = numbuf + sizeof numbuf;
+      wnumend = &wnumbuf[sizeof wnumbuf / sizeof wnumbuf[0]];
+      numend = &numbuf[sizeof numbuf / sizeof numbuf[0]];
       while (wnumend[-1] == L'0')
 	{
 	  --wnumend;
@@ -433,17 +433,13 @@ __printf_fphex (FILE *fp,
 	    + ((expbuf + sizeof expbuf) - expstr));
 	    /* Exponent.  */
 
-  /* Count the decimal point.  */
+  /* Count the decimal point.
+     A special case when the mantissa or the precision is zero and the `#'
+     is not given.  In this case we must not print the decimal point.  */
   if (precision > 0 || info->alt)
     width -= wide ? 1 : strlen (decimal);
 
-  /* A special case when the mantissa or the precision is zero and the `#'
-     is not given.  In this case we must not print the decimal point.  */
-  if (precision == 0 && !info->alt)
-    ++width;		/* This nihilates the +1 for the decimal-point
-			   character in the following equation.  */
-
-  if (!info->left && width > 0)
+  if (!info->left && info->pad != '0' && width > 0)
     PADN (' ', width);
 
   if (negative)
@@ -458,6 +454,10 @@ __printf_fphex (FILE *fp,
     outchar (info->spec + ('x' - 'a'));
   else
     outchar (info->spec == 'A' ? 'X' : 'x');
+
+  if (!info->left && info->pad == '0' && width > 0)
+    PADN ('0', width);
+
   outchar (leading);
 
   if (precision > 0 || info->alt)
@@ -474,9 +474,6 @@ __printf_fphex (FILE *fp,
 	PADN ('0', tofill);
     }
 
-  if (info->left && info->pad == '0' && width > 0)
-    PADN ('0', width);
-
   if ('P' - 'A' == 'p' - 'a')
     outchar (info->spec + ('p' - 'a'));
   else