Created
April 8, 2019 10:24
-
-
Save ljmccarthy/0cffd0ee9a06926e09d206ecef46bd1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const File = std.os.File; | |
fn copyFile(src: File, dst: File) !void { | |
var buf = []u8 {0} ** 1024; | |
while (true) { | |
const n = try src.read(&buf); | |
if (n == 0) break; | |
try dst.write(buf[0..n]); | |
} | |
} | |
pub fn main() !void { | |
const stdout = try std.io.getStdOut(); | |
var direct_allocator = std.heap.DirectAllocator.init(); | |
defer direct_allocator.deinit(); | |
var arena = std.heap.ArenaAllocator.init(&direct_allocator.allocator); | |
defer arena.deinit(); | |
var argsIter = std.os.args(); | |
const programName = try argsIter.next(&arena.allocator) orelse return; | |
var fileName = try (argsIter.next(&arena.allocator) orelse { | |
const stdin = try std.io.getStdIn(); | |
try copyFile(stdin, stdout); | |
return; | |
}); | |
while (true) { | |
const file = try File.openRead(fileName); | |
defer file.close(); | |
try copyFile(file, stdout); | |
fileName = try (argsIter.next(&arena.allocator) orelse break); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment