summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@cvs.openbsd.org>2014-10-08 08:31:54 +0000
committerIngo Schwarze <schwarze@cvs.openbsd.org>2014-10-08 08:31:54 +0000
commit11f76198643ded88095f949870000b6a790ba54e (patch)
treec62085e0a0f31778d9e7eaf95f0563c4a049a89e
parent5282fb0b7e6c6c48b22b50d6827788357c28c98e (diff)
Fix a 37 year old bug introduced by Bill Joy on August 24, 1977
that was already present in the 1BSD release on March 9, 1978 by merging Keith Bostic's 22 year old fix from 4.4BSD (not kidding). Original CSRG SCCS commit message: ^As 00009/00006/00145 ^Ad D 5.7 92/03/04 14:35:42 bostic 9 8 ^Ac can't use freopen; example is "date | head file1 /dev/stdin" ok deraadt@ tedu@, also checked by Martin <Natano dot net>
-rw-r--r--usr.bin/head/head.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/usr.bin/head/head.c b/usr.bin/head/head.c
index d48bd682a8c..3e94eb5761b 100644
--- a/usr.bin/head/head.c
+++ b/usr.bin/head/head.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: head.c,v 1.17 2014/10/07 19:38:57 tedu Exp $ */
+/* $OpenBSD: head.c,v 1.18 2014/10/08 08:31:53 schwarze Exp $ */
/*
* Copyright (c) 1980, 1987 Regents of the University of California.
@@ -48,6 +48,7 @@ static void usage(void);
int
main(int argc, char *argv[])
{
+ FILE *fp;
long cnt;
int ch, firsttime;
long linecnt = 10;
@@ -81,13 +82,13 @@ main(int argc, char *argv[])
errx(1, "line count %s: %s", errstr, p);
}
- /* setlinebuf(stdout); */
for (firsttime = 1; ; firsttime = 0) {
if (!*argv) {
if (!firsttime)
exit(status);
+ fp = stdin;
} else {
- if (!freopen(*argv, "r", stdin)) {
+ if ((fp = fopen(*argv, "r")) == NULL) {
warn("%s", *argv++);
status = 1;
continue;
@@ -99,10 +100,11 @@ main(int argc, char *argv[])
}
++argv;
}
- for (cnt = linecnt; cnt && !feof(stdin); --cnt)
- while ((ch = getchar()) != EOF)
+ for (cnt = linecnt; cnt && !feof(fp); --cnt)
+ while ((ch = getc(fp)) != EOF)
if (putchar(ch) == '\n')
break;
+ fclose(fp);
}
/*NOTREACHED*/
}