Skip to content

Commit 3aa257c

Browse files
refactor: start over
1 parent be469a6 commit 3aa257c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1379
-1068
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
zig-cache
1+
.zig-cache
22
zig-out
3+
result
4+
result-cache

applets.zig

+50-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
1-
pub const arch = @import("applets/arch.zig");
2-
pub const cal = @import("applets/cal.zig");
3-
pub const cat = @import("applets/cat.zig");
4-
pub const chroot = @import("applets/chroot.zig");
5-
pub const @"false" = @import("applets/false.zig");
6-
pub const pwd = @import("applets/pwd.zig");
7-
pub const @"true" = @import("applets/true.zig");
8-
pub const umount = @import("applets/umount.zig");
9-
pub const uptime = @import("applets/uptime.zig");
10-
pub const yes = @import("applets/yes.zig");
1+
pub const all: []const []const u8 = &.{
2+
"arch",
3+
"basename",
4+
"cat",
5+
"chmod",
6+
"chroot",
7+
"date",
8+
"dsh",
9+
"expr",
10+
"false",
11+
"find",
12+
"grep",
13+
"head",
14+
"install",
15+
"mkdir",
16+
"mktemp",
17+
"nproc",
18+
"pwd",
19+
"rm",
20+
"sed",
21+
"sort",
22+
"tail",
23+
"tar",
24+
"true",
25+
"uptime",
26+
};
27+
28+
pub const min: []const []const u8 = &.{
29+
"arch",
30+
"basename",
31+
"cat",
32+
"date",
33+
"expr",
34+
"false",
35+
"find",
36+
"grep",
37+
"head",
38+
"install",
39+
"mkdir",
40+
"mktemp",
41+
"nproc",
42+
"pwd",
43+
"rm",
44+
"sed",
45+
"sort",
46+
"tail",
47+
"tar",
48+
"true",
49+
"uptime",
50+
};

applets/arch.zig

-7
This file was deleted.

applets/arch/main.zig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const builtin = @import("builtin");
2+
const std = @import("std");
3+
4+
pub fn main() !void {
5+
const stdout = std.io.getStdOut().writer().any();
6+
try stdout.writeAll(@tagName(builtin.cpu.arch) ++ "\n");
7+
}

applets/basename/main.zig

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const builtin = @import("builtin");
2+
const std = @import("std");
3+
const native_os = builtin.os.tag;
4+
5+
var arena_allocator: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
6+
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
7+
8+
pub fn main() !void {
9+
const gpa, const gpa_deinit = gpa: {
10+
if (native_os == .wasi) break :gpa .{ std.heap.wasm_allocator, false };
11+
if (builtin.single_threaded) break :gpa .{ arena_allocator.allocator(), true };
12+
break :gpa switch (builtin.mode) {
13+
.Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true },
14+
.ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false },
15+
};
16+
};
17+
defer if (gpa_deinit) {
18+
_ = debug_allocator.deinit();
19+
};
20+
21+
var args = try std.process.argsWithAllocator(gpa);
22+
defer args.deinit();
23+
24+
_ = args.skip();
25+
26+
if (args.next()) |comp| {
27+
const basename = std.fs.path.basename(comp);
28+
const stdout = std.io.getStdOut().writer().any();
29+
try stdout.writeAll(if (basename.len == 0) "/" else basename);
30+
try stdout.writeByte('\n');
31+
} else {
32+
const stderr = std.io.getStdErr().writer().any();
33+
try stderr.writeAll("basename: path is required\n");
34+
std.process.exit(1);
35+
}
36+
}

applets/cal.zig

-158
This file was deleted.

applets/cat.zig

-58
This file was deleted.

applets/cat/main.zig

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const builtin = @import("builtin");
2+
const std = @import("std");
3+
const native_os = builtin.os.tag;
4+
5+
var arena_allocator: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
6+
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
7+
8+
pub fn main() !void {
9+
const gpa, const gpa_deinit = gpa: {
10+
if (native_os == .wasi) break :gpa .{ std.heap.wasm_allocator, false };
11+
if (builtin.single_threaded) break :gpa .{ arena_allocator.allocator(), true };
12+
break :gpa switch (builtin.mode) {
13+
.Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true },
14+
.ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false },
15+
};
16+
};
17+
defer if (gpa_deinit) {
18+
_ = debug_allocator.deinit();
19+
};
20+
21+
var args = try std.process.argsWithAllocator(gpa);
22+
defer args.deinit();
23+
24+
_ = args.skip();
25+
26+
var i: usize = 0;
27+
28+
const raw_stdout = std.io.getStdOut().writer().any();
29+
var buffered_stdout = std.io.bufferedWriter(raw_stdout);
30+
defer buffered_stdout.flush() catch |err| std.debug.panic("Failed to flush stdout: {}", .{err});
31+
const stdout = buffered_stdout.writer().any();
32+
33+
while (args.next()) |p| : (i += 1) {
34+
var file = if (std.fs.path.isAbsolute(p)) try std.fs.openFileAbsolute(p, .{}) else try std.fs.cwd().openFile(p, .{});
35+
defer file.close();
36+
37+
const reader = file.reader();
38+
39+
while (reader.readByte() catch null) |b| try stdout.writeByte(b);
40+
}
41+
42+
if (i == 0) {
43+
const stderr = std.io.getStdErr().writer().any();
44+
try stderr.writeAll("cat: path is required\n");
45+
std.process.exit(1);
46+
}
47+
}

0 commit comments

Comments
 (0)