builtins to allow stdin to be closed

Prior to this fix, if stdin were explicitly closed, then builtins would
silently fail. For example:

    count <&-

would just fail with status 1. Remove this limitation and allow each
builtin to handle a closed stdin how it sees fit.
This commit is contained in:
ridiculousfish
2021-02-10 17:19:08 -08:00
parent f239329f33
commit 84d59accfc
9 changed files with 44 additions and 8 deletions

View File

@@ -236,11 +236,17 @@ static maybe_t<int> builtin_count(parser_t &parser, io_streams_t &streams, wchar
// Count the newlines coming in via stdin like `wc -l`.
if (streams.stdin_is_directly_redirected) {
assert(streams.stdin_fd >= 0 &&
"Should have a valid fd since stdin is directly redirected");
char buf[COUNT_CHUNK_SIZE];
while (true) {
long n = read_blocked(streams.stdin_fd, buf, COUNT_CHUNK_SIZE);
// Ignore all errors for now.
if (n <= 0) break;
if (n == 0) {
break;
} else if (n < 0) {
wperror(L"read");
return STATUS_CMD_ERROR;
}
for (int i = 0; i < n; i++) {
if (buf[i] == L'\n') {
argc++;