Hi,
we've had a bug report from one of our customers recently, saying
pr -nNUM doesn't zero-pad line numbers on output with certain
values of NUM (for example 1-11, 13-14, 16-17...). The problem
seems to be the faulty format string that I fixed in the patch
attached. I've also added 2 test cases in tests/misc/pr.pl to
exercise the output with problematic as well as ok NUM values.
Cheers,
Ondrej
>From 78e32d37ba174a50dad86b5b6d557a76755b957d Mon Sep 17 00:00:00 2001
From: Ondrej Oprala <[email protected]>
Date: Wed, 24 Oct 2012 10:49:05 +0200
Subject: [PATCH] pr: fix omitted leading zeros with particular arguments to
-n option
* NEWS: Mention the fix.
* src/pr.c (add_line_number): Change sprintf format string.
* tests/misc/pr.pl: Add a test case to exercise the change.
---
NEWS | 5 +++++
src/pr.c | 2 +-
tests/misc/pr.pl | 6 +++++-
3 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/NEWS b/NEWS
index 04721da..6b1a99b 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,11 @@ GNU coreutils NEWS -*-
outline -*-
* Noteworthy changes in release ?.? (????-??-??) [?]
+** Bug fixes
+
+ pr now properly zero-pads line numbers with arbitrary integer
+ arguments to the -n option.
+
* Noteworthy changes in release 8.20 (2012-10-23) [stable]
diff --git a/src/pr.c b/src/pr.c
index e97c434..45e80e1 100644
--- a/src/pr.c
+++ b/src/pr.c
@@ -2034,7 +2034,7 @@ add_line_number (COLUMN *p)
/* Cutting off the higher-order digits is more informative than
lower-order cut off*/
if (line_number < power_10)
- sprintf (number_buff, "%*d", chars_per_number, line_number);
+ sprintf (number_buff, "%0*d", chars_per_number, line_number);
else
{
left_cut = line_number % power_10;
diff --git a/tests/misc/pr.pl b/tests/misc/pr.pl
index 0f25e25..383b4ef 100755
--- a/tests/misc/pr.pl
+++ b/tests/misc/pr.pl
@@ -1,5 +1,5 @@
#!/usr/bin/perl
-# Exercise a bug with pr -m -s
+# Exercise a bug with pr -m -s and -n
# Copyright (C) 2007-2012 Free Software Foundation, Inc.
@@ -33,6 +33,10 @@ my @Tests =
{IN=>{2=>"m\tn\to\n"}},
{IN=>{3=>"x\ty\tz\n"}},
{OUT=>join("\t", qw(a b c m n o x y z)) . "\n"} ],
+ ['zeros1', '-n10 -t',
+ {IN=>"abc\n"}, {OUT=>"0000000001 abc\n"}],
+ ['zeros2', '-n12 -t',
+ {IN=>"abc\n"}, {OUT=>"000000000001 abc\n"}],
);
my $save_temps = $ENV{DEBUG};
--
1.7.11.7