Hi,

The attached patch fixes a bug in /bin/cat when using -n with multiple input
files.  This bug seems to have been introduced in 4.3BSD-Reno.

Numbering currently starts over at 1 for each input file, here is a simple
reproducer:

$ echo "foo\nbar" > /tmp/1
$ echo "foobar\nbar\nbaz" > /tmp/2
$ cat -n /tmp/1 /tmp/2
     1  foo
     2  bar
     1  foobar
     2  bar
     3  baz

Historic BSD behaviour, (confirmed in at least 2.9-BSD, 4.1c-BSD, 4.2BSD,
and 4.3BSD), and current gnu coreutils behaviour numbers the lines of the
output as a single entity.  For example, with gnu cat:

$ gcat -n /tmp/1 /tmp/2
     1  foo
     2  bar
     3  foobar
     4  bar
     5  baz

Here is a proposed patch to fix it, by making 'line' a static:

--- cat_netbsd.c.dist   Wed Nov 15 07:43:09 2023
+++ cat_netbsd.c        Wed Nov 15 07:46:48 2023
@@ -170,9 +170,10 @@
 void
 cook_buf(FILE *fp)
 {
-       int ch, gobble, line, prev;
+       int ch, gobble, prev;
+       static int line;
 
-       line = gobble = 0;
+       gobble = 0;
        for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
                if (prev == '\n') {
                        if (sflag) {

Reply via email to