From 1b00c916a1fc52af2bb6ff93ded372fa853a76e5 Mon Sep 17 00:00:00 2001 From: Ruslan Ermilov Date: Wed, 26 Sep 2001 11:32:23 +0000 Subject: [PATCH] The "cat - -" feature was broken by the last commit. Restore the code that avoided closing and reopening stdin. This is also required by POSIX. As a bonus, enable multiple stdin reads with the -benstv flags, by resetting the EOF condition on stdin. --- bin/cat/cat.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/bin/cat/cat.c b/bin/cat/cat.c index 2bfac6c7b9ec..e7239c492cff 100644 --- a/bin/cat/cat.c +++ b/bin/cat/cat.c @@ -135,13 +135,14 @@ scanfiles(argv, cooked) { int i = 0; char *path; + FILE *fp; while ((path = argv[i]) != NULL || i == 0) { int fd; if (path == NULL || strcmp(path, "-") == 0) { filename = "stdin"; - fd = 0; + fd = STDIN_FILENO; } else { filename = path; fd = open(path, O_RDONLY); @@ -154,12 +155,17 @@ scanfiles(argv, cooked) warn("%s", path); rval = 1; } else if (cooked) { - FILE *fp = fdopen(fd, "r"); - cook_cat(fp); - fclose(fp); + if (fd == STDIN_FILENO) + cook_cat(stdin); + else { + fp = fdopen(fd, "r"); + cook_cat(fp); + fclose(fp); + } } else { raw_cat(fd); - close(fd); + if (fd != STDIN_FILENO) + close(fd); } if (path == NULL) break; @@ -173,6 +179,10 @@ cook_cat(fp) { register int ch, gobble, line, prev; + /* Reset EOF condition on stdin. */ + if (fp == stdin && feof(stdin)) + clearerr(stdin); + line = gobble = 0; for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) { if (prev == '\n') {