summaryrefslogtreecommitdiff
path: root/ipl/progs/rcat.icn
diff options
context:
space:
mode:
Diffstat (limited to 'ipl/progs/rcat.icn')
-rw-r--r--ipl/progs/rcat.icn54
1 files changed, 54 insertions, 0 deletions
diff --git a/ipl/progs/rcat.icn b/ipl/progs/rcat.icn
new file mode 100644
index 0000000..a655695
--- /dev/null
+++ b/ipl/progs/rcat.icn
@@ -0,0 +1,54 @@
+############################################################################
+#
+# File: rcat.icn
+#
+# Subject: Program to output a file from back to front
+#
+# Author: Gregg M. Townsend
+#
+# Date: March 7, 2000
+#
+############################################################################
+#
+# This file is in the public domain.
+#
+############################################################################
+#
+# This program outputs in reverse order the lines of one or more files.
+# Unlike some versions of "tail -r", the input file does not need to
+# fit in memory; but it must be seekable.
+#
+# usage: rcat file...
+#
+############################################################################
+
+$define BUFSIZE 65536
+
+procedure main(args)
+ local f, fname, len, i, nseg, buf, leftover, lines
+
+ if *args = 0 then
+ stop("usage: ", &progname, " file...")
+
+ every fname := !args do {
+
+ lines := []
+ leftover := ""
+ f := open(fname, "u") | stop("cannot open ", fname)
+ len := where(seek(f, 0)) - 1 | stop("cannot seek ", fname)
+ nseg := (len + BUFSIZE - 1) / BUFSIZE
+
+ every i := nseg - 1 to 0 by -1 do {
+ seek(f, 1 + BUFSIZE * i)
+ (reads(f, BUFSIZE) || leftover) ? {
+ leftover := tab(upto('\n') + 1 | 0)
+ while push(lines, tab(upto('\n') + 1))
+ if not pos(0) then
+ push(lines, tab(0))
+ }
+ while writes(get(lines))
+ }
+
+ writes(leftover)
+ }
+end