42 lines
1.1 KiB
Zig
42 lines
1.1 KiB
Zig
const std = @import("std");
|
|
const lib = @import("./lib.zig");
|
|
|
|
pub fn main() anyerror!void {
|
|
const args = try std.process.argsAlloc(std.heap.page_allocator);
|
|
|
|
if (args.len <= 1) {
|
|
try print_input();
|
|
}
|
|
|
|
const files = args[1..];
|
|
for (files) |f| {
|
|
if (std.mem.eql(u8, f, "-")) { try print_input(); }
|
|
else { try print_file (f); }
|
|
}
|
|
}
|
|
|
|
fn print_input() !void {
|
|
const stdin = std.io.getStdIn().reader();
|
|
var buffer: [4096]u8 = undefined;
|
|
while (true) {
|
|
const nbytes = try stdin.read(&buffer);
|
|
lib.print("{s}", .{buffer[0..nbytes]});
|
|
if (nbytes == 0) break;
|
|
}
|
|
}
|
|
|
|
fn print_file(dest: []const u8) !void {
|
|
// open file and defer closing
|
|
var file = try std.fs.cwd().openFile(dest, .{ .mode = .read_only });
|
|
defer file.close();
|
|
|
|
// read file content and print everything
|
|
var buffer: [4096]u8 = undefined;
|
|
var nbytes : u64 = 0;
|
|
while (true) {
|
|
nbytes = try file.read(&buffer);
|
|
lib.print("{s}", .{buffer[0..nbytes]});
|
|
if (nbytes == 0) break;
|
|
}
|
|
}
|