#!/usr/bin/env swift; /****************************************************************************** * This program reads bytes from a binary file and copies them to another file. * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ import Foundation import Utils func copyBinaryFile(_ fromFilespec:String, _ toFilespec:String) throws -> Void { let ifh:FileHandle? = FileHandle(forReadingAtPath: fromFilespec) FileManager.default.createFile(atPath: toFilespec, contents: nil) let ofh:FileHandle? = FileHandle(forWritingAtPath: toFilespec) if ifh == nil { throw RuntimeError("Problem opening input file!") } if ofh == nil { throw RuntimeError("Problem opening output file!") } var c:UInt8? c = Utils.getbyte(ifh!) while c != nil { ofh!.write(Data([UInt8(c!)])) c = Utils.getbyte(ifh!) } try ifh!.close() try ofh!.close() } // Begin Main if CommandLine.arguments.count != 3 { print("Syntax: " + CommandLine.arguments[0] + " {fromFilespec} {toFilespec}") exit(1) } var fromFilespec:String = CommandLine.arguments[1] var toFilespec:String = CommandLine.arguments[2] do { try copyBinaryFile(fromFilespec, toFilespec) } catch let ex as RuntimeError { print("Error: " + ex.localizedDescription) } exit(EXIT_SUCCESS)