summaryrefslogtreecommitdiffstats
path: root/bin/cat
diff options
context:
space:
mode:
authorru <ru@FreeBSD.org>2001-09-26 11:32:23 +0000
committerru <ru@FreeBSD.org>2001-09-26 11:32:23 +0000
commite43023b2d0bbef29cace4600bceace83284edeaf (patch)
tree6006dc47eb408282a785825ca9bcbb7fd57cbb70 /bin/cat
parent9ab85c59297c02e5c30289d283cdd33f73017e7a (diff)
downloadFreeBSD-src-e43023b2d0bbef29cace4600bceace83284edeaf.zip
FreeBSD-src-e43023b2d0bbef29cace4600bceace83284edeaf.tar.gz
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.
Diffstat (limited to 'bin/cat')
-rw-r--r--bin/cat/cat.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/bin/cat/cat.c b/bin/cat/cat.c
index 2bfac6c..e7239c4 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') {
OpenPOWER on IntegriCloud