logo

utils-std

Collection of commonly available Unix tools
commit: b8680d26cb0c44f4bc0155563dfa99d2d663a85e
parent d176c8781a093f5e300689dbe75e7fa4172584bd
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Wed, 29 May 2024 09:31:03 +0200

cmd/head: Fix infinite loop when /dev/null is passed

Diffstat:

Mcmd/head.c2++
Mtest-cmd/head.t10++++++++++
2 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/cmd/head.c b/cmd/head.c @@ -70,6 +70,8 @@ copy_bytes(char *filename) break; } + if(nread == 0) break; + if(write(STDOUT_FILENO, buf, nread) < 0) { fprintf( diff --git a/test-cmd/head.t b/test-cmd/head.t @@ -51,6 +51,16 @@ 0 [1] + $ head -c 3 </dev/null | wc -c + 0 + $ head -c 3 /dev/null | wc -c + 0 + + $ head -n 3 </dev/null | wc -l + 0 + $ head -n 3 /dev/null | wc -l + 0 + $ seq 1 2 | head - /dev/null ==> - <== 1