home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / rcat.icn < prev    next >
Text File  |  2000-07-29  |  1KB  |  55 lines

  1. ############################################################################
  2. #
  3. #    File:     rcat.icn
  4. #
  5. #    Subject:  Program to output a file from back to front
  6. #
  7. #    Author:   Gregg M. Townsend
  8. #
  9. #    Date:     March 7, 2000
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #   This program outputs in reverse order the lines of one or more files.
  18. #   Unlike some versions of "tail -r", the input file does not need to
  19. #   fit in memory; but it must be seekable.
  20. #
  21. #   usage:  rcat file...
  22. #
  23. ############################################################################
  24.  
  25. $define BUFSIZE 65536
  26.  
  27. procedure main(args)
  28.    local f, fname, len, i, nseg, buf, leftover, lines
  29.  
  30.    if *args = 0 then
  31.       stop("usage: ", &progname, " file...")
  32.  
  33.    every fname := !args do {
  34.  
  35.       lines := []
  36.       leftover := ""
  37.       f := open(fname, "u") | stop("cannot open ", fname)
  38.       len := where(seek(f, 0)) - 1 | stop("cannot seek ", fname)
  39.       nseg := (len + BUFSIZE - 1) / BUFSIZE
  40.  
  41.       every i := nseg - 1 to 0 by -1 do {
  42.          seek(f, 1 + BUFSIZE * i)
  43.          (reads(f, BUFSIZE) || leftover) ? {
  44.             leftover := tab(upto('\n') + 1 | 0)
  45.             while push(lines, tab(upto('\n') + 1))
  46.             if not pos(0) then
  47.                push(lines, tab(0))
  48.             }
  49.          while writes(get(lines))
  50.          }
  51.  
  52.       writes(leftover)
  53.       }
  54. end
  55.