In order to perform faster when doing "head -n", use a pair of

fgetln/fwrite instead of getc/putchar, this seems about five times
faster.
This commit is contained in:
Alfred Perlstein 2001-11-02 09:27:16 +00:00
parent bfd1f63d44
commit c7a2aa5dfc
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=85861

View File

@ -130,14 +130,15 @@ head(fp, cnt)
FILE *fp;
register int cnt;
{
register int ch;
char *cp;
int error, readlen;
while (cnt && (ch = getc(fp)) != EOF) {
if (putchar(ch) == EOF)
err(1, "stdout");
if (ch == '\n')
cnt--;
}
while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
error = fwrite(cp, sizeof(char), readlen, stdout);
if (error != readlen)
err(1, "stdout");
cnt--;
}
}
void