coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

head and tail header quoting


From: Collin Funk
Subject: head and tail header quoting
Date: Mon, 25 May 2026 19:31:58 -0700
User-agent: Gnus/5.13 (Gnus v5.13)

Bruno Haible via GNU coreutils General Discussion <coreutils@gnu.org>
writes:

> The CI today reports a test failure of tests/tail/pipe-f.
>
> Log is attached.
>
> It looks like a consequence of the
>   "head,tail: quote name in file headers appropriately"
> commit from today.

You are right. At first I thought the quoting like this was a bit
strange:

    $ ./src/tail - - < /dev/null
    ==> 'standard input' <==
    
    ==> 'standard input' <==

However, I guess I am okay with it given that is what is already done
for errors (*):

   $ ./src/tail <&-
   tail: cannot fstat 'standard input': Bad file descriptor
   tail: -: Bad file descriptor

My 0001 patch attached fixes that test case. I haven't pushed it yet in
case we decide that is not the desired behavior.

I noticed another issue with Pádraig's patch that I mentioned privately,
but I think is worth discussion publicly in case anyone wants to
comment. I believe that it does violate POSIX requirements. The page for
'head' says [1]:

    STDOUT
    
      The standard output shall contain designated portions of the input
      files.
    
      If multiple file operands are specified, head shall precede the
      output for each with the header:
    
      "\n==> %s <==\n", <pathname>
    
      except that the first header written shall not include the initial
      <newline>.

My interpretation of this is that "pathname" refers to an unaltered name
from the command line. Conversion between "-" and "standard input" seems
okay since it isn't really a path.

Given that the headers are mostly a visual aid, I am okay with the new
behavior. However, I propose following the existing behavior if
POSIXLY_CORRECT is defined. I have attached patch 0002 which does this.

Note that POSIX only defines the behavior of 'tail' on a single
file [2]. Given that our implementation behaves similar to 'head', I
think that it is reasonable to do the same thing there.

(*) Note that there is an unrelated issue where we attempt to close
standard input when we know it was closed, therefore the duplicate error
messages. I had a look at fixing that in 'cat' which is another simple
case that always calls fstat on standard input. However, I haven't
decided on cases like this:

    $ ./src/tail - - - <&- > /dev/null
    tail: cannot fstat 'standard input': Bad file descriptor
    tail: cannot fstat 'standard input': Bad file descriptor
    tail: cannot fstat 'standard input': Bad file descriptor
    tail: -: Bad file descriptor

My current opinion is that the behavior in that case should be something
like this:

    $ ./src/tail - - - <&- > /dev/null
    tail: cannot fstat 'standard input': Bad file descriptor
    tail: cannot fstat 'standard input': Bad file descriptor
    tail: cannot fstat 'standard input': Bad file descriptor

One might be tempted to skip reading standard input for the second and
third argument. However, it feels wrong to skip a file explicitly given
via the command-line.

Collin

[1] https://pubs.opengroup.org/onlinepubs/9799919799/utilities/head.html
[2] https://pubs.opengroup.org/onlinepubs/9799919799/utilities/tail.html

Attachment: 0001-tests-fail-fix-a-test-failure-caused-by-the-previous.patch
Description: Text Data

Attachment: 0002-head-tail-don-t-quote-file-names-in-headers-if-POSIX.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]