Skip to content

Instantly share code, notes, and snippets.

@maxsei
Created May 31, 2023 14:50
Show Gist options
  • Select an option

  • Save maxsei/5dfafdc6c433a40bd7ea9f5cb3c2e332 to your computer and use it in GitHub Desktop.

Select an option

Save maxsei/5dfafdc6c433a40bd7ea9f5cb3c2e332 to your computer and use it in GitHub Desktop.
unoptimized ls written in zig (still recurses subdirs)
const std = @import("std");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer {
const deinit_status = gpa.deinit();
if (deinit_status == .leak) std.log.err("leaked memory", .{});
}
const cwd = std.fs.cwd();
{
const p = try cwd.realpathAlloc(allocator, ".");
std.debug.print("cwd: {s}\n", .{p});
allocator.free(p);
}
const cwd_iterable = try cwd.openIterableDir(".", .{});
{
var walker = try cwd_iterable.walk(allocator);
defer walker.deinit();
while (true) {
const maybe_entry = try walker.next();
if (maybe_entry == null) {
break;
}
const entry = maybe_entry.?;
const depth = std.mem.count(u8, entry.path, "/");
if (depth > 0) continue;
std.debug.print("{s}\n", .{entry.basename});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment