Don't use buffered IO (fread) when not appropriate

fread reads the entire requested size (BUFSIZ), which causes tools to
block if only small amounts of data are available at a time. At best,
this causes unnecessary copies and inefficiency, at worst, tools like
tee and cat are almost unusable in some cases since they only display
large chunks of data at a time.
This commit is contained in:
Michael Forney
2017-01-01 17:00:33 -08:00
committed by Anselm R Garbe
parent 5cb3a1eba1
commit 9a3b12525b
5 changed files with 83 additions and 69 deletions

39
tee.c
View File

@@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include "util.h"
@@ -13,14 +14,15 @@ usage(void)
int
main(int argc, char *argv[])
{
FILE **fps = NULL;
size_t i, n, nfps;
int ret = 0, aflag = 0, iflag = 0;
int *fds = NULL;
size_t i, nfds;
ssize_t n;
int ret = 0, aflag = O_TRUNC, iflag = 0;
char buf[BUFSIZ];
ARGBEGIN {
case 'a':
aflag = 1;
aflag = O_APPEND;
break;
case 'i':
iflag = 1;
@@ -31,31 +33,28 @@ main(int argc, char *argv[])
if (iflag && signal(SIGINT, SIG_IGN) == SIG_ERR)
eprintf("signal:");
nfps = argc + 1;
fps = ecalloc(nfps, sizeof(*fps));
nfds = argc + 1;
fds = ecalloc(nfds, sizeof(*fds));
for (i = 0; i < argc; i++) {
if (!(fps[i] = fopen(argv[i], aflag ? "a" : "w"))) {
weprintf("fopen %s:", argv[i]);
if ((fds[i] = open(argv[i], O_WRONLY|O_CREAT|aflag, 0666)) < 0) {
weprintf("open %s:", argv[i]);
ret = 1;
}
}
fps[i] = stdout;
fds[i] = 1;
while ((n = fread(buf, 1, sizeof(buf), stdin))) {
for (i = 0; i < nfps; i++) {
if (fps[i] && fwrite(buf, 1, n, fps[i]) != n) {
fshut(fps[i], (i != argc) ? argv[i] : "<stdout>");
fps[i] = NULL;
while ((n = read(0, buf, sizeof(buf))) > 0) {
for (i = 0; i < nfds; i++) {
if (fds[i] >= 0 && writeall(fds[i], buf, n) < 0) {
weprintf("write %s:", (i != argc) ? argv[i] : "<stdout>");
fds[i] = -1;
ret = 1;
}
}
}
ret |= fshut(stdin, "<stdin>");
for (i = 0; i < nfps; i++)
if (fps[i])
ret |= fshut(fps[i], (i != argc) ? argv[i] : "<stdout>");
if (n < 0)
eprintf("read <stdin>:");
return ret;
}