Skip to content

refactor: start over #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
zig-cache
.zig-cache
zig-out
result
result-cache
60 changes: 50 additions & 10 deletions applets.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
pub const arch = @import("applets/arch.zig");
pub const cal = @import("applets/cal.zig");
pub const cat = @import("applets/cat.zig");
pub const chroot = @import("applets/chroot.zig");
pub const @"false" = @import("applets/false.zig");
pub const pwd = @import("applets/pwd.zig");
pub const @"true" = @import("applets/true.zig");
pub const umount = @import("applets/umount.zig");
pub const uptime = @import("applets/uptime.zig");
pub const yes = @import("applets/yes.zig");
pub const all: []const []const u8 = &.{
"arch",
"basename",
"cat",
"chmod",
"chroot",
"date",
"dsh",
"expr",
"false",
"find",
"grep",
"head",
"install",
"mkdir",
"mktemp",
"nproc",
"pwd",
"rm",
"sed",
"sort",
"tail",
"tar",
"true",
"uptime",
};

pub const min: []const []const u8 = &.{
"arch",
"basename",
"cat",
"date",
"expr",
"false",
"find",
"grep",
"head",
"install",
"mkdir",
"mktemp",
"nproc",
"pwd",
"rm",
"sed",
"sort",
"tail",
"tar",
"true",
"uptime",
};
7 changes: 0 additions & 7 deletions applets/arch.zig

This file was deleted.

7 changes: 7 additions & 0 deletions applets/arch/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const builtin = @import("builtin");
const std = @import("std");

pub fn main() !void {
const stdout = std.io.getStdOut().writer().any();
try stdout.writeAll(@tagName(builtin.cpu.arch) ++ "\n");
}
36 changes: 36 additions & 0 deletions applets/basename/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const builtin = @import("builtin");
const std = @import("std");
const native_os = builtin.os.tag;

var arena_allocator: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;

pub fn main() !void {
const gpa, const gpa_deinit = gpa: {
if (native_os == .wasi) break :gpa .{ std.heap.wasm_allocator, false };
if (builtin.single_threaded) break :gpa .{ arena_allocator.allocator(), true };
break :gpa switch (builtin.mode) {
.Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true },
.ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false },
};
};
defer if (gpa_deinit) {
_ = debug_allocator.deinit();
};

var args = try std.process.argsWithAllocator(gpa);
defer args.deinit();

_ = args.skip();

if (args.next()) |comp| {
const basename = std.fs.path.basename(comp);
const stdout = std.io.getStdOut().writer().any();
try stdout.writeAll(if (basename.len == 0) "/" else basename);
try stdout.writeByte('\n');
} else {
const stderr = std.io.getStdErr().writer().any();
try stderr.writeAll("basename: path is required\n");
std.process.exit(1);
}
}
158 changes: 0 additions & 158 deletions applets/cal.zig

This file was deleted.

58 changes: 0 additions & 58 deletions applets/cat.zig

This file was deleted.

47 changes: 47 additions & 0 deletions applets/cat/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const builtin = @import("builtin");
const std = @import("std");
const native_os = builtin.os.tag;

var arena_allocator: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;

pub fn main() !void {
const gpa, const gpa_deinit = gpa: {
if (native_os == .wasi) break :gpa .{ std.heap.wasm_allocator, false };
if (builtin.single_threaded) break :gpa .{ arena_allocator.allocator(), true };
break :gpa switch (builtin.mode) {
.Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true },
.ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false },
};
};
defer if (gpa_deinit) {
_ = debug_allocator.deinit();
};

var args = try std.process.argsWithAllocator(gpa);
defer args.deinit();

_ = args.skip();

var i: usize = 0;

const raw_stdout = std.io.getStdOut().writer().any();
var buffered_stdout = std.io.bufferedWriter(raw_stdout);
defer buffered_stdout.flush() catch |err| std.debug.panic("Failed to flush stdout: {}", .{err});
const stdout = buffered_stdout.writer().any();

while (args.next()) |p| : (i += 1) {
var file = if (std.fs.path.isAbsolute(p)) try std.fs.openFileAbsolute(p, .{}) else try std.fs.cwd().openFile(p, .{});
defer file.close();

const reader = file.reader();

while (reader.readByte() catch null) |b| try stdout.writeByte(b);
}

if (i == 0) {
const stderr = std.io.getStdErr().writer().any();
try stderr.writeAll("cat: path is required\n");
std.process.exit(1);
}
}
Loading