1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Fix handling of files without newlines in the head and tail builtins (#365)

The head and tail builtins don't correctly handle files that lack
newlines[*]:
    $ print -n foo > /tmp/bar
    $ /opt/ast/bin/head -1 /tmp/bar  # No output
    $ print -n 'foo\nbar' > /tmp/bar
    $ /opt/ast/bin/tail -1 /tmp/bar
    foo
    bar$
This commit backports the required changes from ksh93v- to handle files
without a newline in the head and tail builtins. (Also note that the
required fix to sfmove was already backported in commit 1bd06207.)

src/lib/libcmd/{head,tail}.c:
- Backport the relevant ksh93v- code for handling files
  without newlines.

src/cmd/ksh93/tests/builtins.sh:
- Add a few regression tests for using 'head -1' and 'tail -1' on a file
  missing and ending newline.

[*]: https://www.illumos.org/issues/4149
This commit is contained in:
Johnothan King 2021-12-07 22:56:52 -08:00 committed by Martijn Dekker
parent beccb93fd4
commit 2b8eaa6609
4 changed files with 52 additions and 12 deletions

View file

@ -1396,5 +1396,22 @@ got=$?; exp=1
got=$?; exp=2
(( got == exp )) || err_exit "cd -eP to empty string has wrong exit status (expected $exp, got $got)"
# ======
# The head and tail builtins should work on files without newlines
if builtin head 2> /dev/null; then
print -n nonewline > "$tmp/nonewline"
exp=nonewline
got=$(head -1 "$tmp/nonewline")
[[ $got == $exp ]] || err_exit "head builtin fails to correctly handle files without an ending newline" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
fi
if builtin tail 2> /dev/null; then
print -n 'newline\nnonewline' > "$tmp/nonewline"
exp=nonewline
got=$(tail -1 "$tmp/nonewline")
[[ $got == $exp ]] || err_exit "tail builtin fails to correctly handle files without an ending newline" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
fi
# ======
exit $((Errors<125?Errors:125))