From 70e338981360fe65ded5b997f6394f09826ecb51 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 24 May 2020 23:02:04 +0300 Subject: Initial commit --- build.zig | 24 ++++++++++++++++++++++++ src/main.zig | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 build.zig create mode 100644 src/main.zig diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..5badf19 --- /dev/null +++ b/build.zig @@ -0,0 +1,24 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("zig", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..1b8f702 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,59 @@ +const std = @import("std"); +const adler32 = std.hash.Adler32; +const openFlags = std.fs.File.OpenFlags{ + .read = true, + .write = false, + .lock = std.fs.File.Lock.None, +}; + +fn dump(args: [][]u8) u8 { + if (args.len < 3) { + std.debug.warn("Usage: {} [subcommand] [options]\n", .{args[0]}); + return 255; + } + + var file = std.fs.openFileAbsolute(args[2], openFlags) catch { + std.debug.warn("Cannot open file {}\n", .{args[2]}); + return 255; + }; + defer file.close(); + + var buffer: [16]u8 = undefined; + var r: u64 = undefined; + while (true) { + r = file.read(&buffer) catch { + break; + }; + if (r <= 0) + break; + + for (buffer[0..r]) |c| { + std.debug.warn("0x{x:2} ", .{c}); + } + std.debug.warn(" -- {}\n", .{r}); + } + + return 0; +} + +fn help(args: [][]u8) u8 { + std.debug.warn("Usage: {} [subcommand] [options]\n", .{args[0]}); + return if (args.len < 2) 255 else 0; +} + +pub fn main() anyerror!u8 { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + + std.debug.warn("All your codebase are belong to us.\n\n", .{}); + + const args = try std.process.argsAlloc(&arena.allocator); + if (args.len < 2) { + return help(args); + } + + return switch (adler32.hash(args[1])) { + adler32.hash("dump") => dump(args), + else => help(args), + }; +} -- cgit v1.2.1