aboutsummaryrefslogtreecommitdiff
path: root/bin/cat
diff options
context:
space:
mode:
authorRuslan Ermilov <ru@FreeBSD.org>2001-09-26 11:32:23 +0000
committerRuslan Ermilov <ru@FreeBSD.org>2001-09-26 11:32:23 +0000
commit1b00c916a1fc52af2bb6ff93ded372fa853a76e5 (patch)
tree6006dc47eb408282a785825ca9bcbb7fd57cbb70 /bin/cat
parent3418ebebfe36e7bcaf380fd3a462b77b59adf1f9 (diff)
downloadsrc-1b00c916a1fc52af2bb6ff93ded372fa853a76e5.tar.gz
src-1b00c916a1fc52af2bb6ff93ded372fa853a76e5.zip
Notes
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 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') {