Skip to content

Instantly share code, notes, and snippets.

@ljmccarthy
Created April 8, 2019 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljmccarthy/0cffd0ee9a06926e09d206ecef46bd1b to your computer and use it in GitHub Desktop.
Save ljmccarthy/0cffd0ee9a06926e09d206ecef46bd1b to your computer and use it in GitHub Desktop.
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