summaryrefslogtreecommitdiff
path: root/src/tac.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1994-01-11 05:52:05 +0000
committerJim Meyering <jim@meyering.net>1994-01-11 05:52:05 +0000
commita63a148116d71ba64dc34f73c1826bb1073fd495 (patch)
treea538bd9d0d9318374da8983fb1655bdf3eda6673 /src/tac.c
parentcd7ed8b3833f7fa7756b53f0cfb2697bfdf86ee2 (diff)
downloadcoreutils-a63a148116d71ba64dc34f73c1826bb1073fd495.tar.xz
Revert last change -- timing showed 10% speedup w/o stream I/O.
Diffstat (limited to 'src/tac.c')
-rw-r--r--src/tac.c49
1 files changed, 42 insertions, 7 deletions
diff --git a/src/tac.c b/src/tac.c
index 38008acd8..c295de444 100644
--- a/src/tac.c
+++ b/src/tac.c
@@ -75,6 +75,7 @@ static char *xmalloc ();
static char *xrealloc ();
static void output ();
static void save_stdin ();
+static void xwrite ();
void error ();
int full_write ();
@@ -250,12 +251,13 @@ main (argc, argv)
errors |= tac_file (argv[optind]);
}
- if (ferror (stdout) || fclose (stdout) == EOF)
- error (1, errno, "write error");
+ /* Flush the output buffer. */
+ output ((char *) NULL, (char *) NULL);
if (have_read_stdin && close (0) < 0)
error (1, errno, "-");
-
+ if (close (1) < 0)
+ error (1, errno, "write error");
exit (errors);
}
@@ -600,11 +602,31 @@ output (start, past_end)
char *start;
char *past_end;
{
- int n_bytes;
+ static char buffer[WRITESIZE];
+ static int bytes_in_buffer = 0;
+ int bytes_to_add = past_end - start;
+ int bytes_available = WRITESIZE - bytes_in_buffer;
+
+ if (start == 0)
+ {
+ xwrite (1, buffer, bytes_in_buffer);
+ bytes_in_buffer = 0;
+ return;
+ }
+
+ /* Write out as many full buffers as possible. */
+ while (bytes_to_add >= bytes_available)
+ {
+ bcopy (start, buffer + bytes_in_buffer, bytes_available);
+ bytes_to_add -= bytes_available;
+ start += bytes_available;
+ xwrite (1, buffer, WRITESIZE);
+ bytes_in_buffer = 0;
+ bytes_available = WRITESIZE;
+ }
- n_bytes = past_end - start;
-
- fwrite (start, 1, n_bytes, stdout);
+ bcopy (start, buffer + bytes_in_buffer, bytes_to_add);
+ bytes_in_buffer += bytes_to_add;
}
static RETSIGTYPE
@@ -614,6 +636,19 @@ cleanup ()
exit (1);
}
+static void
+xwrite (desc, buffer, size)
+ int desc;
+ char *buffer;
+ int size;
+{
+ if (full_write (desc, buffer, size) < 0)
+ {
+ error (0, errno, "write error");
+ cleanup ();
+ }
+}
+
/* Allocate N bytes of memory dynamically, with error checking. */
static char *