Use unbuffered I/O when writing to stdout and make sure we write

the entire buffer.
This commit is contained in:
Todd C. Miller
2009-09-19 00:21:27 +00:00
parent 6744c573b9
commit 7de6bbcfd5

View File

@@ -187,6 +187,7 @@ main(argc, argv)
double seconds; double seconds;
unsigned long nbytes; unsigned long nbytes;
size_t len, nread; size_t len, nread;
ssize_t nwritten;
double speed = 1.0; double speed = 1.0;
double max_wait = 0; double max_wait = 0;
double to_wait; double to_wait;
@@ -271,6 +272,7 @@ main(argc, argv)
/* /*
* Timing file consists of line of the format: "%f %d\n" * Timing file consists of line of the format: "%f %d\n"
*/ */
fflush(stdout);
while (fgets(buf, sizeof(buf), tfile) != NULL) { while (fgets(buf, sizeof(buf), tfile) != NULL) {
errno = 0; errno = 0;
seconds = strtod(buf, &ep); seconds = strtod(buf, &ep);
@@ -289,16 +291,20 @@ main(argc, argv)
to_wait = max_wait; to_wait = max_wait;
delay(to_wait); delay(to_wait);
fflush(stdout);
while (nbytes != 0) { while (nbytes != 0) {
if (nbytes > sizeof(buf)) if (nbytes > sizeof(buf))
len = sizeof(buf); len = sizeof(buf);
else else
len = nbytes; len = nbytes;
/* XXX - read/write all of len */
nread = fread(buf, 1, len, sfile); nread = fread(buf, 1, len, sfile);
fwrite(buf, nread, 1, stdout);
nbytes -= nread; nbytes -= nread;
do {
/* no stdio, must be unbuffered */
nwritten = write(STDOUT_FILENO, buf, nread);
if (nwritten == -1)
error(1, "writing to standard output");
nread -= nwritten;
} while (nread);
} }
} }
exit(0); exit(0);