2016-04-19 00:42:56 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const io = std.io;
|
2019-05-27 04:35:26 +01:00
|
|
|
const process = std.process;
|
2019-11-30 20:14:04 +00:00
|
|
|
const fs = std.fs;
|
2017-02-26 19:35:30 +00:00
|
|
|
const mem = std.mem;
|
2020-09-27 06:00:41 +01:00
|
|
|
const warn = std.log.warn;
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
pub fn main() !void {
|
2020-12-01 03:18:40 +00:00
|
|
|
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
|
|
defer arena_instance.deinit();
|
2021-10-29 02:08:41 +01:00
|
|
|
const arena = arena_instance.allocator();
|
2020-12-01 03:18:40 +00:00
|
|
|
|
|
|
|
const args = try process.argsAlloc(arena);
|
2020-09-27 06:00:41 +01:00
|
|
|
|
2020-12-01 03:18:40 +00:00
|
|
|
const exe = args[0];
|
2016-01-16 10:10:15 +00:00
|
|
|
var catted_anything = false;
|
2019-11-13 01:36:07 +00:00
|
|
|
const stdout_file = io.getStdOut();
|
2017-10-31 08:47:55 +00:00
|
|
|
|
2019-11-30 20:14:04 +00:00
|
|
|
const cwd = fs.cwd();
|
|
|
|
|
2020-12-01 03:18:40 +00:00
|
|
|
for (args[1..]) |arg| {
|
2017-02-26 19:35:30 +00:00
|
|
|
if (mem.eql(u8, arg, "-")) {
|
2016-01-16 10:10:15 +00:00
|
|
|
catted_anything = true;
|
2020-12-01 03:18:40 +00:00
|
|
|
try stdout_file.writeFileAll(io.getStdIn(), .{});
|
|
|
|
} else if (mem.startsWith(u8, arg, "-")) {
|
2016-01-16 10:10:15 +00:00
|
|
|
return usage(exe);
|
|
|
|
} else {
|
2019-11-30 20:14:04 +00:00
|
|
|
const file = cwd.openFile(arg, .{}) catch |err| {
|
2020-12-01 03:18:40 +00:00
|
|
|
warn("Unable to open file: {s}\n", .{@errorName(err)});
|
2016-01-16 10:10:15 +00:00
|
|
|
return err;
|
2016-01-26 06:56:46 +00:00
|
|
|
};
|
2017-10-31 08:47:55 +00:00
|
|
|
defer file.close();
|
2016-01-14 01:15:51 +00:00
|
|
|
|
2016-01-16 10:10:15 +00:00
|
|
|
catted_anything = true;
|
2020-12-01 03:18:40 +00:00
|
|
|
try stdout_file.writeFileAll(file, .{});
|
2016-01-16 10:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-21 01:18:50 +00:00
|
|
|
if (!catted_anything) {
|
2020-12-01 03:18:40 +00:00
|
|
|
try stdout_file.writeFileAll(io.getStdIn(), .{});
|
2016-01-16 10:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 03:48:40 +00:00
|
|
|
fn usage(exe: []const u8) !void {
|
2021-01-03 09:20:37 +00:00
|
|
|
warn("Usage: {s} [FILE]...\n", .{exe});
|
2016-01-24 08:34:48 +00:00
|
|
|
return error.Invalid;
|
2016-01-16 10:10:15 +00:00
|
|
|
}
|