commit 4fc8fa37adeb5d55f3e67255ab888d8a61ae8d44 Author: Karchnu Date: Sat Dec 5 00:00:33 2020 +0100 First commands, with no error management whatsoever. diff --git a/README.md b/README.md new file mode 100644 index 0000000..598147f --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +This is an experimental repository. + +We just want to try Zig code, writing basic functions and commands. diff --git a/src/cat.zig b/src/cat.zig new file mode 100644 index 0000000..0c4bbff --- /dev/null +++ b/src/cat.zig @@ -0,0 +1,61 @@ +const std = @import("std"); +const mem = std.mem; +const stdout = std.io.getStdOut().writer(); +const process = std.process; + +const fs = std.fs; + +const warn = std.debug.warn; +const print = std.debug.print; + +const cli_arguments = @import("./cli_arguments.zig"); + +pub fn get_file_size(path: []const u8) !u64 { + var file = try fs.cwd().openFile(path, .{}); + defer file.close(); + + // Find the size of the file and create a buffer with this size. + var file_stat = try file.stat(); + return file_stat.size; +} + +fn print_file(path: []const u8) !void { + const size = try get_file_size(path); + // print("path: {}, size: {}\n", .{path, size}); + + // Create another allocator, for the file. + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + + const allocator = &arena.allocator; + const buffer = try allocator.alloc(u8, size + 1); // Last value will be a null-byte. + buffer[size] = 0; + + const content = try fs.cwd().readFile(path, buffer); + print("{}", .{content}); +} + +pub fn cat() !void { + // Here we use an ArenaAllocator backed by a DirectAllocator because `cat` is a short-lived, + // one shot program. We don't need to waste time freeing memory and finding places to squish + // bytes into. So we free everything all at once at the very end. + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + + const allocator = &arena.allocator; + var args = try process.argsAlloc(allocator); + defer process.argsFree(allocator, args); + + // Skipping the executable binary name. + var arg_idx: usize = 1; + const gui_file_path = cli_arguments.nextArg(args, &arg_idx) orelse { + warn("Expected first argument to be path to gui file\n", .{}); + return error.InvalidArgs; + }; + + try print_file(gui_file_path); +} + +pub fn main() !void { + try cat(); +} diff --git a/src/cli_arguments.zig b/src/cli_arguments.zig new file mode 100644 index 0000000..ac1ba7a --- /dev/null +++ b/src/cli_arguments.zig @@ -0,0 +1,6 @@ + +pub fn nextArg(args: [][]const u8, idx: *usize) ?[]const u8 { + if (idx.* >= args.len) return null; + defer idx.* += 1; + return args[idx.*]; +} diff --git a/src/ls.zig b/src/ls.zig new file mode 100644 index 0000000..afcce63 --- /dev/null +++ b/src/ls.zig @@ -0,0 +1,44 @@ +const std = @import("std"); +const mem = std.mem; +const stdout = std.io.getStdOut().writer(); +const process = std.process; + +const fs = std.fs; + +const warn = std.debug.warn; +const print = std.debug.print; + +const cli_arguments = @import("./cli_arguments.zig"); + +// TODO: error management. + +pub fn ls() !void { + + // Memory allocator for the cli arguments. + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + + const allocator = &arena.allocator; + var args = try process.argsAlloc(allocator); + defer process.argsFree(allocator, args); + + // Skipping the executable binary name. + var arg_idx: usize = 1; + + const directory = cli_arguments.nextArg(args, &arg_idx) orelse { + warn("Expected first argument to be path to gui file\n", .{}); + return error.InvalidArgs; + }; + + var current_working_directory: fs.Dir = fs.cwd(); + var cwd: fs.Dir = try current_working_directory.openDir(directory, .{.iterate = true}); + var dir_it = cwd.iterate(); + + while (try dir_it.next()) |entry| { + print("{}\n", .{entry.name}); + } +} + +pub fn main() !void { + try ls(); +}