Prev Up Next

Let's now tackle a more substantial problem. We need to transfer files from one computer to another and the only method we have is to use a 3.5'' floppy as a ferry. We need a script split4floppy that will split files larger than 1.44 million bytes into floppy-sized chunks. The script file split4floppy is as follows:

":";exec mzscheme -r $0 "$@"

;floppy-size = number of bytes that will comfortably fit on a
;              3.5" floppy

(define floppy-size 1440000)

;split splits the bigfile f into the smaller, floppy-sized
;subfiles, viz, subfile-prefix.1, subfile-prefix.2, etc.

(define split
  (lambda (f subfile-prefix)
    (call-with-input-file f
      (lambda (i)
        (let loop ((n 1))
          (if (copy-floppy-size-chunk i subfile-prefix n)
              (loop (+ n 1))))))))

;copy-to-floppy-sized-subfile copies the next 1.44 million
;bytes (if there are less than that many bytes left, it
;copies all of them) from the big file to the nth
;subfile.  Returns true if there are bytes left over,
;otherwise returns false.

(define copy-to-floppy-sized-subfile
  (lambda (i subfile-prefix n)
    (let ((nth-subfile (string-append subfile-prefix "."
                                      (number->string n))))
      (if (file-exists? nth-subfile) (delete-file nth-subfile))
      (call-with-output-file nth-subfile
        (lambda (o)
          (let loop ((k 1))
            (let ((c (read-char i)))
              (cond ((eof-object? c) #f)
                    (else
                     (write-char c o)
                     (if (< k floppy-size)
                         (loop (+ k 1))
                         #t))))))))))

;bigfile = script's first arg
;        = the file that needs splitting

(define bigfile (vector-ref argv 0))

;subfile-prefix = script's second arg
;               = the basename of the subfiles

(define subfile-prefix (vector-ref argv 1))

;Call split, making subfile-prefix.{1,2,3,...} from
;bigfile

(split bigfile subfile-prefix)

Script split4floppy is called as follows:

split4floppy largefile chunk

This splits largefile into subfiles chunk.1, chunk.2, ..., such that each subfile fits on a floppy.

After the chunk.i have been ferried over to the target computer, the file largefile can be retrieved by stringing the chunk.i together. This can be done on Unix with:

cat chunk.1 chunk.2 ... > largefile

and on DOS with:

copy /b chunk.1+chunk.2+... largefile

Prev Up Next

Log in or register to write something here or to contact authors.