Archived
3
0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues or pull requests.
libipc-old/cat/cat.zig

35 lines
865 B
Zig
Raw Normal View History

2022-04-14 15:52:01 +02:00
const std = @import("std");
2022-05-01 00:41:11 +02:00
const stdout = std.io.getStdOut().writer();
2022-04-14 15:52:01 +02:00
pub fn main() anyerror!void {
const args = try std.process.argsAlloc(std.heap.page_allocator);
if (args.len <= 1) {
2022-05-01 00:41:11 +02:00
try print_input();
2022-04-14 15:52:01 +02:00
}
2022-05-01 00:41:11 +02:00
for (args[1..]) |f| {
if (std.mem.eql(u8, f, "-")) { try print_input(); }
else { try print_file (f); }
2022-04-14 15:52:01 +02:00
}
}
2022-05-01 00:41:11 +02:00
fn print_input() !void {
try print_all (std.io.getStdIn());
}
2022-04-14 15:52:01 +02:00
fn print_file(dest: []const u8) !void {
2022-05-01 00:41:11 +02:00
var file = try std.fs.cwd().openFile(dest, .{ .mode = .read_only });
defer file.close();
try print_all (file);
}
2022-04-14 15:52:01 +02:00
2022-05-01 00:41:11 +02:00
fn print_all(reader: std.fs.File) !void {
var buffer: [4096]u8 = undefined;
while (true) {
const nbytes = try reader.read(&buffer);
try stdout.print("{s}", .{buffer[0..nbytes]});
if (nbytes == 0) break;
}
2022-04-14 15:52:01 +02:00
}