Skip to content

Commit dc1cc6a

Browse files
[lit] Make builtin cat work with stdin
cat with no files passed to it is supposed to read from STDIN according to POSIX. The builtin cat lacking this behavior led to the clang test in dev-fd-fs.c to fail because it expected this behavior. This is a simple modification and I do not think it is possible to rewrite the test without this easily while preserving the semantics around named pipes. Reviewers: petrhosek, arichardson, ilovepi, cmtice, jh7370 Reviewed By: jh7370, arichardson, ilovepi, cmtice Pull Request: llvm/llvm-project#158447
1 parent 04b1e87 commit dc1cc6a

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

lit/builtin_commands/cat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def main(argv):
4949
import os, msvcrt
5050

5151
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
52+
if len(filenames) == 0:
53+
sys.stdout.write(sys.stdin.read())
54+
sys.exit(0)
5255
for filename in filenames:
5356
try:
5457
contents = None

tests/Inputs/shtest-cat/cat.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@
7070
# NP-CAT-OUTPUT-NEXT:M-HM-IM-JM-KM-LM-MM-NM-OM-PM-QM-RM-SM-TM-UM-VM-WM-XM-YM-ZM-[
7171
# NP-CAT-OUTPUT-NEXT:M-\M-]M-^M-_M-`M-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-kM-lM-mM-nM-o
7272
# NP-CAT-OUTPUT-NEXT:M-pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?
73+
74+
## Test that cat will pipe stdin to stdout if no other files are specified.
75+
# RUN: echo test | cat | FileCheck --check-prefix=CAT-STDIN %s
76+
# CAT-STDIN: test

0 commit comments

Comments
 (0)