From f0b4f9e5bb263ea63207d9355c87d02f9f04efed Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 2 Feb 2023 21:19:48 -0600 Subject: [PATCH] Make head lseek backwards to return unused data from seekable inputs. --- tests/head.test | 2 ++ toys/posix/head.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/head.test b/tests/head.test index 729dfa7e..e23b70d7 100755 --- a/tests/head.test +++ b/tests/head.test @@ -32,3 +32,5 @@ testcmd "-c 3" "-c 3" "one" "" "one\ntwo" testcmd "-c bigger than input" "-c 3" "a" "" "a" testcmd "-c 3 -n 1" "-c 3 -n 1" "one\n" "" "one\ntwo" testcmd "-n 1 -c 3" "-n 1 -c 3" "one" "" "one\ntwo" +testing "unget" 'while read i; do echo =$i; head -n 1; done < input' \ + '=one\ntwo\n=three\nfour\n=five\n' 'one\ntwo\nthree\nfour\nfive\n' '' diff --git a/toys/posix/head.c b/toys/posix/head.c index d5c0700c..04e1658c 100644 --- a/toys/posix/head.c +++ b/toys/posix/head.c @@ -34,7 +34,7 @@ GLOBALS( static void do_head(int fd, char *name) { - long i, len, lines=TT.n, bytes=TT.c; + long i = 0, len = 0, lines = TT.n, bytes = TT.c; if ((toys.optc > 1 && !FLAG(q)) || FLAG(v)) { // Print an extra newline for all but the first file @@ -50,11 +50,14 @@ static void do_head(int fd, char *name) if (bytes) { i = bytes >= len ? len : bytes; bytes -= i; - } else for(i=0; ii) lseek(fd, i-len, SEEK_CUR); + TT.file_no++; }