ref: 8e87dfb15e3d9f3c940b3de2378c0591e2e97a12
dir: /tarsum.fnl/
#!/usr/bin/env fennel ;; vim: set filetype=scheme : (fn usage [] (io.stderr:write "Usage: tarsum <filename>\n") (os.exit 1)) (if (< (# arg) 1) (usage)) ;; (local tarball (io.open (. arg 1) "r+b")) (with-open (tarball (io.open (. arg 1) "r+b")) (let [header (tarball:read 0x132)] (if (= header fail) (error "Unable to read file.")) (if (~= (# header) 0x132) (error "Invalid header! (Is this really a tarball?)")) (var headertbl {}) (for [i 1 (# header) 1] (table.insert headertbl (header:sub i i))) (for [i 0x94 0x9b 1] (tset headertbl i " ")) (var header "") (each [k v (ipairs headertbl)] (set header (.. header v))) (var sum 0) (for [i 1 (# header) 1] (set sum (+ sum (header:byte i)))) (print (string.format "Checksum: %.7o\n" sum)) (if (~= (tarball:seek "set" 0x94) 0x94) (error "Could not seek to 0x94")) (tarball:write (string.format "%.7o" sum)) (if (~= (tarball:seek "set" 0x9b) 0x9b) (error "Could not seek to 0x9b")) (tarball:write (string.format "%c" 0)) (tarball:flush) 0 ))